node.js上的Socket.io-client只能工作一次
每次服务器收到请求时,我都希望在服务器端(使用node.js)创建新的sockei.io-client。但是下面的代码只工作一次,然后sockei.io-client不响应。应用程序没有停止,没有错误node.js上的Socket.io-client只能工作一次,node.js,socket.io,Node.js,Socket.io,每次服务器收到请求时,我都希望在服务器端(使用node.js)创建新的sockei.io-client。但是下面的代码只工作一次,然后sockei.io-client不响应。应用程序没有停止,没有错误 var http = require("http"); http.createServer(function(request, response) { var io = require('socket.io-client'); localSocket = io.connect('http:
var http = require("http");
http.createServer(function(request, response) {
var io = require('socket.io-client');
localSocket = io.connect('http://localhost:9876/');
localSocket.on('connect', function (data) {
response.writeHead(200, {"Content-type": "text/plain"});
response.write(data.name);
response.end();
localSocket.disconnect();
});
}).listen(8080);
io服务器(在localhost:9876上)运行良好-它为具有名称的客户端提供服务(这是
在这里的代码中)
怎么了
更新:问题由我自己解决:要为socket.io-client运行多个连接,需要添加连接选项{'force new connection':true},如下所示:
localSocket = io.connect('http://localhost:9876/', {'force new connection': true});
谢谢大家!
对于那些在谷歌上找到这个的人,现在有一个解决方案:
Socket.disconnect()启动客户端(服务器端)。客户端没有机会保持连接:)
我想您只需要删除disconnect()行。我知道disconnect()的作用。我想创建新的socket.io-client,并在每次收到http请求时连接到服务器。感谢您更新了答案。我建议添加它,而不是更新,因为从socket.io 1.0开始,answer
forcenewconnection
已更改为forceNew
localSocket = io.connect('http://localhost:9876/', {'force new connection': true});