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

我正在尝试开始使用node.js和socket.io。
我有一个非常( 非常 )简单的客户端服务器testing应用程序,我不能让它工作。
该系统设置在一台带有apache服务器的Windows机器上。
apache在port 81上运行,而socket.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'); }); 

客户端 – 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> 

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

 info: socket.io started 

当我在浏览器中打开index.html (在我的例子中是chrome – 没有在其他浏览器上testing),以下日志被写入服务器控制台:

 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 ... ... ... 

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

 connect_failed 

因此,似乎客户端到达服务器并尝试连接,并且服务器正在授权握手,但connect事件从未在客户端触发,而是connect方法达到其connect timeoutmax reconnection attempts并放弃返回connect_failed

任何帮助\洞察这将是有益的。

谢谢

 //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');` }); 

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

服务器端:和客户端一样,使得socketIO方法可以在var io下使用。

更改前端(客户端):

var socket = io.connect(http://myapp.herokuapp.com);

var socket = io.connect();

经过几个小时处理与服务器没有正确注册连接的各种客户端(Chrome,Firefox浏览器和Phonegap应用程序)(即服务器在其日志中成功注册websocket连接,但前端没有注册连接事件 ),我从评论中尝试了@蒂莫西的解决scheme,现在一切都在heroku和localhost上运行。

尝试以下操作:

更改

 var socket = io.connect('http://localhost', { port: 8001 }); 

 var socket = io.connect('http://localhost:8001'); 
 // server var server = http.createServer(app).listen(8001); // client var socket = io.connect(); 

尝试使用这个:

Server.js

 var http = require("http").createServer(), io = require("socket.io").listen(http); http.listen(8001); io.sockets.on("connection", function(socket){ console.log("Connected!"); socket.emit("foo", "Now, we are connected"); }); 

的index.html

 <script src="http://localhost:8001/socket.io/socket.io.js"></script> <script> var i = io.connect("http://localhost:5001"); i.on("foo", function(bar){ console.log(bar); }) </script>