MCPcopy Index your code
hub / github.com/socketio/socket.io / patchAdapter

Function patchAdapter

lib/uws.ts:13–90  ·  view source on GitHub ↗
(app /* : TemplatedApp */)

Source from the content-addressed store, hash-verified

11const { addAll, del, broadcast } = Adapter.prototype;
12
13export function patchAdapter(app /* : TemplatedApp */) {
14 Adapter.prototype.addAll = function (id, rooms) {
15 const isNew = !this.sids.has(id);
16 addAll.call(this, id, rooms);
17 const socket: Socket = this.nsp.sockets.get(id);
18 if (!socket) {
19 return;
20 }
21 if (socket.conn.transport.name === "websocket") {
22 subscribe(this.nsp.name, socket, isNew, rooms);
23 return;
24 }
25 if (isNew) {
26 socket.conn.on("upgrade", () => {
27 const rooms = this.sids.get(id);
28 if (rooms) {
29 subscribe(this.nsp.name, socket, isNew, rooms);
30 }
31 });
32 }
33 };
34
35 Adapter.prototype.del = function (id, room) {
36 del.call(this, id, room);
37 const socket: Socket = this.nsp.sockets.get(id);
38 if (socket && socket.conn.transport.name === "websocket") {
39 // @ts-ignore
40 const sessionId = socket.conn.id;
41 // @ts-ignore
42 const websocket: WebSocket = socket.conn.transport.socket;
43 const topic = `${this.nsp.name}${SEPARATOR}${room}`;
44 debug("unsubscribe connection %s from topic %s", sessionId, topic);
45 websocket.unsubscribe(topic);
46 }
47 };
48
49 Adapter.prototype.broadcast = function (packet, opts) {
50 const useFastPublish = opts.rooms.size <= 1 && opts.except!.size === 0;
51 if (!useFastPublish) {
52 broadcast.call(this, packet, opts);
53 return;
54 }
55
56 const flags = opts.flags || {};
57 const basePacketOpts = {
58 preEncoded: true,
59 volatile: flags.volatile,
60 compress: flags.compress,
61 };
62
63 packet.nsp = this.nsp.name;
64 const encodedPackets = this.encoder.encode(packet);
65
66 const topic =
67 opts.rooms.size === 0
68 ? this.nsp.name
69 : `${this.nsp.name}${SEPARATOR}${opts.rooms.keys().next().value}`;
70 debug("fast publish to %s", topic);

Callers 1

attachAppMethod · 0.90

Calls 5

subscribeFunction · 0.85
keysMethod · 0.80
writeToEngineMethod · 0.80
onMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected