如何在Client中获取socket.io客户端的session id
我想在我的 socket.io 客户端中获取客户端的会话 ID。
这是我的 socket.io 客户端:
var socket = new io.Socket(config.host, {port: config.port, rememberTransport: false});
// when connected, clear out display
socket.on('connect',function() {
console.log('dummy user connected');
});
socket.on('disconnect',function() {
console.log('disconnected');
});
socket.connect();
return socket;
我想获取该客户端的会话 ID,如何获取?
I want to get session id of client in my socket.io client.
here is my socket.io client :
var socket = new io.Socket(config.host, {port: config.port, rememberTransport: false});
// when connected, clear out display
socket.on('connect',function() {
console.log('dummy user connected');
});
socket.on('disconnect',function() {
console.log('disconnected');
});
socket.connect();
return socket;
I want to get session id of this client , how can i get that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
看看关于这个主题的我的入门书。
更新:
Have a look at my primer on exactly this topic.
UPDATE:
在 socket.io >=1.0 上,连接事件触发后:
On socket.io >=1.0, after the connect event has triggered:
我刚刚遇到了同样的问题,并像这样解决了它(仅客户端代码):
I just had the same problem/question and solved it like this (only client code):
* 请注意:从 v0.9 开始,
set
和get
API 已被弃用 *以下代码只能用于版本 >socket.io < 0.9
参见:http://socket.io/docs/迁移自 0-9/
可以通过握手/授权机制来完成。
现在可以通过 socket.io 连接对象的握手属性访问分配给数据对象的所有属性。
* Please Note: as of v0.9 the
set
andget
API has been deprecated *The following code should only be used for version socket.io < 0.9
See: http://socket.io/docs/migrating-from-0-9/
It can be done through the handshake/authorization mechanism.
All the attributes, that are assigned to the data object are now accessible through the handshake attribute of the socket.io connection object.
在服务器端
在客户端
如果
socket.id
不起作用,请确保在on('connect')
中或连接后调用它。On Server side
On Client side
If
socket.id
, doesn't work, make sure you call it inon('connect')
or after the connection.由于某种原因
不适合我。不过
确实对我有用。希望这对那些在获取 ID 时遇到问题的人有所帮助。顺便说一句,我使用 Socket IO >= 1.0。
For some reason
did not work for me. However
did work for me. Hopefully this is helpful for people who also had issues with getting the id. I use Socket IO >= 1.0, by the way.
从你的代码中尝试
套接字.socket.sessionid
IE。
Try from your code
socket.socket.sessionid
ie.
试试这个方法。
Try this way.