javascript WebSocket 发送有关连接的额外信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19461534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebSocket send extra information on connection
提问by ma11hew28
Is there a way for a WebSocket client to send additional information on the initial connection to the WebSocket server?
WebSocket 客户端是否可以将有关初始连接的附加信息发送到 WebSocket 服务器?
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.
我问是因为我希望客户端立即将用户 ID(字符串)发送到服务器。我不希望客户端在 onopen 回调中发送用户 ID。为什么?因为它更快更简单。
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?
如果 WebSocket API 不允许您这样做,为什么不呢?如果没有充分的理由,我怎么能建议他们添加这个简单的功能?
回答by ma11hew28
Updated 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).
@dandavis 是个天才。他对在 WebSocket 构造函数的第一个 (url) 参数的查询字符串中发送用户 ID 的问题的评论有效!而且,我很确定它在 WebSocket 协议 (RFC 6455)的打开握手 (1.3)期间仅由客户端发送一次。
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.jswith 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:
它甚至可以将它发送到路径中,我现在更喜欢这种方式,因为我制作的 WebSocket 服务器仅适用于 WebSockets。我正在使用Node.js和ws。我正在连接到 URL ws://localhost:5000/4
,4
用户 ID在哪里。要获取用户 ID,我会这样做ws.upgradeReq.url
,如下所示:
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.
接下来,为了确保安全,我将传递访问令牌而不是用户 ID。
Original Answer
原答案
You can pass the user ID (string) as the second (protocols) argument of the WebSocket constructor.
您可以将用户 ID(字符串)作为WebSocket 构造函数的第二个(协议)参数传递。
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.
这应该有效,但它并不适合您想要的。这绝对是一个黑客。所以,我不推荐。此外,服务器将在其握手中将用户 ID 回显给客户端。而且,你不需要那个。那将是通过线路发送的额外数据,而这些数据对您毫无用处。
I'm not sure why the WebSocket API doesn't let you do what you want. Maybe it's more secure that way.
我不确定为什么 WebSocket API 不允许你做你想做的事。也许那样更安全。
回答by Kevin Goedecke
Since v3 ws.upgradeReq
is deprecated and doesn't work anymore.
由于 v3ws.upgradeReq
已被弃用并且不再起作用。
You can now use:
您现在可以使用:
wss.on('connection', function(ws, req) {
console.log(req.url); # => '/4'
});
Source: https://github.com/websockets/ws#client-authentication