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

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

问题描述

API 提供了发送/发送回调机制来确认收到的消息.但是,在断开连接或错误的情况下不会触发此回调.在我看来,在断开连接时,需要通过一些相当混乱的程序来清理未完成的已发送消息(例如 - 假设您可能想要存储消息以供稍后转发等的用例).关于如何实现这一点的任何简单想法?想知道我是否遗漏了什么......谢谢.

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.

推荐答案

真正的潜在问题

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

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.

在通过 socket.io 进行通信的任何阶段,链接都可能被破坏,并且可以发送 callback 但对方无法确定它是否到达.

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.

您需要接受这样的事实,这始终是可能的.对此,没有简单的解决方案.这个问题及其泛化仍在多代理系统研究等领域得到积极研究.

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.

有一些常见的方法可以缓解这个问题.

There are some common approaches to mitigate this issue.

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

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.

  • Related question here in SO
  • The Byzantine Generals Problem by Microsoft Research (LESLIE LAMPORT, ROBERT SHOSTAK, and MARSHALL PEASE) which discusses the problem and suggests some solutions
  • Acclaimed Book by Yoav Shoham that talks about Multiagent systems and how they address this issue.
  • Blog post about this problem in TCP.

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

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