Skip to content

Commit 5579d40

Browse files
feat: add support for the "wsPreEncoded" writing option
A few notes: - only the first element is pre-encoded, because the other elements are buffers and will be sent as is over the WebSocket connection - using `socket.packet()` was unexpectedly working, we will now use `socket.client.writeToEngine()` (see [1]) See also: socketio/engine.io@7706b12 [1]: socketio/socket.io#3775
1 parent c679e62 commit 5579d40

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lib/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,16 @@ export class Adapter extends EventEmitter {
135135
packet.nsp = this.nsp.name;
136136
const encodedPackets = this.encoder.encode(packet);
137137

138+
const firstPacketOpts = {
139+
wsPreEncoded: "4" + encodedPackets[0], // "4" being the "message" packet type in Engine.IO
140+
...packetOpts
141+
};
142+
138143
this.apply(opts, socket => {
139-
socket.packet(encodedPackets, packetOpts);
144+
socket.client.writeToEngine(encodedPackets[0], firstPacketOpts);
145+
for (let i = 1; i < encodedPackets.length; i++) {
146+
socket.client.writeToEngine(encodedPackets[i], packetOpts);
147+
}
140148
});
141149
}
142150

test/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,20 @@ describe("socket.io-adapter", () => {
6565
id,
6666
{
6767
id,
68-
packet() {
69-
ids.push(id);
68+
client: {
69+
writeToEngine() {
70+
ids.push(id);
71+
}
7072
}
7173
}
7274
];
7375
}
7476
const nsp = {
7577
server: {
7678
encoder: {
77-
encode() {}
79+
encode() {
80+
return [];
81+
}
7882
}
7983
},
8084
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])
@@ -98,16 +102,20 @@ describe("socket.io-adapter", () => {
98102
id,
99103
{
100104
id,
101-
packet() {
102-
ids.push(id);
105+
client: {
106+
writeToEngine() {
107+
ids.push(id);
108+
}
103109
}
104110
}
105111
];
106112
}
107113
const nsp = {
108114
server: {
109115
encoder: {
110-
encode() {}
116+
encode() {
117+
return [];
118+
}
111119
}
112120
},
113121
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])

0 commit comments

Comments
 (0)