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

后端 未结 8 871
谎友^
谎友^ 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:22

    On socket.io >=1.0, after the connect event has triggered:

    var socket = io('localhost');
    var id = socket.io.engine.id
    
    0 讨论(0)
  • 2020-12-07 16:27

    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.

    0 讨论(0)
提交回复
热议问题