-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_session_test.go
60 lines (53 loc) · 1.54 KB
/
ws_session_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
package dnet
import (
"fmt"
"net"
"testing"
"time"
)
func TestNewWSSession(t *testing.T) {
go func() {
ServeWSFunc(":4522", func(conn net.Conn) {
fmt.Println("new Conn", conn.RemoteAddr())
session := NewWSSession(conn,
WithCloseCallback(func(session Session, reason error) {
fmt.Println(session.RemoteAddr(), reason, "ss close")
}),
WithMessageCallback(func(session Session, message interface{}) {
fmt.Println("ss", message)
}),
WithErrorCallback(func(session Session, err error) {
fmt.Println("ss error", err)
}))
time.Sleep(time.Millisecond * 200)
fmt.Println(session.Send([]byte{4, 3, 2, 1}))
fmt.Println(session.Send([]byte{4, 3, 2, 1}))
})
}()
//http.HandleFunc()
time.Sleep(time.Millisecond * 100)
wsConn, err := DialWS("127.0.0.1:4522", 0)
if err != nil {
fmt.Println("dialWs", err)
return
}
session := NewWSSession(wsConn,
WithCloseCallback(func(session Session, reason error) {
fmt.Println(session.RemoteAddr(), reason, "cc close")
}),
WithMessageCallback(func(session Session, message interface{}) {
fmt.Println("cc", message)
}),
WithErrorCallback(func(session Session, err error) {
fmt.Println("cc error", err)
}))
fmt.Println(session.Send([]byte{1, 2, 3, 4}))
fmt.Println(session.Send([]byte{1, 2, 3, 4, 5}))
fmt.Println(session.Send([]byte{1, 2, 3, 4, 5, 6}))
//fmt.Println(session.Send(123))
time.Sleep(time.Second)
fmt.Println(" ------- close ----------")
session.Close(nil)
fmt.Println(session.Send([]byte{1, 2, 3, 4}))
time.Sleep(time.Second)
}