Skip to content

Commit 2da8210

Browse files
test: add test for volatile packet with binary
See also: socketio/socket.io-adapter@88eee59
1 parent 02b0f73 commit 2da8210

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

lib/uws.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ export function patchAdapter(app /* : TemplatedApp */) {
8181
this.apply(opts, (socket) => {
8282
if (socket.conn.transport.name !== "websocket") {
8383
// classic publish for clients connected with HTTP long-polling
84-
for (let i = 0; i < encodedPackets.length; i++) {
85-
socket.client.writeToEngine(encodedPackets[i], basePacketOpts);
86-
}
84+
socket.client.writeToEngine(encodedPackets, basePacketOpts);
8785
}
8886
});
8987
};

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"base64id": "~2.0.0",
5050
"debug": "~4.3.2",
5151
"engine.io": "~6.1.0",
52-
"socket.io-adapter": "~2.3.2",
52+
"socket.io-adapter": "~2.3.3",
5353
"socket.io-parser": "~4.0.4"
5454
},
5555
"devDependencies": {

test/socket.io.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,32 @@ describe("socket.io", () => {
14531453
}, 200);
14541454
});
14551455

1456+
it("should broadcast only one consecutive volatile event with binary (ws)", (done) => {
1457+
const srv = createServer();
1458+
const sio = new Server(srv, { transports: ["websocket"] });
1459+
1460+
let counter = 0;
1461+
srv.listen(() => {
1462+
sio.on("connection", (s) => {
1463+
// Wait to make sure there are no packets being sent for opening the connection
1464+
setTimeout(() => {
1465+
sio.volatile.emit("ev", Buffer.from([1, 2, 3]));
1466+
sio.volatile.emit("ev", Buffer.from([4, 5, 6]));
1467+
}, 20);
1468+
});
1469+
1470+
const socket = client(srv, { transports: ["websocket"] });
1471+
socket.on("ev", () => {
1472+
counter++;
1473+
});
1474+
});
1475+
1476+
setTimeout(() => {
1477+
expect(counter).to.be(1);
1478+
done();
1479+
}, 200);
1480+
});
1481+
14561482
it("should emit regular events after trying a failed volatile event (polling)", (done) => {
14571483
const srv = createServer();
14581484
const sio = new Server(srv, { transports: ["polling"] });
@@ -2516,28 +2542,6 @@ describe("socket.io", () => {
25162542
});
25172543
});
25182544
});
2519-
2520-
it("should pre encode a broadcast packet", (done) => {
2521-
const srv = createServer();
2522-
const sio = new Server(srv);
2523-
2524-
srv.listen(() => {
2525-
const clientSocket = client(srv, { multiplex: false });
2526-
2527-
sio.on("connection", (socket) => {
2528-
socket.conn.on("packetCreate", (packet) => {
2529-
expect(packet.data).to.eql('2["hello","world"]');
2530-
expect(packet.options.wsPreEncoded).to.eql('42["hello","world"]');
2531-
2532-
clientSocket.close();
2533-
sio.close();
2534-
done();
2535-
});
2536-
2537-
sio.emit("hello", "world");
2538-
});
2539-
});
2540-
});
25412545
});
25422546

25432547
describe("middleware", () => {

test/uws.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ describe("socket.io with uWebSocket.js-based engine", () => {
103103
io.emit("hello", Buffer.from([1, 2, 3]));
104104
});
105105

106+
it("should broadcast volatile packet with binary content", (done) => {
107+
const partialDone = createPartialDone(done, 3);
108+
109+
client.on("hello", partialDone);
110+
clientWSOnly.on("hello", partialDone);
111+
clientPollingOnly.on("hello", partialDone);
112+
clientCustomNamespace.on("hello", shouldNotHappen(done));
113+
114+
// wait to make sure there are no packets being sent for opening the connection
115+
setTimeout(() => {
116+
io.volatile.emit("hello", Buffer.from([1, 2, 3]));
117+
}, 20);
118+
});
119+
106120
it("should broadcast in a room", (done) => {
107121
const partialDone = createPartialDone(done, 2);
108122

0 commit comments

Comments
 (0)