MCPcopy Index your code
hub / github.com/coder/websocket / OnMessage

Method OnMessage

internal/wsjs/wsjs_js.go:107–123  ·  view source on GitHub ↗

OnMessage registers a function to be called when the WebSocket receives a message.

(fn func(m MessageEvent))

Source from the content-addressed store, hash-verified

105
106// OnMessage registers a function to be called when the WebSocket receives a message.
107func (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.
126func (c WebSocket) Subprotocol() string {

Callers 1

initMethod · 0.80

Calls 3

addEventListenerMethod · 0.95
extractArrayBufferFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected