Socket.io 自定义客户端 ID
我正在使用 socket.io 制作一个聊天应用程序,并且我想使用我的自定义客户端 ID,而不是默认的客户端 ID(8411473621394412707
、1120516437992682114
)。有没有什么方法可以在连接时发送自定义标识符,或者只是使用某些东西来跟踪每个 ID 的自定义名称?谢谢!
I'm making a chat app with socket.io, and I'd like to use my custom client id, instead of the default ones (8411473621394412707
, 1120516437992682114
). Is there any ways of sending the custom identifier when connecting or just using something to track a custom name for each ID? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以在服务器上创建一个数组,并在其上存储自定义对象。例如,您可以存储 Socket.io 创建的 id 以及每个客户端发送到服务器的自定义 ID:
在此示例中,您需要从每个客户端调用 storeClientInfo。
希望这有帮助。
You can create an array on the server, and store custom objects on it. For example, you could store the id created by Socket.io and a custom ID sent by each client to the server:
in this example, you need to call storeClientInfo from each client.
Hope this helps.
要设置自定义套接字 ID,generateId 函数必须被覆盖。
Socket.io
server 对象的eio
和engine
属性都可以用于管理此操作。举个简单的例子:
好像已经处理好了。
必须记住,考虑到安全性和应用程序操作,套接字 ID 必须是不可预测且具有唯一值!
额外:如果由于
generateId
方法上的密集处理而导致socket.id
返回为undefined
,async/await
组合可用于解决node.js
7.6.0 及更高版本上的此问题。node_modules/engine.io/lib/server.js 文件的
handshake
方法应更改如下:当前:
新:
注意:位于Engine.io v4。 0,
generateId
方法将接受回调。所以不需要改变握手
方法。只需替换generateId
方法就足够了。例如:To set custom socket id, generateId function must be overwritten. Both of
eio
andengine
props ofSocket.io
server object can be used for to manage this operation.A simple example:
It seems to be it has been handled.
It must be in mind that socket id must be unpredictable and unique value with considering security and the app operations!
Extra: If
socket.id
is returned asundefined
because of your intense processes on yourgenerateId
method,async/await
combination can be used to overcome this issue onnode.js
version 7.6.0 and later.handshake
method ofnode_modules/engine.io/lib/server.js
file should be changed as following:current:
new:
Note: At Engine.io v4.0,
generateId
method would accept a callback. So it would not needed to changehandshake
method. OnlygenerateId
method replacement is going to be enough. For instance:在最新的socket.io(版本1.x)中你可以这样做
In the newest socket.io (version 1.x) you can do something like this
我会使用一个对象作为哈希查找 - 这将节省您循环遍历数组
I would use an object as a hash lookup - this will save you looping through an array
不要将套接字 ID 更改为您自己选择的 ID,这会完全破坏 Socket.io 房间系统。它会默默地失败,您将不知道为什么您的客户没有收到消息。
Do not change the socket IDs to ones of your own choosing, it breaks the Socket.io room system entirely. It will fail silently and you'll have no clue why your clients aren't receiving the messages.
这适用于 2.2.0 及以上版本的 Socket.IO
要设置自定义 Socket Id,必须覆盖
generateId
函数。一个简单的示例:
服务器端
客户端端
注意:请记住,套接字 ID 必须是唯一值。
This will work with 2.2.0 and above version of Socket.IO
To set custom Socket Id,
generateId
function must be overwritten.A simple example:
Server Side
Clint Side
NOTE: Keep in mind that socket id must be a unique value.
为什么不是一个更简单的解决方案,不需要维护连接的客户端数组,并且不覆盖内部套接字 ID?
客户端
why not a simpler solution that does not need to maintain an array of connected clients and does not override internal socket id?
Client side
或者您可以覆盖套接字 ID,如下所示:
您可以在数组下看到:
io.sockets.sockets
or you can override the socket id, like this:
you can see under the array:
io.sockets.sockets
可以以对象格式存储customId(例如userId)而不是for循环,这将提高连接、断开连接和检索socketId以发出
`期间的性能
Can store customId (example userId) in object format instead of for loop, this will improve performance during connection, disconnect and retrieving socketId for emitting
`
使用 Socket.IO 2.2.0 版本,您可以实现这一目标。
With this 2.2.0 version of Socket.IO, you can achieve this.
如果您尝试使用自定义 ID 来与特定客户端进行通信,那么您可以这样做
If you are trying to use a custom id to in order to communicate with a specific client then you can do this