Javascript 如何在客户端获取socket.io客户端的会话ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6979992/
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
how to get session id of socket.io client in Client
提问by XMen
I want to get session id of client in my socket.io client.
我想在我的 socket.io 客户端中获取客户端的会话 ID。
here is my socket.io client :
这是我的 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;
I want to get session id of this client , how can i get that ?
我想获得这个客户的会话 ID,我怎样才能得到它?
回答by Daniel Baulig
Have a look at my primeron exactly this topic.
UPDATE:
更新:
var sio = require('socket.io'),
app = require('express').createServer();
app.listen(8080);
sio = sio.listen(app);
sio.on('connection', function (client) {
console.log('client connected');
// send the clients id to the client itself.
client.send(client.id);
client.on('disconnect', function () {
console.log('client disconnected');
});
});
回答by bluehallu
On socket.io >=1.0, after the connect event has triggered:
在 socket.io >=1.0 上,connect 事件触发后:
var socket = io('localhost');
var id = socket.io.engine.id
回答by sreekumar
* Please Note: as of v0.9 the set
and get
API has been deprecated *
* 请注意:从 v0.9 开始,set
和get
API 已被弃用 *
The following code should only be used for version socket.io < 0.9
See: http://socket.io/docs/migrating-from-0-9/
以下代码仅适用于socket.io < 0.9版本
请参见:http: //socket.io/docs/migrating-from-0-9/
It can be done through the handshake/authorization mechanism.
它可以通过握手/授权机制来完成。
var cookie = require('cookie');
io.set('authorization', function (data, accept) {
// check if there's a cookie header
if (data.headers.cookie) {
// if there is, parse the cookie
data.cookie = cookie.parse(data.headers.cookie);
// note that you will need to use the same key to grad the
// session id, as you specified in the Express setup.
data.sessionID = data.cookie['express.sid'];
} else {
// if there isn't, turn down the connection with a message
// and leave the function.
return accept('No cookie transmitted.', false);
}
// accept the incoming connection
accept(null, true);
});
All the attributes, that are assigned to the data object are now accessible through the handshake attribute of the socket.io connection object.
现在可以通过 socket.io 连接对象的握手属性访问分配给数据对象的所有属性。
io.sockets.on('connection', function (socket) {
console.log('sessionID ' + socket.handshake.sessionID);
});
回答by luschn
I just had the same problem/question and solved it like this (only client code):
我刚刚遇到了同样的问题/问题并像这样解决了它(只有客户端代码):
var io = io.connect('localhost');
io.on('connect', function () {
console.log(this.socket.sessionid);
});
回答by Jelle Blaauw
For some reason
因为某些原因
socket.on('connect', function() {
console.log(socket.io.engine.id);
});
did not work for me. However
对我不起作用。然而
socket.on('connect', function() {
console.log(io().id);
});
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.
确实为我工作。希望这对在获取 id 方面也有问题的人有所帮助。顺便说一下,我使用 Socket IO >= 1.0。
回答by Shubham Kushwah
On Server side
在服务器端
io.on('connection', socket => {
console.log(socket.id)
})
On Client side
在客户端
import io from 'socket.io-client';
socket = io.connect('http://localhost:5000');
socket.on('connect', () => {
console.log(socket.id, socket.io.engine.id, socket.json.id)
})
If socket.id
, doesn't work, make sure you call it in on('connect')
or after the connection.
如果socket.id
, 不起作用,请确保on('connect')
在连接中或连接后调用它。
回答by CrIceIs
Try from your code socket.socket.sessionid ie.
从你的代码 socket.socket.sessionid 即尝试。
var socket = io.connect('http://localhost');
alert(socket.socket.sessionid);
var sendBtn= document.getElementById('btnSend');
sendBtn.onclick= function(){
var userId=document.getElementById('txt1').value;
var userMsg = document.getElementById('txt2').value;
socket.emit('sendto',{username: userId, message: userMsg});
};
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
socket.on('message',function(data){ console.log(data);});
回答by Patrik Patux
Try this way.
试试这个方法。
var socket = io.connect('http://...');
console.log(socket.Socket.sessionid);