admin管理员组文章数量:1416079
Hi I want to create unique integer id for each client connected to the websocket server.
The logic is like this:
- when a client connects to the server, it receives an id which is uint16.
- The id needs to be unique so if it gets an ID that is used by an existing client a new one will be assigned to the client.
- When the client leaves the websocket server the ID is recycled and is ready for use by ining clients.
- At any time there will be no more than 1000 concurrent clients, but new clients can connect at any time. I mean at any second 24/7.
What I have tried:
let i = Date.now();
i &= 0xFFFF; // i will be the unique id.
In this way a connected client will get a 16 bit integer id. When it leaves the server there is some logic to recycle it, maybe put it into a cached array. But since there are 65535 possibilities I may not need to use that cached array.
I know that websocket connection will generate a socket id automatically once connected, but that is more than 20 bytes which is not what I want.
Caveat:
This method is based on the assumption that there is only one client trying to connect to the websocket server at any given time (precision is millisecond).
If two or more clients try to connect at the same time. This doesn't work. But I guess this also depends on the cpu clock rate or precisely how often a websocket server allows a new client to connect.
Hi I want to create unique integer id for each client connected to the websocket server.
The logic is like this:
- when a client connects to the server, it receives an id which is uint16.
- The id needs to be unique so if it gets an ID that is used by an existing client a new one will be assigned to the client.
- When the client leaves the websocket server the ID is recycled and is ready for use by ining clients.
- At any time there will be no more than 1000 concurrent clients, but new clients can connect at any time. I mean at any second 24/7.
What I have tried:
let i = Date.now();
i &= 0xFFFF; // i will be the unique id.
In this way a connected client will get a 16 bit integer id. When it leaves the server there is some logic to recycle it, maybe put it into a cached array. But since there are 65535 possibilities I may not need to use that cached array.
I know that websocket connection will generate a socket id automatically once connected, but that is more than 20 bytes which is not what I want.
Caveat:
This method is based on the assumption that there is only one client trying to connect to the websocket server at any given time (precision is millisecond).
If two or more clients try to connect at the same time. This doesn't work. But I guess this also depends on the cpu clock rate or precisely how often a websocket server allows a new client to connect.
Share Improve this question edited May 24, 2017 at 3:17 newguy asked May 24, 2017 at 3:13 newguynewguy 5,98612 gold badges62 silver badges102 bronze badges 10- @JaromandaX I am generating the id on the websocket server. – newguy Commented May 24, 2017 at 3:17
- oh, sorry, I misunderstood the problem - why not just store the current ID's somewhere (db, an array, whatever, the implementation is up to you) and check that the generated ID is unique by simply checking which ones are already in use - use Math.random to generate the ID in the first place – Jaromanda X Commented May 24, 2017 at 3:20
-
@JaromandaX The thing is if multiple clients try to connect and all of them get an ID that is already being used there will be a lot of
Math.random
calls. I am not sure about the possibilities but this could happen. And I am not sure if there would be race condition or not. Say If A callsMath.random
and gets an existing ID now B kicks in and also asks for an ID, which gets assigned, then A receives a new ID which is being used by B. Again A needs to get a new ID right? If before it finished theMath.random
call C es in and the same thing happens again, A could never get an ID. – newguy Commented May 24, 2017 at 3:42 - What do you think of using a Session ID? The Session ID must be a unique identifier for each server-client connection. I have had a similar scenario, and this solution has worked for me. I use Java, but I'm sure it does not matter. – dim Commented May 24, 2017 at 8:47
-
@dim What is a session ID in websocket protocol? I only know that when the socket connects it is automatically assigned a base-64 encoded value, which is stored in the
Sec-WebSocket-Key
header. When decoded it is 16 bytes. – newguy Commented May 24, 2017 at 8:54
1 Answer
Reset to default 4Give up the idea of using a uint16 and switch to a UUID. https://en.wikipedia/wiki/Universally_unique_identifier
These algorithms are designed for this exact type of use case. They're well-tested and standardized.
With this method, you don't have to re-use IDs. This is particularly handy for logging so that you can always determine what happened with a particular connection. Additionally, it's unique across servers, so if you ever do scale up, you don't have to worry about collisions.
https://www.npmjs./package/uuid
本文标签: javascriptGenerating unique ID for client connected to a websocket serverStack Overflow
版权声明:本文标题:javascript - Generating unique ID for client connected to a websocket server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244335a2649487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论