forked from hesh915/go-socket.io-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
63 lines (56 loc) · 1.25 KB
/
test.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
package main
import (
"github.com/zhouhui8915/go-socket.io-client"
"log"
"bufio"
"os"
"time"
)
func main() {
opts := &socketio_client.Options{
//Transport:"polling",
Transport:"websocket",
Query:make(map[string]string),
}
opts.Query["uid"] = "1"
opts.Query["cid"] = "conf_123"
uri := "http://192.168.1.70:9090"
client,err := socketio_client.NewClient(uri,opts)
if err != nil {
log.Printf("NewClient error:%v\n",err)
return
}
client.On("error", func() {
log.Printf("on error\n")
})
client.On("connection", func() {
log.Printf("on connect\n")
})
client.On("message", func(msg string) {
log.Printf("on message:%v\n", msg)
})
client.On("disconnection", func() {
log.Printf("on disconnect\n")
})
go func() {
authStr := "{\"uid\":\"" + opts.Query["uid"] + "\",\"cid\":\"" + opts.Query["cid"] + "\"}"
for {
err := client.Emit("authenticate", authStr)
if err != nil {
log.Printf("Emit auth error:%v\n",err)
}
time.Sleep(10 * time.Second)
}
}()
reader := bufio.NewReader(os.Stdin)
for {
data, _, _ := reader.ReadLine()
command := string(data)
err := client.Emit("message",command)
if err != nil {
log.Printf("Emit message error:%v\n",err)
continue
}
log.Printf("send message:%v\n",command)
}
}