是否可以将Socket.IO与Node的核心集群(而不是过时的模块)一起使用?
我可以分叉多个工人,它似乎工作正常;但是,当打开连接时,我收到错误:solve: warn - client not handshaken client should reconnect
以下是相关的代码段(删除了一些简单的内容,例如expressjs config):
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]}
这是控制台输出在失败时结束的方式(10次失败大约9次):
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上添加了可能的门票链接:
答案 0 :(得分:3)
似乎问题可能与Socket.IO随附的节点模块有关。
当我安装redis(npm install hiredis redis
)并使用redis模块为RedisStore创建客户端时,一切都突然完美。我已经运行了超过一个小时,有大约500个并发连接,并且没有看到任何一个错误,并且正在使用每个节点进程。
hiredis@0.1.14 redis@0.7.2
在端口6379上运行Redis 2.6rc3。
更新:在Node.s 0.8中,看起来群集库应该更加成熟,因此上述代码/问题可能会过时:http://nodejs.org/docs/v0.7.8/api/cluster.html