【发布时间】:2012-10-14 12:00:37
【问题描述】:
每当服务器收到请求时,我想在服务器端(使用 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://localhost:9876/');
localSocket.on('connect', function (data) {
response.writeHead(200, {"Content-type": "text/plain"});
response.write(data.name);
response.end();
localSocket.disconnect();
});
}).listen(8080);
Socket.io 服务器(在 localhost:9876 上)运行良好 - 它为具有名称的客户端提供服务(即
data.name
在此处的代码中)。
怎么了?
更新:问题自己解决了:要为socket.io-client运行多个连接需要添加连接选项{'force new connection': true},像这样:
localSocket = io.connect('http://localhost:9876/', {'force new connection': true});
谢谢!
【问题讨论】:
-
感谢您更新了答案。我建议不要更新,而是将其添加为答案
-
force new connection已从 socket.io 1.0 更改为forceNew