簡體   English   中英

如何在Client中獲取socket.io客戶端的會話ID

[英]how to get session id of socket.io client in Client

我想在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,我怎么能得到它?

請仔細閱讀我的入門知識

更新:

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');
  });
});

在socket.io> = 1.0上,在觸發connect事件后:

var socket = io('localhost');
var id = socket.io.engine.id

*請注意:從v0.9開始,不推薦使用setget API *

以下代碼僅應用於版本socket.io <0.9
請參閱http//socket.io/docs/migrating-from-0-9/



它可以通過握手/授權機制完成。

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);
});

現在可以通過socket.io連接對象的handshake屬性訪問分配給數據對象的所有屬性。

io.sockets.on('connection', function (socket) {
    console.log('sessionID ' + socket.handshake.sessionID);
});

我只是遇到了同樣的問題/問題並且像這樣解決了它(只有客戶端代碼):

var io = io.connect('localhost');

io.on('connect', function () {
    console.log(this.socket.sessionid);
});

由於某些原因

socket.on('connect', function() {
    console.log(socket.io.engine.id);
}); 

不適合我。 然而

socket.on('connect', function() {
    console.log(io().id);
}); 

對我有用。 希望這對於那些在獲取身份證方面也存在問題的人也很有幫助。 順便說一句,我使用Socket IO> = 1.0。

嘗試從你的代碼socket.socket.sessionid ie。

    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);});

試試這種方式。

                var socket = io.connect('http://...');
                console.log(socket.Socket.sessionid);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM