-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendClt.go
46 lines (32 loc) · 1.08 KB
/
sendClt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"context"
"flag"
"fmt"
"github.com/perlin-network/noise"
)
func check(err error) {
if err != nil {
panic(err)
}
}
// This example demonstrates how to send/handle RPC requests across peers, how to listen for incoming
// peers, how to check if a message received is a request or not, how to reply to a RPC request, and
// how to cleanup node instances after you are done using them.
func main() {
// Let there be nodes Alice and Bob.
addrP := flag.String("addr", "", "peer TCP address")
flag.Parse()
fmt.Printf("The ping addr =%s\n", *addrP)
alice, err := noise.NewNode()
check(err)
// Gracefully release resources for Alice and Bob at the end of the example.
defer alice.Close()
// When Bob gets a message from Alice, print it out and respond to Alice with 'Hi Alice!'
check(alice.Listen())
// Have Alice send Bob a request with the message 'Hi Bob!'
res, err := alice.Request(context.TODO(), *addrP, []byte("Hi Alice->Bob!"))
check(err)
// Print out the response Bob got from Alice.
fmt.Printf("Got a message from Bob: '%s'\n", string(res))
}