forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
134 lines (120 loc) · 3.42 KB
/
websocket.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package playwright
import (
"encoding/base64"
"errors"
)
type webSocketImpl struct {
channelOwner
isClosed bool
page *pageImpl
}
func (ws *webSocketImpl) URL() string {
return ws.initializer["url"].(string)
}
func newWebsocket(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *webSocketImpl {
ws := &webSocketImpl{}
ws.createChannelOwner(ws, parent, objectType, guid, initializer)
ws.page = fromChannel(parent.channel).(*pageImpl)
ws.channel.On("close", func() {
ws.Lock()
ws.isClosed = true
ws.Unlock()
ws.Emit("close", ws)
})
ws.channel.On(
"frameSent",
func(params map[string]interface{}) {
ws.onFrameSent(params["opcode"].(float64), params["data"].(string))
},
)
ws.channel.On(
"frameReceived",
func(params map[string]interface{}) {
ws.onFrameReceived(params["opcode"].(float64), params["data"].(string))
},
)
ws.channel.On(
"socketError",
func(params map[string]interface{}) {
ws.Emit("socketerror", params["error"])
},
)
return ws
}
func (ws *webSocketImpl) onFrameSent(opcode float64, data string) {
if opcode == 2 {
payload, err := base64.StdEncoding.DecodeString(data)
if err != nil {
logger.Error("could not decode WebSocket.onFrameSent payload", "error", err)
return
}
ws.Emit("framesent", payload)
} else {
ws.Emit("framesent", []byte(data))
}
}
func (ws *webSocketImpl) onFrameReceived(opcode float64, data string) {
if opcode == 2 {
payload, err := base64.StdEncoding.DecodeString(data)
if err != nil {
logger.Error("could not decode WebSocket.onFrameReceived payload", "error", err)
return
}
ws.Emit("framereceived", payload)
} else {
ws.Emit("framereceived", []byte(data))
}
}
func (ws *webSocketImpl) ExpectEvent(event string, cb func() error, options ...WebSocketExpectEventOptions) (interface{}, error) {
return ws.expectEvent(event, cb, options...)
}
func (ws *webSocketImpl) WaitForEvent(event string, options ...WebSocketWaitForEventOptions) (interface{}, error) {
if len(options) == 1 {
option := WebSocketExpectEventOptions(options[0])
return ws.expectEvent(event, nil, option)
} else {
return ws.expectEvent(event, nil)
}
}
func (ws *webSocketImpl) expectEvent(event string, cb func() error, options ...WebSocketExpectEventOptions) (interface{}, error) {
var predicate interface{} = nil
timeout := ws.page.timeoutSettings.Timeout()
if len(options) == 1 {
if options[0].Timeout != nil {
timeout = *options[0].Timeout
}
if options[0].Predicate != nil {
predicate = options[0].Predicate
}
}
waiter := newWaiter().WithTimeout(timeout)
if event != "close" {
waiter.RejectOnEvent(ws, "close", errors.New("websocket closed"))
}
if event != "socketerror" {
waiter.RejectOnEvent(ws, "socketerror", errors.New("websocket error"))
}
waiter.RejectOnEvent(ws.page, "close", errors.New("page closed"))
if cb == nil {
return waiter.WaitForEvent(ws, event, predicate).Wait()
} else {
return waiter.WaitForEvent(ws, event, predicate).RunAndWait(cb)
}
}
func (ws *webSocketImpl) IsClosed() bool {
ws.RLock()
defer ws.RUnlock()
return ws.isClosed
}
func (ws *webSocketImpl) OnClose(fn func(WebSocket)) {
ws.On("close", fn)
}
func (ws *webSocketImpl) OnFrameReceived(fn func(payload []byte)) {
ws.On("framereceived", fn)
}
func (ws *webSocketImpl) OnFrameSent(fn func(payload []byte)) {
ws.On("framesent", fn)
}
func (ws *webSocketImpl) OnSocketError(fn func(string)) {
ws.On("socketerror", fn)
}