代码下载
git clone https://github.com/libp2p/go-libp2p.git
chat实例分析
进入目录go-libp2p/examples/chat/, 编译 go build
先看README.md, 可以了解到使用chat的方式
- 在节点 B
$ ./chat -sp 3001
2021/10/28 11:01:25 Run './chat -d /ip4/127.0.0.1/tcp/3001/p2p/QmXgEXt4CiqmVmDhPPQnyVkoj3Bsx3Nggq57PPsk5tLNTs' on another console.
2021/10/28 11:01:25 You can replace 127.0.0.1 with public IP as well.
2021/10/28 11:01:25 Waiting for incoming connection
2021/10/28 11:01:25
2021/10/28 11:03:03 Got a new stream!
> hello
- 在节点 A, 如果B有公网IP,则把
127.0.0.1替换成B的公网IP
$ ./chat -d /ip4/127.0.0.1/tcp/3001/p2p/QmXgEXt4CiqmVmDhPPQnyVkoj3Bsx3Nggq57PPsk5tLNTs
2021/10/28 11:03:03 This node's multiaddresses:
2021/10/28 11:03:03 - /ip4/172.16.3.205/tcp/53084
2021/10/28 11:03:03 - /ip4/127.0.0.1/tcp/53084
2021/10/28 11:03:03
2021/10/28 11:03:03 Established connection to destination
> hello
作为被连接方B
先看代码:
……
阅读全文