mirror of https://github.com/libp2p/go-libp2p.git
Steven Allen
4 years ago
3 changed files with 0 additions and 127 deletions
@ -1,6 +0,0 @@ |
|||
# 04 Protocol |
|||
|
|||
Be sure to check out these modules: |
|||
|
|||
- https://godoc.org/github.com/libp2p/go-libp2p |
|||
- https://godoc.org/github.com/libp2p/go-libp2p-core/host#Host |
@ -1,76 +0,0 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
|
|||
"github.com/libp2p/go-libp2p" |
|||
"github.com/libp2p/go-libp2p-core/peer" |
|||
mplex "github.com/libp2p/go-libp2p-mplex" |
|||
secio "github.com/libp2p/go-libp2p-secio" |
|||
yamux "github.com/libp2p/go-libp2p-yamux" |
|||
tcp "github.com/libp2p/go-tcp-transport" |
|||
ws "github.com/libp2p/go-ws-transport" |
|||
"github.com/multiformats/go-multiaddr" |
|||
) |
|||
|
|||
func main() { |
|||
ctx, cancel := context.WithCancel(context.Background()) |
|||
defer cancel() |
|||
|
|||
transports := libp2p.ChainOptions( |
|||
libp2p.Transport(tcp.NewTCPTransport), |
|||
libp2p.Transport(ws.New), |
|||
) |
|||
|
|||
muxers := libp2p.ChainOptions( |
|||
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport), |
|||
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport), |
|||
) |
|||
|
|||
security := libp2p.Security(secio.ID, secio.New) |
|||
|
|||
listenAddrs := libp2p.ListenAddrStrings( |
|||
"/ip4/0.0.0.0/tcp/0", |
|||
"/ip4/0.0.0.0/tcp/0/ws", |
|||
) |
|||
|
|||
host, err := libp2p.New( |
|||
ctx, |
|||
transports, |
|||
listenAddrs, |
|||
muxers, |
|||
security, |
|||
) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
// TODO: Register a stream handler for our protocol!
|
|||
|
|||
for _, addr := range host.Addrs() { |
|||
fmt.Println("Listening on", addr) |
|||
} |
|||
|
|||
targetAddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/63785/p2p/QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d") |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
targetInfo, err := peer.AddrInfoFromP2pAddr(targetAddr) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
err = host.Connect(ctx, *targetInfo) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
fmt.Println("Connected to", targetInfo.ID) |
|||
|
|||
// TODO: Implement a simple read loop to read user input and send messages
|
|||
// to peers we are connected to that support our protocol.
|
|||
|
|||
host.Close() |
|||
} |
@ -1,45 +0,0 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"bufio" |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
|
|||
"github.com/libp2p/go-libp2p-core/network" |
|||
) |
|||
|
|||
const protocol = "/libp2p/chat/1.0.0" |
|||
|
|||
func chatHandler(s network.Stream) { |
|||
var ( |
|||
err error |
|||
chunk []byte |
|||
line string |
|||
) |
|||
r := bufio.NewReader(s) |
|||
for { |
|||
prefix := true |
|||
for prefix { |
|||
chunk, prefix, err = r.ReadLine() |
|||
switch err { |
|||
case io.EOF: |
|||
return |
|||
default: |
|||
fmt.Fprintln(os.Stderr, err) |
|||
} |
|||
line += string(chunk) |
|||
} |
|||
fmt.Println(line) |
|||
line = "" |
|||
} |
|||
} |
|||
|
|||
func send(msg string, s network.Stream) error { |
|||
w := bufio.NewWriter(s) |
|||
n, err := w.WriteString(msg) |
|||
if n != len(msg) { |
|||
return fmt.Errorf("expected to write %d bytes, wrote %d", len(msg), n) |
|||
} |
|||
return err |
|||
} |
Loading…
Reference in new issue