forked from graarh/golang-socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
76 lines (62 loc) · 1.37 KB
/
client.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
package main
import (
"log"
"runtime"
"time"
gosocketio "github.com/Jason-SP-Chien/golang-socketio"
"github.com/Jason-SP-Chien/golang-socketio/transport"
)
type Channel struct {
Channel string `json:"channel"`
}
type Message struct {
Id int `json:"id"`
Channel string `json:"channel"`
Text string `json:"text"`
}
func sendJoin(c *gosocketio.Client) {
log.Println("Acking /join")
result, err := c.Ack("/join", Channel{"main"}, time.Second*5)
if err != nil {
log.Fatal(err)
} else {
log.Println("Ack result to /join: ", result)
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
c, err := gosocketio.Dial(
gosocketio.GetUrl("localhost", 3811, false),
"",
transport.GetDefaultWebsocketTransport())
if err != nil {
log.Fatal(err)
}
err = c.On("/message", func(h *gosocketio.Channel, args Message) {
log.Println("--- Got chat message: ", args)
})
if err != nil {
log.Fatal(err)
}
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
log.Fatal("Disconnected")
})
if err != nil {
log.Fatal(err)
}
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
log.Println("Connected")
})
if err != nil {
log.Fatal(err)
}
time.Sleep(1 * time.Second)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
time.Sleep(60 * time.Second)
c.Close()
log.Println(" [x] Complete")
}