Skip to content

Commit

Permalink
feat: add support for the "wsPreEncoded" writing option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed May 10, 2021
1 parent c679e62 commit 5579d40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
10 changes: 9 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ export class Adapter extends EventEmitter {
packet.nsp = this.nsp.name;
const encodedPackets = this.encoder.encode(packet);

const firstPacketOpts = {
wsPreEncoded: "4" + encodedPackets[0], // "4" being the "message" packet type in Engine.IO
...packetOpts
};

this.apply(opts, socket => {
socket.packet(encodedPackets, packetOpts);
socket.client.writeToEngine(encodedPackets[0], firstPacketOpts);
for (let i = 1; i < encodedPackets.length; i++) {
socket.client.writeToEngine(encodedPackets[i], packetOpts);
}
});
}

Expand Down
20 changes: 14 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ describe("socket.io-adapter", () => {
id,
{
id,
packet() {
ids.push(id);
client: {
writeToEngine() {
ids.push(id);
}
}
}
];
}
const nsp = {
server: {
encoder: {
encode() {}
encode() {
return [];
}
}
},
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])
Expand All @@ -98,16 +102,20 @@ describe("socket.io-adapter", () => {
id,
{
id,
packet() {
ids.push(id);
client: {
writeToEngine() {
ids.push(id);
}
}
}
];
}
const nsp = {
server: {
encoder: {
encode() {}
encode() {
return [];
}
}
},
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])
Expand Down

0 comments on commit 5579d40

Please # to comment.