我想要开始使用node.js和socket.io。
我有一个非常(非常)简单的客户端服务器测试应用程序,我不能让它工作。
该系统设置在带有apache服务器的Windows机器上。
的Apache是在port 81
运行,并且在socket.io设置听port 8001
socket.io - 'connect'事件不会在客户端触发
服务器 - 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
启动服务器后,标准(预期)输出为wr伊顿服务器控制台:
info: socket.io started
当我在浏览器中打开index.html
(铬在我的情况 - 在其他浏览器没有测试)以下日志写入服务器控制台:
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 timeout
和max reconnection attempts
,并放弃返回connect_failed
。
任何帮助\对此的见解将有所帮助。
谢谢
只用io.connect(),它可以推断尝试来自您所在网址的网址和端口。我遇到过问题,io.connect中的事情并不像我期望的那样工作。 – 2012-04-05 18:45:26
@kfiroo你有没有得到任何解决方案? – Hackableweb 2013-07-19 01:11:07
@TimothyStrimple谢谢蒂姆! **再次在heroku(Cedar)和localhost **上工作! – AmpT 2014-04-10 23:34:23