node.js - 在服务器之间扩展 socket.io

标签 node.js socket.io

扩展 socket.io 应用程序的方法有哪些?我看到以下问题,我不明白如何解决:

  • 缩放的 socket.io 应用程序如何向房间广播?换句话说,socket.io 如何知道来自其他服务器的邻居?

我很难想象它应该如何工作——也许是所有必要信息的共享变体存储,例如 redis——这有可能吗?

编辑:我找到了这篇文章:http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html

基于它我做了以下事情:

   var pub = redis.createClient();  
   var sub = redis.createClient();
   var store = redis.createClient();
   pub.auth("pass");
   sub.auth("pass");
   store.auth("pass");

    io.configure( function(){
io.enable('browser client minification');  // send minified client
io.enable('browser client etag');          // apply etag caching logic based on version number
    io.enable('browser client gzip');          // gzip the file
io.set('log level', 1);                    // reduce logging
io.set('transports', [                     // enable all transports (optional if you want flashsocket)
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);
var RedisStore = require('socket.io/lib/stores/redis');
io.set('store', new RedisStore({redisPub:pub, redisSub:sub, redisClient:store}));
    });

但是我得到以下错误:

      Error: Uncaught, unspecified 'error' event.
     at RedisClient.emit (events.js:50:15)
     at Command.callback (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/index.js:232:29)
     at RedisClient.return_error (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/index.js:382:25)
     at RedisReplyParser.<anonymous> (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/index.js:78:14)
     at RedisReplyParser.emit (events.js:67:17)
     at RedisReplyParser.send_error (    /home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/lib/parser/javascript.js:265:14)
     at RedisReplyParser.execute (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/lib/parser/javascript.js:124:22)
     at RedisClient.on_data (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/index.js:358:27)
     at Socket.<anonymous> (/home/qwe/chat/io2/node_modules/socket.io/node_modules/redis/index.js:93:14)
     at Socket.emit (events.js:67:17)

我的 Redis 凭据绝对正确。

编辑:很奇怪,但禁用 Redis 授权后一切正常。所以这个问题仍然有效。另外,我有一个问题,关于如何在这种 RedisStorage 模式下获取一个组(房间)的所有参与者的信息(例如用户名),是否可以实现?理想情况下,这可以通过 Redis Pub/Sub 功能来完成。

最佳答案

你可以使用 socket.io 集群让它工作 https://github.com/muchmala/socket.io-cluster

关于node.js - 在服务器之间扩展 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893350/

相关文章:

node.js - 如何让socket.io在pm2集群模式下正常工作?

javascript - 使用 Express REST API 编辑帖子

javascript - 使用云函数将函数值添加到 firestore 中

node.js - 是否可以在执行当前路由句柄之前执行另一个路由句柄?

node.js - 在重新启动之间保存 readline CLI 命令历史记录

node.js - Socket.IO 问题与非法来源?

android - 回收站 View : No adapter attached; skipping layout with android socket io

javascript - 为什么 socket.post() 比 socket.emit() 慢 [太慢了]

javascript - 调用nodejs文件的onclick函数

Socket.IO 失败 : Error during WebSocket handshake: Unexpected response code: 400