Javascript socket.io - 'connect' 事件不会在客户端触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10030639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.io - 'connect' event does not fire on the client
提问by kfiroo
I am trying to get started with node.js and socket.io.
I have a very (very) simple client-server test application and i can't get it to work.
The system is set up on a windows machine with an apache server.
The apache is running on port 81
and the the socket.io is set up to listen to port 8001
我正在尝试开始使用 node.js 和 socket.io。
我有一个非常(非常)简单的客户端-服务器测试应用程序,但我无法让它工作。
该系统设置在带有 apache 服务器的 Windows 机器上。
apache 正在运行,port 81
socket.io 设置为监听port 8001
Server- server.js
服务器- 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');
});
Client- index.html
客户端- 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>
After starting my server using node server.js
the standard (expected) output is written to the server console:
使用node server.js
标准(预期)输出启动服务器后,将写入服务器控制台:
info: socket.io started
When I open index.html
in the browser (chrome in my case - didn't test on other browsers) the following log is written to the server console:
当我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
...
...
...
In the browsers console I get:
在浏览器控制台中,我得到:
connect_failed
So it seems like the client is reaching the server and trying to connect, and the server is authorizing the handshake but the connect
event is never fired in the client, instead the connect method reaches its connect timeout
and max reconnection attempts
and gives up returning connect_failed
.
因此,它似乎像客户端到达服务器,并尝试连接,服务器被授权握手,但该connect
事件从未在客户端发射,而不是连接方法达到connect timeout
与max reconnection attempts
和放弃返回connect_failed
。
Any help\insight on this would be helpful.
对此的任何帮助\见解都会有所帮助。
Thank you
谢谢
回答by jDubs
//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');`
});
Client side: The client side code requests the client version of socket IO from the same file that gives you server side socketIO. Var socket will now have all the methods that are available with socketIO, such as socket.emit or socket.on
客户端:客户端代码从为您提供服务器端 socketIO 的同一文件请求套接字 IO 的客户端版本。Var socket 现在将拥有 socketIO 可用的所有方法,例如 socket.emit 或 socket.on
Server Side: same as client side, making the socketIO methods available for use under var io.
服务器端:与客户端相同,使 socketIO 方法可在 var io 下使用。
回答by AmpT
Change in the frontend (client):
前端(客户端)的变化:
var socket = io.connect(http://myapp.herokuapp.com);
var socket = io.connect(http://myapp.herokuapp.com);
to
到
var socket = io.connect();
var socket = io.connect();
After many hours of dealing with various clients (Chrome, Firefox browsers and Phonegap app) that did not register connection to the server properly (i.e. the server registered the websocket connection successfully in its logs, but the frontend did not register the connection event), I tried @Timothy's solution from the comments and now everything works again on heroku and on localhost.
经过数小时处理未正确注册到服务器的连接的各种客户端(Chrome、Firefox 浏览器和 Phonegap 应用程序)(即服务器在其日志中成功注册了 websocket 连接,但前端没有注册连接事件),我从评论中尝试了@Timothy 的解决方案,现在一切都在 heroku 和 localhost 上再次运行。
回答by Stepan Yakovenko
With socketio 2.3.0 you have to use socket.on('connect')
使用 socketio 2.3.0 你必须使用 socket.on(' connect')
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io({
transports: ['polling']
});
console.log("created",socket);
socket.on('connect', function(data){ // <-- this works
console.log("socket ON connect",data);
});
socket.on('connection', function(data){ // <-- this fails
console.log("socket ON connection",data);
});
</script>
回答by ZeroBryan
Try using this:
尝试使用这个:
Server.js
服务器.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
索引.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>
回答by zallarak
Try the following:
请尝试以下操作:
Change
改变
var socket = io.connect('http://localhost', { port: 8001 });
to
到
var socket = io.connect('http://localhost:8001');