ws_wasm.mx raw

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