Socket.io连接到服务器但未建立与任何服务的连接

时间:2013-05-13 13:53:08

标签: javascript node.js websocket socket.io

所以,我正在尝试设置一个socket.io的东西,用于在我的网站的页面上来回传递聊天和其他消息,但是我无法启动并运行它。会发生什么,我启动我的服务器,打开客户端页面,服务器看到有人正在尝试连接并说了很多东西。从客户端的角度来看,它尝试连接webockets,超时,然后xhr-polling,也超时,然后jsonp-polling,也超时,然后连接失败。有时一段时间它会连接一秒,然后掉落它,但这并不理想。我正在尝试连接chrome,并设置了另一个有效的websocket服务器,所以我知道websockets至少是可能的,但不能跨浏览器兼容,足以满足我的需求,因此socket.io。我想知道我在正确连接时做错了什么。

server.js

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

io.sockets.on('connection', function (socket) {
    socket.emit('connect_event', { hello: 'world' });
    socket.on('message', function (msg) { socket.emit('message', msg);});
    socket.on('disconnect', function () { });
});

client.php

<script src="http://domain.com:8080/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('domain.com',{port: 8080});
socket.on('connecting', function (e) {console.log("attempting to connect with: "+e)});
socket.on('disconnect', function (e) {console.log(e)});
socket.on('connect_failed', function () {console.log("Connect Failed")});
socket.on('error', function (e) {console.log(e)});
socket.on('message', function (e) {console.log(e)});
socket.on('reconnect_failed', function (e) {console.log(e)});
socket.on('reconnect', function (e) {console.log(e)});
socket.on('reconnecting', function (e) {console.log(e)});

socket.on('connect', function () {
    socket.send('hi');
    console.log('connected');
});
socket.on('message', function (msg) {
    console.log(msg);
});
</script>

浏览器中的JS控制台输出:

attempting to connect with: websocket client.php:5
attempting to connect with: xhr-polling client.php:5
attempting to connect with: jsonp-polling client.php:5
Connect Failed client.php:7

服务器调试信息:

debug - served static content /socket.io.js
debug - client authorized
info  - handshake authorized yJKfSwjarsZxert_Ij5V
debug - setting request GET /socket.io/1/websocket/yJKfSwjarsZxert_Ij5V
debug - set heartbeat interval for client yJKfSwjarsZxert_Ij5V
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"connect_event","args":[{"hello":"world"}]}
debug - setting request GET /socket.io/1/xhr-polling/yJKfSwjarsZxert_Ij5V?t=1368452739259
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client yJKfSwjarsZxert_Ij5V
debug - setting request GET /socket.io/1/jsonp-polling/yJKfSwjarsZxert_Ij5V?t=1368452749248&i=0
debug - setting poll timeout
debug - discarding transport
debug - clearing poll timeout

当服务器运行时,我可以访问domain.com:8080,它说

Welcome to socket.io.

所以这个端口也在netstat中打开 - 但它有这个用于确认目的:

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN

没有任何内容被添加到error.log中,只有页面的get请求被添加到访问日志中。

Socket.io版本0.9.14

node.js v 0.10.5 建议尝试不同的节点版本(特别是0.8.x),但它们似乎都是相同的。

服务器是使用Ubuntu 12.04 LTS的VPS

0 个答案:

没有答案