在socket.io下可靠的消息传递? [英] Reliable messaging under socket.io?

查看:137
本文介绍了在socket.io下可靠的消息传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API提供了发射/发送回调机制来确认收到的消息.但是,如果断开连接或发生错误,则不会触发此回调.在我看来,断开连接后,您将需要执行一些相当混乱的过程来清理未发送的已发送消息(例如,假设您可能要存储消息以备后用,等等).关于如何完成此操作的任何简单想法?想知道我是否想念一些东西....谢谢.

解决方案

真正的根本问题

此问题不仅限于socket.io.这是一个众所周知的问题,称为两位将军的问题.

每支由将军领导的两支军队正准备进攻一个要塞城市.军队在城市附近安营扎寨,每个人都在自己的山上.一个山谷将两个山丘隔开,两个将军交流的唯一方法是通过山谷派遣使者.不幸的是,该山谷被城市的防御者占领,并且有可能捕获通过该山谷发送的任何特定的使者(这种情况是假设,尽管两位将军已经同意进攻,但他们并没有同意进攻,然后在各自的山丘上占据位置.

您正试图通过不可靠的链接访问常识.

在通过socket.io进行通信的任何阶段,都可以断开链接,并且可以发送callback,但是另一端不能确定它是否到达.

可以做什么

您需要拥抱,这总是有可能的.为此,没有简单的解决方案.这个问题及其泛化仍在诸如多代理系统研究等领域中得到积极研究. /p>

在您的具体情况下仍然可以做什么

有一些缓解此问题的常用方法.

在使用socket.io设计应用程序时,我所做的是将ID附加到消息上,如果发生断开连接并且一方尝试发送已发送的消息,则接收方将知道该消息已被接收.

请注意,实际上,您无需在任何地方都执行此操作.

关于此问题的更多阅读内容

The API provides the emit/send callback mechanism to acknowledge received messages. However, this callback doesn't get fired in case of disconnect or error. It appears to me that upon a disconnection one would need to go through some rather messy procedures to clean up outstanding sent messages (e.g. - assume a use case where you may want to store messages for forwarding later, etc.). Any simple ideas out here on how to accomplish this? Wondering if I'm missing something.... Thanks.

解决方案

The Real Underlying Issue

This issue isn't just limited to socket.io. It is a well known problem called the Two Generals' Problem.

Two armies, each led by a general, are preparing to attack a fortified city. The armies are encamped near the city, each on its own hill. A valley separates the two hills, and the only way for the two generals to communicate is by sending messengers through the valley. Unfortunately, the valley is occupied by the city's defenders and there's a chance that any given messenger sent through the valley will be captured (this scenario assumes that while the two generals have agreed that they will attack, they haven't agreed upon a time for attack before taking up their positions on their respective hills).

You are trying to reach Common Knowledge over an unreliable link.

At any stage of the communication over socket.io the link can be broken, and a callback can be sent but the other side could not be sure that it arrived.

What Can Be Done

You need to embrace the fact this is always a possibility. There is no trivial solution for this. This problem and its generalization are still actively studied in fields like Multi-Agent Systems research.

What can still be done in your specific case

There are some common approaches to mitigate this issue.

What I did when designing an application using socket.io is attach IDs to messages, if a disconnect happens and one side tried to send an already-sent message, the receiving side will be aware that the message was already received.

Note that in practice you don't need to do this everywhere.

More Reading on the Issue

这篇关于在socket.io下可靠的消息传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆