forked from tarndt/wasmws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockettype_js.go
53 lines (46 loc) · 1.35 KB
/
sockettype_js.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
package wasmws
import (
"syscall/js"
)
const (
socketTypeUnknown = iota
socketTypeBlob
socketTypeArrayBuffer
)
type socketType uint8
//newSocketType returns the socket type of the provided JavaScript websocket object
func newSocketType(websocket js.Value) socketType {
return newSocketTypeString(websocket.Get("binaryType").String())
}
//newSocketTypeString returns a socketType from a string of the socket type that
// matches the JavaScript websock.binaryType property:
// See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
func newSocketTypeString(wsTypeStr string) socketType {
switch wsTypeStr {
case "blob":
return socketTypeBlob
case "arraybuffer":
return socketTypeArrayBuffer
default:
return socketTypeUnknown
}
}
//String returns the a string of the socket type that matches the JavaScript
// websock.binaryType property: See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
func (st socketType) String() string {
switch st {
case socketTypeArrayBuffer:
return "arraybuffer"
case socketTypeBlob:
return "blob"
default:
return "unknown"
}
}
//Set sets the type of the provided JavaScript websocket to itself
func (st socketType) Set(websocket js.Value) {
websocket.Set("binaryType", st.String())
if debugVerbose {
println("Websocket: Set websocket binaryType (mode) to", st.String())
}
}