简体   繁体   English

NodeJs Socket.io房间

[英]NodeJs Socket.io Rooms

This is a pretty simple question but i want to make sure that i am scaling our socket.io implementation correctly. 这是一个非常简单的问题,但我想确保我正确缩放了socket.io实现。 We are using socket.io to respond back to the client after a lengthy process on the nodejs backend. 经过nodejs后端的冗长过程后,我们正在使用socket.io来响应客户端。 So basically client makes call, then socket.io signals the client that the process has completed. 因此,基本上客户进行了呼叫,然后socket.io向客户发出信号,通知该过程已完成。 Also socket.io ONLY responds to a temporary room that was established for the request. 同样,socket.io仅响应为该请求建立的临时房间。

In nodejs i created a global variable for the following so that i could emit back to the client room: 在nodejs中,我为以下内容创建了一个全局变量,以便可以发回到客户室:

global.io = require('socket.io')(server);

But to create the room itself I am a little unsure how to create it globally such that only the socket that connected and made the request receives the response. 但是要创建房间本身,我有点不确定如何全局创建它,以便只有连接并发出请求的套接字才能收到响应。

So if i have 500 client machines that initiate a connection through socket.io, each one will have its own socket. 因此,如果我有500台通过socket.io启动连接的客户端计算机,那么每台客户端计算机都将拥有自己的套接字。 To ensure that the rooms are unique i use a guid across all 500. Of course i do not want all sockets to receive traffic if only one socket for a specific room is supposed to be evaluating the emit.... 为了确保房间是唯一的,我在所有500个房间中都使用了GUID。当然,如果特定房间只有一个套接字正在评估发射,我不希望所有套接字都接收流量。

any ideas? 有任何想法吗?

If I understood your question correctly, you're looking to send information to that 1 socket? 如果我正确理解了您的问题,那么您正在寻找向该1个套接字发送信息的方法吗?

Perhaps something like this: 也许是这样的:

socket.broadcast.to(socketId).emit('someeventname', eventData);

If you have the connection open with that client, that means you have their socket id through socket.id . 如果您与该客户端打开了连接,则意味着您可以通过socket.id获得其套接字ID。 You can emit events to just that socket. 您可以向该套接字发出事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM