javascript 如何确保通过 socket.io 的消息已被客户端收到?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11382495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:59:44  来源:igfitidea点击:

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

javascriptnode.jswebsocketsocket.io

提问by Bart

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

如何检查使用socket.io库发送的消息是否已收到客户端。socket.io 有专门的方法吗?

Thanks for your answers!

感谢您的回答!

回答by almypal

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.

现在检查服务器端的控制台。它应该显示“is_it_ok”。接下来检查客户端的控制台。它应该显示“确定”。这就是确认信息。

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.io 连接本质上是持久的。以下内置函数可让您根据连接状态采取行动。

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'.

所以你不需要使用计时器。回调会立即运行,除非连接具有以下任何状态 - 'disconnect'、'reconnecting'、'reconnect_failed'。

回答by Mirko

You can use the socket.io's acknowledgements.

您可以使用 socket.io 的确认。

Quote from the socket.io documentation:

引用 socket.io 文档:

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 .sendor .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along.

有时,您可能希望在客户端确认消息接收时获得回调。

为此,只需将函数作为.sendor 的最后一个参数传递.emit。更重要的是,当您使用 时.emit,确认由您完成,这意味着您还可以传递数据。

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

回答by hasanyasin

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.

当你添加一个函数作为.send()or.emit()方法调用的最后一个参数时,当对方收到消息时调用这个函数。

socket.send('hi', function() {
    // if we are here, our salutation has been received by the other party.
});