Integration: Add Go port

Relates to #5
This commit is contained in:
backkem 2019-02-14 17:37:30 +01:00 committed by Michiel De Backker
parent 40fc80ae24
commit db99ed5c56
2 changed files with 91 additions and 3 deletions

View File

@ -9,7 +9,7 @@ flags.defineBoolean(listenFlag, false, 'Listen for incoming connections.')
flags.parse()
const listening = flags.get(listenFlag)
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const maddr = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const direct = new WebRTCDirect()
@ -31,11 +31,11 @@ if (listening) {
})
})
listener.listen(mh, () => {
listener.listen(maddr, () => {
console.log('[listener] Listening')
})
} else {
direct.dial(mh, { config: {} }, (err, conn) => {
direct.dial(maddr, { config: {} }, (err, conn) => {
if (err) {
console.log(`[dialer] Failed to open connection: ${err}`)
}

88
integration/main.go Normal file
View File

@ -0,0 +1,88 @@
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
tpt "github.com/libp2p/go-libp2p-transport"
direct "github.com/libp2p/go-libp2p-webrtc-direct"
smux "github.com/libp2p/go-stream-muxer"
ma "github.com/multiformats/go-multiaddr"
"github.com/pions/webrtc"
mplex "github.com/whyrusleeping/go-smux-multiplex"
)
const listenFlag = "listen"
func main() {
listening := flag.Bool(listenFlag, false, "Listen for incoming connections.")
flag.Parse()
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct")
check(err)
transport := direct.NewTransport(
webrtc.RTCConfiguration{},
new(mplex.Transport),
)
if *listening {
list, err := transport.Listen(maddr)
check(err)
defer list.Close()
fmt.Println("[listener] Listening")
for {
c, err := list.Accept()
check(err)
fmt.Println("[listener] Got connection")
go handleConn(c)
}
} else {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := transport.Dial(ctx, maddr, "peerA")
check(err)
defer c.Close()
fmt.Println("[dialer] Opened connection")
s, err := c.OpenStream()
check(err)
fmt.Println("[dialer] Opened stream")
_, err = s.Write([]byte("hey, how is it going. I am the dialer"))
check(err)
err = s.Close()
check(err)
}
}
func handleConn(c tpt.Conn) {
for {
s, err := c.AcceptStream()
if err != nil {
return
}
fmt.Println("[listener] Got stream")
go handleStream(s)
}
}
func handleStream(s smux.Stream) {
b, err := ioutil.ReadAll(s)
check(err)
fmt.Println("[listener] Received:")
fmt.Println(string(b))
}
func check(err error) {
if err != nil {
panic(err)
}
}