-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_test.go
94 lines (71 loc) · 1.73 KB
/
websocket_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
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
93
94
package websocket
import (
"fmt"
"net/http"
"testing"
"time"
)
func Test(t *testing.T){
server := NewServer("http://localhost:3000")
http.Handle("/ws", server.Handler())
http.HandleFunc("/client.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./client.js")
})
static := http.FileServer(http.Dir("./test/"))
http.Handle("/", static)
LogErrors()
server.Connect(func(client *Client){
fmt.Println("connected:", client.ClientID)
client.On("message", func(msg interface{}) {
str := ToType[string](msg)
fmt.Println("client:", str)
})
client.Disconnect(func(code int) {
fmt.Println("client disconnected:", client.ClientID, "-", code)
})
})
handled := 0
server.Connect(func(client *Client){
fmt.Println("connected")
handled++
client.Store["key"] = "value"
client.On("message", func(msg interface{}) {
str := ToType[string](msg)
fmt.Println("client:", str)
handled++
})
client.Disconnect(func(code int) {
fmt.Println("client disconnected", code)
handled++
})
server.Broadcast("message", "test")
server.Broadcast("no-message", "test should not be sent")
})
server.On("message", func(client *Client, msg interface{}) {
fmt.Println("server:", msg)
fmt.Println("key:", client.Store["key"])
if client.Store["key"] == "value" {
handled++
}
handled++
})
server.Disconnect(func(client *Client, code int) {
fmt.Println("server disconnected", code)
handled++
})
go func(){
err := http.ListenAndServe(":3000", nil)
if err != nil {
t.Error(err)
}
}()
time.Sleep(10 * time.Second)
if handled > 0 && handled < 6 {
t.Error("test did not finish correctly")
}
if len(ErrLog) != 0 {
for _, err := range ErrLog {
t.Error(err)
}
}
}