doubeizhong5178 2019-04-10 13:55
浏览 602
已采纳

如何使用golang libp2p流将数据发送到多个主机?

my question refers to the libp2p library in golang: https://github.com/libp2p/go-libp2p

This video explains the background: https://www.youtube.com/watch?v=hP0hSZ7E7_Y

The peers in the network communicate via buffered streams rw. For each new connecting peer there is a new stream created that connects him to an existing peer using addresses. This means there are multiple streams but not all peers are directly connected with each other. Using those streams, the peers can read and write data from it via

rw := bufio.NewReadWriter(bufio.NewReader(stream),bufio.NewWriter(stream))

by writing messages `rw.WriteString("message"),rw.Flush()``

and reading this messages via message := rw.ReadString(rw).

As the peers are not all connected, they only receive messages from directly connected peers and not even all of them if there are multiple connections.

Exactly this problem is mentioned in the video, around time 09:45. The author of the video says that this could be easily modified to send messages not only to directly connected peers but also to multiple hosts. But how can this be done?

My goal is to send message from one peer and that all other peers in the network, also the not directly connected ones, can receive (and answer) it.

  • 写回答

1条回答 默认 最新

  • drxzpo70788179614 2019-07-31 12:40
    关注

    The best way to send multicast or broadcast messages in libp2p is to use PubSub, So every node subscribed to a topic will be notified about sent messages despite not being directly connected to sender node.

    You can find an example implementation using Pubsub here. It uses gossibSub implementation from libp2p.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀