能否在Node的核心集群中使用Socket.IO(而不是过时的模块)?
我可以派生多个worker,看起来完全没有问题;但是,当打开连接时,我会收到错误:solve: warn - client not handshaken client should reconnect
以下是相关的代码片段(删除了一些简单的内容,如expressjs配置):
if ( cluster.isMaster ) {
for ( var i = 0; i < numCPUs; i++ ) {
cluster.fork();
}
} else {
app.get('/', function (req, res) {
res.sendfile( __dirname + '/public/html/index.html' );
});
io.configure( function() {
var RedisStore = require('socket.io').RedisStore,
opts = { host: 'localhost', port: 8888 };
io.set('store', new RedisStore( { redisPub: opts, redisSub: opts, redisClient: opts } ));
});
app.listen( 8888 );
io.sockets.on('connection', function (socket) {
socket.emit( 'some', 'data' );
});
}
我已经尝试过使用RedisStore和这个网站上的技巧(我认为现在已经过时了):http://www.danielbaulig.de/socket-ioexpress/ 我还查看了http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html上的代码,尽管我不知道那段代码与使用MemoryStore有何不同。
所有我的测试连接都使用Websockets(RFC 6455)。 如果我将numCPUs设置为1,这很好用。
Node.js版本0.6.17 Socket.io版本0.9.5 Expressjs版本2.5.9
更新-包括控制台输出(请注意,在此尝试中,连接最终确实起作用,尽管它抛出了相同的错误):
info - socket.io started
info - socket.io started
info - socket.io started
info - socket.io started
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 17644195072103946664
debug - setting request GET /socket.io/1/websocket/17644195072103946664
debug - set heartbeat interval for client 17644195072103946664
debug - websocket writing 7:::1+0
warn - client not handshaken client should reconnect
info - transport end (error)
debug - set close timeout for client 17644195072103946664
debug - cleared close timeout for client 17644195072103946664
debug - cleared heartbeat interval for client 17644195072103946664
debug - discarding transport
debug - client authorized
info - handshake authorized 16098526291524652257
debug - setting request GET /socket.io/1/websocket/16098526291524652257
debug - set heartbeat interval for client 16098526291524652257
debug - websocket writing 7:::1+0
warn - client not handshaken client should reconnect
info - transport end (error)
debug - set close timeout for client 16098526291524652257
debug - cleared close timeout for client 16098526291524652257
debug - cleared heartbeat interval for client 16098526291524652257
debug - discarding transport
debug - client authorized
info - handshake authorized 13419993801561067603
debug - setting request GET /socket.io/1/websocket/13419993801561067603
debug - set heartbeat interval for client 13419993801561067603
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"some":"data","args":[11354]}
debug - websocket writing 5:::{"some":"data","args":[36448]}
这是当控制台输出失败时的结束情况(大约十次中会有九次失败):
info - transport end by forced client disconnection
debug - websocket writing 0::
info - transport end (booted)
debug - set close timeout for client 1639301251431944437
debug - cleared close timeout for client 1639301251431944437
debug - cleared heartbeat interval for client 1639301251431944437
debug - discarding transport
debug - got disconnection packet
debug - got disconnection packet
更新 - 添加了github上可能的反馈链接:
io = require('socket.io').listen(app)
。 - dakRedisStore
指向了localhost:8888
,这也是你的Web服务器所在的位置。你应该将你的RedisStore
指向你的redis服务器(默认端口为6379
)。 - Linus Thiel