From 5579d40c24d15f69e44246f788fb93beb367f994 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 10 May 2021 09:35:33 +0200 Subject: [PATCH] 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: https://github.com/socketio/engine.io/commit/7706b123df914777d19c8179b45ab6932f82916c [1]: https://github.com/socketio/socket.io/issues/3775 --- lib/index.ts | 10 +++++++++- test/index.js | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index a8a7675..5147ae7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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); + } }); } diff --git a/test/index.js b/test/index.js index d976771..8e2bbe5 100644 --- a/test/index.js +++ b/test/index.js @@ -65,8 +65,10 @@ describe("socket.io-adapter", () => { id, { id, - packet() { - ids.push(id); + client: { + writeToEngine() { + ids.push(id); + } } } ]; @@ -74,7 +76,9 @@ describe("socket.io-adapter", () => { const nsp = { server: { encoder: { - encode() {} + encode() { + return []; + } } }, sockets: new Map([socket("s1"), socket("s2"), socket("s3")]) @@ -98,8 +102,10 @@ describe("socket.io-adapter", () => { id, { id, - packet() { - ids.push(id); + client: { + writeToEngine() { + ids.push(id); + } } } ]; @@ -107,7 +113,9 @@ describe("socket.io-adapter", () => { const nsp = { server: { encoder: { - encode() {} + encode() { + return []; + } } }, sockets: new Map([socket("s1"), socket("s2"), socket("s3")])