-
Notifications
You must be signed in to change notification settings - Fork 27
/
wss_test.go
67 lines (57 loc) · 1.17 KB
/
wss_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
package gotiktoklive
import (
"testing"
"time"
"github.com/Davincible/gotiktoklive/tests"
"golang.org/x/net/context"
)
func TestWebsocket(t *testing.T) {
tiktok := NewTikTok()
tiktok.Debug = true
tiktok.debugHandler = func(i ...interface{}) {
t.Log(i...)
}
id, err := tiktok.getRoomID(tests.USERNAME)
if err != nil {
t.Fatal(err)
}
live := Live{
t: tiktok,
ID: id,
Events: make(chan interface{}, 100),
}
ctx, cancel := context.WithCancel(context.Background())
live.done = ctx.Done
live.close = func() {
cancel()
close(live.Events)
}
err = live.getRoomData()
if err != nil {
t.Fatal(err)
}
if live.wsURL == "" {
t.Fatal("No websocket url provided")
}
t.Logf("Ws url: %s, %+v", live.wsURL, live.wsParams)
if err := live.connect(live.wsURL, live.wsParams); err != nil {
t.Fatal(err)
}
tiktok.wg.Add(2)
go live.readSocket()
go live.sendPing()
timeout := time.After(5 * time.Second)
for {
select {
case <-timeout:
return
case event := <-live.Events:
switch e := event.(type) {
case UserEvent:
t.Logf("%T: %s (%s) %s", e, e.User.Nickname, e.User.Nickname, e.Event)
default:
t.Logf("%T: %+v", e, e)
}
}
}
}