admin管理员组文章数量:1200756
Is there a way for a WebSocket client to send additional information on the initial connection to the WebSocket server?
I'm asking because I want the client to send the user ID (string) to the server immediately. I don't want the client to send the user ID in the onopen callback. Why? Because it's faster and simpler.
If the WebSocket API won't let you do this, why not? If there's no good reason, how could I suggest they add this simple feature?
Is there a way for a WebSocket client to send additional information on the initial connection to the WebSocket server?
I'm asking because I want the client to send the user ID (string) to the server immediately. I don't want the client to send the user ID in the onopen callback. Why? Because it's faster and simpler.
If the WebSocket API won't let you do this, why not? If there's no good reason, how could I suggest they add this simple feature?
Share Improve this question edited Oct 19, 2013 at 2:07 ma11hew28 asked Oct 19, 2013 at 2:02 ma11hew28ma11hew28 126k122 gold badges460 silver badges659 bronze badges 1- 15 it is a url, you have the whole queryString for your use... – dandavis Commented Oct 19, 2013 at 2:24
2 Answers
Reset to default 16Updated Answer
@dandavis is a genius. His comment on the question of sending the user ID in the query string of the first (url) argument of the WebSocket constructor works! And, I'm pretty sure it's only sent once by the client during the opening handshake (1.3) of the WebSocket protocol (RFC 6455).
It even worked to send it in the path, which I prefer for now since the WebSocket server I made is just for WebSockets. I'm using Node.js with ws. I'm connecting to the URL ws://localhost:5000/4
, where 4
is the user ID. To get the user ID, I do ws.upgradeReq.url
, like so:
wss.on('connection', function(ws) {
console.log(ws.upgradeReq.url); # => '/4'
});
Next, to make it secure, I'll pass the access token instead of the user ID.
Original Answer
You can pass the user ID (string) as the second (protocols) argument of the WebSocket constructor.
That should work, but it's not meant for what you want. It's definitely a hack. So, I don't recommend it. Also, the server will echo the user ID back to the client in its handshake. And, you don't need that. That would be extra data being sent over the wire that's of no use to you.
I'm not sure why the WebSocket API doesn't let you do what you want. Maybe it's more secure that way.
Since v3 ws.upgradeReq
is deprecated and doesn't work anymore.
You can now use:
wss.on('connection', function(ws, req) {
console.log(req.url); # => '/4'
});
Source: https://github.com/websockets/ws#client-authentication
本文标签: javascriptWebSocket send extra information on connectionStack Overflow
版权声明:本文标题:javascript - WebSocket send extra information on connection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738572103a2100657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论