-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (79 loc) · 2.32 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"github.com/craftdome/go-nym"
"github.com/craftdome/go-nym/response"
"github.com/craftdome/go-nym/tags"
"os"
"os/signal"
)
var (
done = make(chan struct{}, 1)
)
func main() {
// Kill signals processing
interrupt := make(chan os.Signal)
signal.Notify(interrupt, os.Interrupt, os.Kill)
// Init the client via server credentials
client := nym.NewClient("ws://192.168.88.4:1977")
// Dial a connection to the server
if err := client.Dial(); err != nil {
panic(err)
}
// Start reading WS messages
go func() {
if err := client.ListenAndServe(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
go func() {
// Incoming Message Channel
for message := range client.Messages() {
switch message.Type() {
case tags.Error:
msg := message.(*response.Error)
fmt.Printf("Error: %s\n", msg.Message)
case tags.SelfAddress:
msg := message.(*response.SelfAddress)
fmt.Printf("SelfAddress: %s\n", msg.Address)
case tags.Received:
msg := message.(*response.Received)
fmt.Printf("Received: %s, SenderTag: %s\n", msg.Message, msg.SenderTag)
}
}
fmt.Println("Closed")
done <- struct{}{}
}()
// Request your own address
if err := client.SendRequestAsText(nym.NewGetSelfAddress()); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Send a message
addr := "2w2mvQzGHuzXdz1pQSvTWXiqZe26Z2BKNkFTQ5g7MuLi.DfkhfLipgtuRLAWWHx74iGkJWCpM6U5RFwaJ3FUaMicu@HWdr8jgcr32cVGbjisjmwnVF4xrUBRGvbw86F9e3rFzS"
r := nym.NewSend("Mix it up!", addr)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Send an anonymous message
addr = "2w2mvQzGHuzXdz1pQSvTWXiqZe26Z2BKNkFTQ5g7MuLi.DfkhfLipgtuRLAWWHx74iGkJWCpM6U5RFwaJ3FUaMicu@HWdr8jgcr32cVGbjisjmwnVF4xrUBRGvbw86F9e3rFzS"
replySurbs := 1
r = nym.NewSendAnonymous("Enjoy your anonymous!", addr, replySurbs)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Reply to an anonymous message
senderTag := "7vv2LmF9M6EwQRrmCiCJhr"
r = nym.NewReply("Pong.", senderTag)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Waiting for the kill or interrupt signal
<-interrupt
// Closing the client
if err := client.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Waiting for the done signal
<-done
fmt.Println("Done.")
}