ws_wasm.mx raw
1 //go:build wasm
2 //:build wasm
3
4 package ws
5
6 import (
7 "git.smesh.lol/smesh/web/common/jsbridge/callback"
8 "unsafe"
9 )
10
11 //:wasmimport bridge ws_dial
12 func wasmDial(urlPtr *byte, urlLen int32, urlCap int32, onMsgID int32, onOpenID int32, onCloseID int32, onErrID int32) (n int32)
13
14 //:wasmimport bridge ws_send
15 func wasmSend(conn int32, msgPtr *byte, msgLen int32, msgCap int32) (n int32)
16
17 //:wasmimport bridge ws_close
18 func wasmClose(conn int32)
19
20 //:wasmimport bridge ws_ready_state
21 func wasmReadyState(conn int32) (n int32)
22
23 func Dial(url string, onMessage func(int32, string), onOpen func(int32), onClose func(int32, int32, string), onError func(int32)) (c Conn) {
24 msgID := callback.RegisterIS(onMessage)
25 openID := callback.RegisterI(onOpen)
26 errID := callback.RegisterI(onError)
27 var closeID int32
28 closeID = callback.RegisterIIS(func(a int32, b int32, c string) {
29 callback.Release(msgID)
30 callback.Release(openID)
31 callback.Release(errID)
32 callback.Release(closeID)
33 onClose(a, b, c)
34 })
35 h := wasmDial(
36 unsafe.StringData(url), int32(len(url)), int32(len(url)),
37 msgID, openID, closeID, errID,
38 )
39 return Conn(h)
40 }
41
42 func Send(conn Conn, msg string) (ok bool) {
43 return wasmSend(int32(conn), unsafe.StringData(msg), int32(len(msg)), int32(len(msg))) != 0
44 }
45
46 func Close(conn Conn) {
47 wasmClose(int32(conn))
48 }
49
50 func ReadyState(conn Conn) (n int32) {
51 return wasmReadyState(int32(conn))
52 }
53