Skip to content

Commit

Permalink
test: add test for volatile packet with binary
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Nov 16, 2021
1 parent 02b0f73 commit 2da8210
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
4 changes: 1 addition & 3 deletions lib/uws.ts
Expand Up @@ -81,9 +81,7 @@ export function patchAdapter(app /* : TemplatedApp */) {
this.apply(opts, (socket) => {
if (socket.conn.transport.name !== "websocket") {
// classic publish for clients connected with HTTP long-polling
for (let i = 0; i < encodedPackets.length; i++) {
socket.client.writeToEngine(encodedPackets[i], basePacketOpts);
}
socket.client.writeToEngine(encodedPackets, basePacketOpts);
}
});
};
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,7 +49,7 @@
"base64id": "~2.0.0",
"debug": "~4.3.2",
"engine.io": "~6.1.0",
"socket.io-adapter": "~2.3.2",
"socket.io-adapter": "~2.3.3",
"socket.io-parser": "~4.0.4"
},
"devDependencies": {
Expand Down
48 changes: 26 additions & 22 deletions test/socket.io.ts
Expand Up @@ -1453,6 +1453,32 @@ describe("socket.io", () => {
}, 200);
});

it("should broadcast only one consecutive volatile event with binary (ws)", (done) => {
const srv = createServer();
const sio = new Server(srv, { transports: ["websocket"] });

let counter = 0;
srv.listen(() => {
sio.on("connection", (s) => {
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(() => {
sio.volatile.emit("ev", Buffer.from([1, 2, 3]));
sio.volatile.emit("ev", Buffer.from([4, 5, 6]));
}, 20);
});

const socket = client(srv, { transports: ["websocket"] });
socket.on("ev", () => {
counter++;
});
});

setTimeout(() => {
expect(counter).to.be(1);
done();
}, 200);
});

it("should emit regular events after trying a failed volatile event (polling)", (done) => {
const srv = createServer();
const sio = new Server(srv, { transports: ["polling"] });
Expand Down Expand Up @@ -2516,28 +2542,6 @@ describe("socket.io", () => {
});
});
});

it("should pre encode a broadcast packet", (done) => {
const srv = createServer();
const sio = new Server(srv);

srv.listen(() => {
const clientSocket = client(srv, { multiplex: false });

sio.on("connection", (socket) => {
socket.conn.on("packetCreate", (packet) => {
expect(packet.data).to.eql('2["hello","world"]');
expect(packet.options.wsPreEncoded).to.eql('42["hello","world"]');

clientSocket.close();
sio.close();
done();
});

sio.emit("hello", "world");
});
});
});
});

describe("middleware", () => {
Expand Down
14 changes: 14 additions & 0 deletions test/uws.ts
Expand Up @@ -103,6 +103,20 @@ describe("socket.io with uWebSocket.js-based engine", () => {
io.emit("hello", Buffer.from([1, 2, 3]));
});

it("should broadcast volatile packet with binary content", (done) => {
const partialDone = createPartialDone(done, 3);

client.on("hello", partialDone);
clientWSOnly.on("hello", partialDone);
clientPollingOnly.on("hello", partialDone);
clientCustomNamespace.on("hello", shouldNotHappen(done));

// wait to make sure there are no packets being sent for opening the connection
setTimeout(() => {
io.volatile.emit("hello", Buffer.from([1, 2, 3]));
}, 20);
});

it("should broadcast in a room", (done) => {
const partialDone = createPartialDone(done, 2);

Expand Down

0 comments on commit 2da8210

Please sign in to comment.