MCPcopy Create free account
hub / github.com/nodejs/node / WebSocket

Class WebSocket

deps/undici/src/lib/web/websocket/websocket.js:60–657  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58
59// https://websockets.spec.whatwg.org/#interface-definition
60class WebSocket extends EventTarget {
61 #events = {
62 open: null,
63 error: null,
64 close: null,
65 message: null
66 }
67
68 #bufferedAmount = 0
69 #protocol = ''
70 #extensions = ''
71
72 /** @type {SendQueue} */
73 #sendQueue
74
75 /** @type {Handler} */
76 #handler = {
77 onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
78 onMessage: (opcode, data) => this.#onMessage(opcode, data),
79 onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
80 onParserDrain: () => this.#onParserDrain(),
81 onSocketData: (chunk) => {
82 if (!this.#parser.write(chunk)) {
83 this.#handler.socket.pause()
84 }
85 },
86 onSocketError: (err) => {
87 this.#handler.readyState = states.CLOSING
88
89 if (channels.socketError.hasSubscribers) {
90 channels.socketError.publish(err)
91 }
92
93 this.#handler.socket.destroy()
94 },
95 onSocketClose: () => this.#onSocketClose(),
96 onPing: (body) => {
97 if (channels.ping.hasSubscribers) {
98 channels.ping.publish({
99 payload: body,
100 websocket: this
101 })
102 }
103 },
104 onPong: (body) => {
105 if (channels.pong.hasSubscribers) {
106 channels.pong.publish({
107 payload: body,
108 websocket: this
109 })
110 }
111 },
112
113 readyState: states.CONNECTING,
114 socket: null,
115 closeState: new Set(),
116 controller: null,
117 wasEverConnected: false

Callers

nothing calls this directly

Calls 9

#onMessageMethod · 0.95
#onParserDrainMethod · 0.95
#onSocketCloseMethod · 0.95
failWebsocketConnectionFunction · 0.70
pauseMethod · 0.65
writeMethod · 0.45
publishMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected