Open tanhuiya opened 5 years ago
I can confirm this behaviour. It doesn't appear that broadcasting to all connected peers is a documented behaviour. It could be if the receiver tracked all received streams, and duplicated messages amongst all peers.
Ok, look, first of all, you should know that Stream
interface is a bidirectional link between two nodes, it's not a multichannel.
Also, you should know, that as far as example have only one stream variable instead of global mapping of streams - it will be owerwritten each time you have a new connection. If you want to communicate with multiple devices - you should have a stream mapping (or array) and append any new stream connection to it.
Then, if you want to implement multicast as your own, you should probably map each stream to some 'topic', and when you want to multicast message to every peer in topic list - you should send message to all streams on that topic.
However, instead of creating this mapping and creating multicast mechanism of your own, you can use PubSub mechanism, which has been created for those purposes.
You can find PubSub here: https://github.com/libp2p/go-libp2p-pubsub
P.S I would appreciate if someone will add PubSub example to this repo.
steps :
go run chat.go
in first windowgo run chat.go -d /ip4/127.0.0.1/tcp/64717/p2p/QmefMVVk1tZzfYshYVUFpnwNswtqgXrD6geWo3PFUzQX1Q
in second window.go run chat.go -d /ip4/127.0.0.1/tcp/64717/p2p/QmefMVVk1tZzfYshYVUFpnwNswtqgXrD6geWo3PFUzQX1Q
in third window.Then ,when I type any message such as
Only the second window receive
aaa
the third window receivebbb
so, how can they receive all messages ?