在服务器之间扩展socket.io

use*_*168 6 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}));
    });
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

      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)
Run Code Online (Sandbox Code Playgroud)

我的Redis凭证绝对是正确的.

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

Ter*_*ska 0

您可以使用 socket.io cluster 来实现此功能 https://github.com/muchmala/socket.io-cluster