How to be sure that message via socket.io has been received to the client?

后端 未结 3 1214
情深已故
情深已故 2020-12-24 08:03

How to check that message sent with socket.io library has been received to the client. Is there special method for it in socket.io?

Thanks for you

相关标签:
3条回答
  • 2020-12-24 08:20

    You should use the callback parameter while defining the event handler.

    A typical implementation would be as follows:

    Client side

    var socket = io.connect('http://localhost');
    socket.emit('set', 'is_it_ok', function (response) {
        console.log(response);
    });
    

    Server side

    io.sockets.on('connection', function (socket) {
        socket.on('set', function (status, callback) {
            console.log(status);
            callback('ok');
        });
    });
    

    Now check the console on the server side. It should display 'is_it_ok'. Next check console on client side. It should display 'ok'. That's the confirmation message.

    Update

    A socket.io connection is essentially persistent. The following in-built functions let you take action based on the state of the connection.

    socket.on('disconnect', function() {} ); // wait for reconnect
    socket.on('reconnect', function() {} ); // connection restored  
    socket.on('reconnecting', function(nextRetry) {} ); //trying to reconnect
    socket.on('reconnect_failed', function() { console.log("Reconnect failed"); });
    

    Using the callback option shown above is effectively a combination of the following two steps:

    socket.emit('callback', 'ok') // happens immediately
    

    and on the client side

    socket.on('callback', function(data) {
        console.log(data);
    });
    

    So you don't need to use a timer. The callback runs immediately except if the connection has any of the following states - 'disconnect', 'reconnecting', 'reconnect_failed'.

    0 讨论(0)
  • 2020-12-24 08:22

    You can use the socket.io's acknowledgements.

    Quote from the socket.io documentation:

    Sometimes, you might want to get a callback when the client confirmed the message reception.

    To do this, simply pass a function as the last parameter of .send or .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along.

    On the client side simply emit the event with your data, the function will be called whenever the server responds to your event:

    client.emit("someEvent", {property:value}, function (data) {
      if (data.error) 
        console.log('Something went wrong on the server');
    
      if (data.ok)
        console.log('Event was processed successfully');
    });
    

    On the server side you get called with the data and the callback handle to send the response:

    socket.on('someEvent', function (data, callback) {
    
      // do some work with the data
    
      if (err) {
        callback({error:'someErrorCode', msg:'Some message'});
        return;
      }
    
      callback({ok:true});
    });
    
    0 讨论(0)
  • 2020-12-24 08:27

    When you add a function as the last parameter of .send() or .emit() method calls, this function is called when the other party receives the message.

    socket.send('hi', function() {
        // if we are here, our salutation has been received by the other party.
    });
    
    0 讨论(0)
提交回复
热议问题