简体   繁体   English

nodejs、socket、https - Socket 没有连接但只是断开连接

[英]nodejs, socket, https - Socket makes no connection but only disconnects

I've been through A LOT of issues in here about this topic but so far no answers has been the solution for my problem.我在这里遇到了很多关于这个主题的问题,但到目前为止还没有答案可以解决我的问题。

My setup is an AWS EC2 server with apache installed.我的设置是安装了 apache 的 AWS EC2 服务器。 I've installed node.js in the /node folder containing a server-file called server.js and in the root folder I have my client-file index.php .我已经在/node文件夹中安装了 node.js ,其中包含一个名为server.js的服务器文件,在根文件夹中我有我的客户端文件index.php I have Let´s Encrypt SSL-certificates for my domain and therefore I need everything to run over https.我为我的域加密了 SSL 证书,因此我需要通过 https 运行所有内容。 I have opened up the port 3000 for all traffic en AWS.我已经为 AWS 上的所有流量打开了端口 3000。

Server.js服务器.js

const app = require('express')();
const fs = require('fs');

const options = {
    key: fs.readFileSync("/etc/letsencrypt/live/example.com/privkey.pem"),
    cert: fs.readFileSync("/etc/letsencrypt/live/example.com/fullchain.pem")
};

const http = require('https').Server(options, app);
const io = require('socket.io')(http);


io.on('connection', (socket) => {
  socket.on('chat message', (msg) => {
    console.log('message: ' + msg);
  });
});

http.listen(3000, function(){
    console.log('listening on 3000');
});

index.php索引.php

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Socket Test</title>
</head>
<body>
    <ul id="events"></ul>

    <script src="https://cdn.jsdelivr.net/npm/socket.io-client@2/dist/socket.io.js"></script>
    <script>
        const events = document.getElementById('events');

        const newItem = (content) => {
            const item = document.createElement('li');
            item.innerText = content;
            return item;
        };

        const socket = io('https://example.com:3000', {transports: ['websocket', 'polling']});
        
        console.log(socket);
    
        socket.on('connect', () => {
            console.log("did connect");
            events.appendChild(newItem('connect'));
        });
        
        socket.on('disconnect', () => {
            console.log("did disconnect");
        });
    </script>
</body>
</html>

The problem问题

When I start my node server everything seems right.当我启动我的节点服务器时,一切似乎都正确。 Executing node server.js returns listening on 3000 as supposed.执行node server.js按预期返回listening on 3000 When I go to index.php it never returns did connect BUT when I then exit the server node I get the did disconnect message in the browser console.当我转到 index.php 时,它永远不会返回did connect但是当我退出服务器节点时,我在浏览器控制台中收到了did disconnect消息。

Also the server never returns anything else but listening on 3000 .此外,服务器除了listening on 3000从不返回任何其他内容。

Please help me :-)请帮我 :-)

Since the recent update to v3.0.0, version 2.xx clients are not compatible so won't be able to connect.由于最近更新到 v3.0.0,版本 2.xx 客户端不兼容,因此将无法连接。

The fix is to make sure both client and server are using the same versions, or use the server served version /socket.io/socket.io.js (which is not always possible, webpack etc)修复是确保客户端和服务器都使用相同的版本,或者使用服务器服务的版本/socket.io/socket.io.js (这并不总是可能的,webpack 等)

Related issue on github: https://github.com/socketio/socket.io-client/issues/1390 github上的相关问题: https : //github.com/socketio/socket.io-client/issues/1390

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM