Skip to content

Commit dc81fcf

Browse files
committedSep 9, 2021
fix: send volatile packets with binary attachments
The binary attachments of volatile packets were discarded (only the header packet was sent) due to a bug introduced by [1]. Related: #3919 [1]: dc381b7
1 parent c100b7b commit dc81fcf

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed
 

‎lib/client.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,11 @@ export class Client<
209209
const encodedPackets = opts.preEncoded
210210
? (packet as any[]) // previous versions of the adapter incorrectly used socket.packet() instead of writeToEngine()
211211
: this.encoder.encode(packet as Packet);
212-
for (const encodedPacket of encodedPackets) {
213-
this.writeToEngine(encodedPacket, opts);
214-
}
212+
this.writeToEngine(encodedPackets, opts);
215213
}
216214

217215
private writeToEngine(
218-
encodedPacket: String | Buffer,
216+
encodedPackets: Array<String | Buffer>,
219217
opts: WriteOptions
220218
): void {
221219
if (opts.volatile && !this.conn.transport.writable) {
@@ -224,7 +222,12 @@ export class Client<
224222
);
225223
return;
226224
}
227-
this.conn.write(encodedPacket, opts);
225+
const packets = Array.isArray(encodedPackets)
226+
? encodedPackets
227+
: [encodedPackets];
228+
for (const encodedPacket of packets) {
229+
this.conn.write(encodedPacket, opts);
230+
}
228231
}
229232

230233
/**

‎test/socket.io.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,32 @@ describe("socket.io", () => {
13821382
}, 200);
13831383
});
13841384

1385+
it("should emit only one consecutive volatile event with binary (ws)", (done) => {
1386+
const srv = createServer();
1387+
const sio = new Server(srv, { transports: ["websocket"] });
1388+
1389+
let counter = 0;
1390+
srv.listen(() => {
1391+
sio.on("connection", (s) => {
1392+
// Wait to make sure there are no packets being sent for opening the connection
1393+
setTimeout(() => {
1394+
s.volatile.emit("ev", Buffer.from([1, 2, 3]));
1395+
s.volatile.emit("ev", Buffer.from([4, 5, 6]));
1396+
}, 20);
1397+
});
1398+
1399+
const socket = client(srv, { transports: ["websocket"] });
1400+
socket.on("ev", () => {
1401+
counter++;
1402+
});
1403+
});
1404+
1405+
setTimeout(() => {
1406+
expect(counter).to.be(1);
1407+
done();
1408+
}, 200);
1409+
});
1410+
13851411
it("should emit regular events after trying a failed volatile event (polling)", (done) => {
13861412
const srv = createServer();
13871413
const sio = new Server(srv, { transports: ["polling"] });

0 commit comments

Comments
 (0)