草庐IT

node.js - Socket.IO 和 Node.js 核心集群

coder 2023-07-18 原文

是否可以将 Socket.IO 与 Node 的核心集群(不是过时的模块)一起使用?

我可以 fork 多个 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]}

这是控制台输出在失败时结束的方式(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 上添加了可能的门票链接:

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

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

最佳答案

问题似乎与 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.s 0.8 中,集群库看起来应该更加成熟,因此上述代码/问题可能会过时:http://nodejs.org/docs/v0.7.8/api/cluster.html

关于node.js - Socket.IO 和 Node.js 核心集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10544350/

有关node.js - Socket.IO 和 Node.js 核心集群的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby-on-rails - Assets 管道损坏 : Not compiling on the fly css and js files - 2

    我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1

  7. ruby-on-rails - Rails - 理解 application.js 和 application.css - 2

    rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:

  8. node.js - 如何在 Travis CI 上的一个项目中运行 Node.js 和 Ruby 测试 - 2

    我有一个包含多个组件的存储库,其中大部分是用JavaScript(Node.js)编写的,一个是用Ruby(RubyonRails)编写的。我想要一个.travis.yml文件来触发一个运行每个组件的所有测试的构建。根据thisTravisCIGoogleGroupthread,目前还没有官方支持。我的目录结构是这样的:.├──构建服务器├──核心├──扩展├──网络应用├──流浪文件├──package.json├──.travis.yml└──生成文件我希望能够运行特定版本的Ruby(2.2.2)和Node.js(0.12.2)。我已经有了一个make目标,所以maketest在每

  9. ruby-on-rails - ruby open ssl api for encrypted key (without nodes option) - 2

    在安装了openssllib的linux机器上,当您执行带有“-nodes”选项的“opensslpkcs12”时,您将获得带有未加密私钥的输出,但如果您跳过–nodes选项,则输出将具有加密的私钥。e.g.opensslpkcs12-intest.pfx-outtest.pem你应该看到像下面这样加密的私钥-----BEGINENCRYPTEDPRIVATEKEY-----MIIFDjBABgkqhkiGG7s=-----ENDENCRYPTEDPRIVATEKEY-----如何使用ruby​​的开放ssl库实现上述目标?这就是我用ruby​​生成私钥的方式:@private_key

  10. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

随机推荐