Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript socket.io和socket.io-redis:获取房间中的所有套接字对象_Javascript_Node.js_Websocket_Socket.io_Socket.io Redis - Fatal编程技术网

Javascript socket.io和socket.io-redis:获取房间中的所有套接字对象

Javascript socket.io和socket.io-redis:获取房间中的所有套接字对象,javascript,node.js,websocket,socket.io,socket.io-redis,Javascript,Node.js,Websocket,Socket.io,Socket.io Redis,我想检索房间中连接的服务器端所有插座 我发现方法clients如果在中的方法之后链接,则返回房间中连接的所有插座: import * as express from 'express'; import * as SocketIO from 'socket.io'; import * as redisSocket from 'socket.io-redis'; import * as sharedsession from 'express-socket.io-session'; const ap

我想检索房间中连接的服务器端所有插座

我发现方法
clients
如果在
中的方法
之后链接,则返回房间中连接的所有插座:

import * as express from 'express';
import * as SocketIO from 'socket.io';
import * as redisSocket from 'socket.io-redis';
import * as sharedsession from 'express-socket.io-session';

const app = express();

const redisOption = {port: 6379, host: 'XXX'};

// use the RedisStore as express session
const session = session({
  name: 'SESSIONID',
  store: new RedisStore(redisOption),
  secret: 'Ashg8hV77bxFeyLVec',
  resave: false,
  saveUninitialized: false
});
app.use(session);

// start express server
const httpServer = app.listen(80);

// attach the express app to socket.io server
const socketServer = SocketIO(httpServer, { path: '/api/socket.io', origins: '*:*' });

// set redis as socket adpter for multi instance/nodes
socketServer.adapter(redisSocket(redisOption));

// share the express session with socket.io
socketServer.use(sharedsession(session, {autoSave: true}));

// get all client in a room
socketServer.in(room_id).clients((err, clients) => {
    for (let i = 0, e = clients.length; i < e; i++) {
        const client = clients[i];
        console.log(client); // print the socket id

        // HOW RETRIVE THE SOCKET OBJECT???
    }
});
从“express”导入*为express;
从“socket.io”导入*作为SocketIO;
从“socket.io redis”导入*作为redisocket;
从“express socket.io session”导入*作为sharedsession;
常量app=express();
const redisOption={端口:6379,主机:'XXX'};
//将Redistore用作express会话
const session=会话({
名称:'SESSIONID',
商店:新的RedisStore(redisOption),
秘密:“Ashg8hV77bxFeyLVec”,
resave:false,
saveUninitialized:false
});
应用程序使用(会话);
//启动express服务器
const httpServer=app.listen(80);
//将express应用程序连接到socket.io服务器
const socketServer=SocketIO(httpServer,{path:'/api/socket.io',origins:'*:*'});
//将redis设置为多实例/节点的套接字适配器
适配器(redisSocket(redisOption));
//与socket.io共享express会话
use(sharedsession(session,{autoSave:true}));
//把所有客户都安排在一个房间里
socketServer.in(房间id).clients((错误,客户端)=>{
for(设i=0,e=clients.length;i
但我需要检索所有套接字会话/握手

是否有一种方法可以检索所有套接字会话/握手

旁注:socket服务器是带有socket.io redis的多实例/节点

  • socket.io:2.3.0
  • socket.io-redis:5.2.0

    • 对象键(io.sockets.sockets);它提供房间中所有连接的套接字。

      我不确定下面的代码是否有效,但我认为通过使用socket.io-redis提供的customHook,我们可以获得基于redis的多节点socket.handshake.session

      我希望下面的代码有帮助

      // get all client in a room
      socketServer.in(room_id).clients((err, clients) => {
          for (let i = 0, e = clients.length; i < e; i++) {
              const client = clients[i];
              console.log(client); // print the socket id
      
              // HOW RETRIVE THE SOCKET OBJECT???
          }
      });
      
      // set root namespace
      const rootNamespace = socketServer.of('/');
      
      // define customHook
      rootNamespace.adapter.customHook = (request, cb) => {
          // every socket.io server execute below, when customRequest requested
          const type = request.type;
          if(type === 'getHandShakeSession'){
              // get all socket objects on local socket.io server
              const sockets = rootNamespace.connected;
              const socketIDs = Object.keys(sockets);
              // get all socket.handshak.session array on local socket.io server
              const sessions = socketIDs.map(socketID => sockets[socketID].handshake.session);
              cb(sessions)
          }
          cb()
      }
      
      // request customRequest
      rootNamespace.adapter.customRequest({type:'getHandShakeSession'},(err,replies) => {
          //replies are array which element was pushed by cb(element) on individual socket.io server
          //remove empty reply 
          const filtered = replies.filter(reply => reply !== undefined)
          // filtered seems like [[session1,session2,...],[sesssion3,session4,...],..]
          console.log(filtered)
      } )
      
      //在一个房间中获取所有客户端
      socketServer.in(房间id).clients((错误,客户端)=>{
      for(设i=0,e=clients.length;i{
      //当customRequest请求时,每个socket.io服务器都会执行以下命令
      const type=request.type;
      如果(类型=='getHandShakeSession'){
      //获取本地socket.io服务器上的所有套接字对象
      const sockets=rootNamespace.connected;
      const socketIDs=Object.key(套接字);
      //获取本地socket.io服务器上的所有socket.handshak.session数组
      const sessions=socketIDs.map(socketID=>sockets[socketID].handshake.session);
      cb(会议)
      }
      cb()
      }
      //请求定制请求
      rootNamespace.adapter.customRequest({type:'getHandShakeSession'},(错误,回复)=>{
      //回复是由单个socket.io服务器上的cb(元素)推送的数组
      //删除空回复
      const filtered=回复。过滤器(回复=>回复!==未定义)
      //过滤后的内容类似于[[session1,session2,…],[Session3,session4,…],]
      console.log(已过滤)
      } )
      
      >此链接对您有用吗?此链接仅适用于当前节点,问题是适用于多节点。