MCPcopy
hub / github.com/socketio/socket.io / WebSocket

Class WebSocket

packages/engine.io/lib/transports/websocket.ts:8–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6const debug = debugModule("engine:ws");
7
8export class WebSocket extends Transport {
9 perMessageDeflate?: boolean | PerMessageDeflateOptions;
10 private socket: WsWebSocket;
11
12 /**
13 * WebSocket transport
14 *
15 * @param {EngineRequest} req
16 */
17 constructor(req: EngineRequest) {
18 super(req);
19 this.socket = req.websocket;
20 this.socket.on("message", (data, isBinary) => {
21 const message = isBinary ? data : data.toString();
22 debug('received "%s"', message);
23 super.onData(message);
24 });
25 this.socket.once("close", this.onClose.bind(this));
26 this.socket.on("error", this.onError.bind(this));
27 this.writable = true;
28 this.perMessageDeflate = null;
29 }
30
31 /**
32 * Transport name
33 */
34 get name() {
35 return "websocket";
36 }
37
38 /**
39 * Advertise upgrade support.
40 */
41 get handlesUpgrades() {
42 return true;
43 }
44
45 send(packets: Packet[]) {
46 this.writable = false;
47
48 for (let i = 0; i < packets.length; i++) {
49 const packet = packets[i];
50 const isLast = i + 1 === packets.length;
51
52 if (this._canSendPreEncodedFrame(packet)) {
53 // the WebSocket frame was computed with WebSocket.Sender.frame()
54 // see https://github.com/websockets/ws/issues/617#issuecomment-283002469
55 // @ts-expect-error use of untyped member
56 this.socket._sender.sendFrame(
57 packet.options.wsPreEncodedFrame,
58 isLast ? this._onSentLast : this._onSent,
59 );
60 } else {
61 this.parser.encodePacket(
62 packet,
63 this.supportsBinary,
64 isLast ? this._doSendLast : this._doSend,
65 );

Callers

nothing calls this directly

Calls 3

emitMethod · 0.65
sendMethod · 0.45
onErrorMethod · 0.45

Tested by

no test coverage detected