socket.io - 'connect'事件不会在客户端上触发

kfi*_*roo 29 javascript node.js socket.io

我正在尝试使用node.js和socket.io.
我有一个非常(非常)简单的客户端 - 服务器测试应用程序,我无法让它工作.
系统设置在带有apache服务器的Windows机器上.
apache正在运行,port 81socket.io已设置为侦听port 8001

服务器 - server.js

var io = require('socket.io').listen(8001);

io.sockets.on('connection', function (socket) {
    console.log('\ngot a new connection from: ' + socket.id + '\n');
});
Run Code Online (Sandbox Code Playgroud)

客户端 - index.html

<!DOCTYPE html>
<html>
<head>
    <title>node-tests</title>
    <script type="text/javascript" src="http://localhost:8001/socket.io/socket.io.js">    </script>
    <script type="text/javascript">
        var socket = io.connect('http://localhost', { port: 8001 });

        socket.on('connect', function () {
            alert('connect');
        });

        socket.on('error', function (data) {
            console.log(data || 'error');
        });

        socket.on('connect_failed', function (data) {
            console.log(data || 'connect_failed');
        });
    </script>
</head>
<body>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

启动我的服务器后,使用node server.js标准(预期)输出写入服务器控制台:

info: socket.io started
Run Code Online (Sandbox Code Playgroud)

当我index.html在浏览器中打开时(在我的情况下为chrome - 未在其他浏览器上测试),以下日志将写入服务器控制台:

info: socket.io started
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized 9952353481638352052
debug: setting request GET /socket.io/1/websocket/9952353481638352052
debug: set heartbeat interval for client 9952353481638352052
debug: client authorized for

got a new connection from: 9952353481638352052

debug: setting request GET /socket.io/1/xhr-polling/9952353481638352052?t=1333635235315
debug: setting poll timeout
debug: discarding transport
debug: cleared heartbeat interval for client 9952353481638352052
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635238316&i=0
debug: setting poll timeout
debug: discarding transport
debug: clearing poll timeout
debug: clearing poll timeout
debug: jsonppolling writing io.j[0]("8::");
debug: set close timeout for client 9952353481638352052
debug: jsonppolling closed due to exceeded duration
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635258339&i=0
...
...
...
Run Code Online (Sandbox Code Playgroud)

在浏览器控制台中我得到:

connect_failed
Run Code Online (Sandbox Code Playgroud)

因此,它似乎像客户端到达服务器,并尝试连接,服务器被授权握手,但该connect事件从未在客户端发射,而不是连接方法达到connect timeoutmax reconnection attempts和放弃返回connect_failed.

任何帮助\洞察力都会有所帮助.

谢谢

jDu*_*ubs 8

//Client side
<script src="http://yourdomain.com:8001/socket.io/socket.io.js"></script>
var socket = io.connect('http://yourdomain.com:8001');

//Server side
var io = require('socket.io').listen(8001);
io.sockets.on('connection',function(socket) {
`console.log('a user connected');`
});
Run Code Online (Sandbox Code Playgroud)

客户端:客户端代码从提供服务器端套接字IO的同一文件请求套接字IO的客户端版本.Var socket现在将具有socketIO可用的所有方法,例如socket.emit或socket.on

服务器端:与客户端相同,使得socketIO方法可以在var io下使用.


jus*_*ing 7

确保客户端和服务器具有相同的 socket.io 主要版本。例如,socket.io@2.3.0在服务器上和socket.io-client@3.x.x在客户端的组合导致这种情况,并且不分派 'connect' 事件。


zal*_*rak -1

请尝试以下操作:

改变

var socket = io.connect('http://localhost', { port: 8001 });
Run Code Online (Sandbox Code Playgroud)

var socket = io.connect('http://localhost:8001');
Run Code Online (Sandbox Code Playgroud)