是否可以将 Socket.IO 与 Node 的核心集群(不是过时的模块)一起使用?
我可以分叉多个工人,它似乎工作正常;但是,打开连接时出现错误: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上的代码,尽管我看不出该代码有什么不同使用内存存储。
我所有的测试连接都使用 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 上添加了可能票证的链接: