Socket.IO和Node.js核心集群

3

能否在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上可能的反馈链接:

https://github.com/LearnBoost/socket.io/issues/881

https://github.com/LearnBoost/socket.io/issues/438


我还没有尝试过你的代码,但是io.listen(app)在哪里调用了呢? - Matt Sergeant
我在我的全局变量中调用它:io = require('socket.io').listen(app) - dak
1
我很惊讶你的示例居然能够工作--看起来你将你的RedisStore指向了localhost:8888,这也是你的Web服务器所在的位置。你应该将你的RedisStore指向你的redis服务器(默认端口为6379)。 - Linus Thiel
1
不,绝对不是这样的。您需要自己运行一个Redis服务器才能使用RedisStore。我觉得很奇怪socket.io没有给您关于此的错误提示。 - Linus Thiel
本以为会成功。我已经在我的系统上运行了Redis,端口为6379(并更改了我的代码以连接到该端口),第一个连接正常工作,但是打开新的浏览器选项卡并尝试连接后,会导致握手错误。随后的连接似乎又出现了相同的问题。 - dak
显示剩余4条评论
1个回答

3
看起来问题可能与 Socket.IO 预装的 Node 模块有关。当我安装了 Redis(npm install hiredis redis)并使用 redis 模块为 RedisStore 创建客户端后,一切突然正常工作。我已经运行了一个多小时,有大约 500 个并发连接,并没有看到任何错误,并且每个 Node 进程都被利用。hiredis@0.1.14,redis@0.7.2,在端口 6379 上运行 Redis 2.6rc3。更新:在 Node.js 0.8 中,集群库看起来应该更加成熟,因此上述代码/问题可能会变得过时:http://nodejs.org/docs/v0.7.8/api/cluster.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接