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

后端 未结 8 870
谎友^
谎友^ 2020-12-07 15:44

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,          


        
相关标签:
8条回答
  • 2020-12-07 16:11

    * Please Note: as of v0.9 the set and get 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.

    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.

    io.sockets.on('connection', function (socket) {
        console.log('sessionID ' + socket.handshake.sessionID);
    });
    
    0 讨论(0)
  • 2020-12-07 16:12

    Try from your code 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);});
    
    0 讨论(0)
  • 2020-12-07 16:15

    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.

    0 讨论(0)
  • 2020-12-07 16:19

    Have a look at my primer on 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');
      });
    });
    
    0 讨论(0)
  • 2020-12-07 16:19

    Try this way.

                    var socket = io.connect('http://...');
                    console.log(socket.Socket.sessionid);
    
    0 讨论(0)
  • 2020-12-07 16:21

    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);
    });
    
    0 讨论(0)
提交回复
热议问题