OnMessage registers a function to be called when the WebSocket receives a message.
(fn func(m MessageEvent))
| 105 | |
| 106 | // OnMessage registers a function to be called when the WebSocket receives a message. |
| 107 | func (c WebSocket) OnMessage(fn func(m MessageEvent)) (remove func()) { |
| 108 | return c.addEventListener("message", func(e js.Value) { |
| 109 | var data any |
| 110 | |
| 111 | arrayBuffer := e.Get("data") |
| 112 | if arrayBuffer.Type() == js.TypeString { |
| 113 | data = arrayBuffer.String() |
| 114 | } else { |
| 115 | data = extractArrayBuffer(arrayBuffer) |
| 116 | } |
| 117 | |
| 118 | me := MessageEvent{ |
| 119 | Data: data, |
| 120 | } |
| 121 | fn(me) |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | // Subprotocol returns the WebSocket subprotocol in use. |
| 126 | func (c WebSocket) Subprotocol() string { |
no test coverage detected