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

Class Socket

examples/basic-websocket-client/src/index.js:38–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36function noop() {}
37
38class Socket extends EventEmitter {
39 id;
40 connected = false;
41
42 #uri;
43 #opts;
44 #ws;
45 #pingTimeoutTimer;
46 #pingTimeoutDelay;
47 #sendBuffer = [];
48 #reconnectTimer;
49 #shouldReconnect = true;
50
51 constructor(uri, opts) {
52 super();
53 this.#uri = uri;
54 this.#opts = Object.assign(
55 {
56 path: "/socket.io/",
57 reconnectionDelay: 2000,
58 },
59 opts
60 );
61 this.#open();
62 }
63
64 #open() {
65 this.#ws = new WebSocket(this.#createUrl());
66 this.#ws.onmessage = ({ data }) => this.#onMessage(data);
67 // dummy handler for Node.js
68 this.#ws.onerror = noop;
69 this.#ws.onclose = () => this.#onClose("transport close");
70 }
71
72 #createUrl() {
73 const uri = this.#uri.replace(/^http/, "ws");
74 const queryParams = "?EIO=4&transport=websocket";
75 return `${uri}${this.#opts.path}${queryParams}`;
76 }
77
78 #onMessage(data) {
79 if (typeof data !== "string") {
80 // TODO handle binary payloads
81 return;
82 }
83
84 switch (data[0]) {
85 case EIOPacketType.OPEN:
86 this.#onOpen(data);
87 break;
88
89 case EIOPacketType.CLOSE:
90 this.#onClose("transport close");
91 break;
92
93 case EIOPacketType.PING:
94 this.#resetPingTimeout();
95 this.#send(EIOPacketType.PONG);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…