Send additional data on socket connection

前端 未结 7 513
清酒与你
清酒与你 2020-12-24 10:12

How to best send additional data upon socket connection?

Client:

socket.on(\'connect\',function(){ 
//I\'d like set some values and pass them up to t         


        
7条回答
  •  清酒与你
    2020-12-24 10:41

    I have a different approach - emit an event right after connecting, with the data:

    socket.on('connect',function(){ 
        // Send ehlo event right after connect:
        socket.emit('ehlo', data);
    });
    
    io.on('connection', function(client){
        // Receive ehlo event with data:
        client.on('ehlo', function(data) {
        });
    });
    

    You can hold a variable/object, and say, when there is no ehlo event with data, the other events are ignored until ehlo is sent.

    If this does not satisfy, you can send data right when connecting, I won't copy the code but it is in this answer: https://stackoverflow.com/a/13940399/1948292

提交回复
热议问题