javascript Socket.io:随时获取客户端 sessionID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19838241/
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
Socket.io : Get Client sessionID at any point
提问by Romain Braun
I know how to retrieve the client sessionID when the user connects.
我知道如何在用户连接时检索客户端 sessionID。
But I'd like to retrieve it at any time, for example when the client clicks on something, I would like to be able to know who clicked, what their sessionID was.
但我想随时检索它,例如当客户点击某物时,我希望能够知道谁点击了,他们的 sessionID 是什么。
socket.sessionID
doesn't work, nor does socket.handshake.sessionID
socket.sessionID
不起作用,也不起作用 socket.handshake.sessionID
For example :
例如 :
I have this express route :
我有这条快速路线:
.get('/result/:survey', function(req, res) {
res.redirect('/result/'+req.params.survey+'/1');
})
Just before the redirection, I'd like to make the user join a socket room, and also get their sessionID. How could I possibly do that ? According to the doc, it would be socket.join('room')
but I doubt socket
only represents the connection I have with the client, and not with the other clients. Maybe I'm just having trouble understanding sockets !
就在重定向之前,我想让用户加入一个套接字房间,并获取他们的 sessionID。我怎么可能这样做?根据文档,这将是socket.join('room')
但我怀疑socket
仅代表我与客户端的连接,而不是与其他客户端的连接。也许我只是在理解套接字时遇到了麻烦!
回答by JanS
As of version 1.0, you get the client's sessionid this way:
从 1.0 版开始,您可以通过以下方式获得客户端的 sessionid:
var socket = io();
socket.on('connect', function(){
var sessionid = socket.io.engine.id;
...
});
回答by robertklep
I'm not sure exactly what you mean, because "when the client clicks on something" assumes that you mean 'client-side' (where you can use socket.socket.sessionid
) but "store it in an array of clients" assumes you mean 'server-side' (where you can access it as socket.id
).
我不确定你的意思,因为“当客户端点击某物时”假设你的意思是“客户端”(你可以使用的地方socket.socket.sessionid
)但“将它存储在一个客户端数组中”假设你的意思是“服务器- side'(您可以在其中访问它socket.id
)。
Client-side:
客户端:
var socket = io.connect();
socket.on('connect', function() {
var sessionid = socket.socket.sessionid;
...
});
Server-side:
服务器端:
io.sockets.on('connection', function(socket) {
var sessionid = socket.id;
...
});
回答by Luke W
I store client id's in server side event handlers simply with:
我将客户端 ID 简单地存储在服务器端事件处理程序中:
// server.js
io.sockets.on('connection', function (client) {
client.on('event_name', function (_) {
var clientId = this.id;
}
}
回答by Alex Henry
I'm using socket.io version 2.0.4 and you can access the client id at any point in time by using this.id
in a handler. For example:
我使用的是 socket.io 2.0.4 版,您可以在任何时间点通过this.id
在处理程序中使用来访问客户端 ID 。例如:
socket.on('myevent',myhandler);
function myhandler(){
console.log('current client id: '+this.id);
}
Apparently the handler function is executed in the Socket context itself, which similarly will give you access to other self-properties like client
(connected Client object), request
(current Request object being processed), etc. You can see the full API here https://socket.io/docs/server-api/#socket
显然,处理程序函数是在 Socket 上下文本身中执行的,这同样可以让您访问其他自属性,如client
(连接的客户端对象)、request
(正在处理的当前请求对象)等。您可以在此处查看完整的 API https:/ /socket.io/docs/server-api/#socket