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 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题