Skip to content

Commit

Permalink
fix(server): Return ping's payload through the response pong
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Jun 9, 2021
1 parent 37720c6 commit 47730a9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/server.ts
Expand Up @@ -586,7 +586,16 @@ export function makeServer<E = unknown>(options: ServerOptions<E>): Server<E> {
return;
}
case MessageType.Ping: {
await socket.send(stringifyMessage({ type: MessageType.Pong }));
await socket.send(
stringifyMessage(
message.payload
? { type: MessageType.Pong, payload: message.payload }
: {
type: MessageType.Pong,
// payload is completely absent if not provided
},
),
);
return;
}
case MessageType.Pong:
Expand Down
20 changes: 20 additions & 0 deletions src/tests/server.ts
Expand Up @@ -662,6 +662,26 @@ describe('Ping/Pong', () => {
});
});

it("should return ping's payload through the pong", async () => {
const { url } = await startTServer();

const client = await createTClient(url);

client.ws.send(
stringifyMessage({
type: MessageType.Ping,
payload: { iCome: 'back' },
}),
);

await client.waitForMessage(({ data }) => {
expect(parseMessage(data)).toEqual({
type: MessageType.Pong,
payload: { iCome: 'back' },
});
});
});

it('should not react to a pong', async () => {
const { url } = await startTServer();

Expand Down

0 comments on commit 47730a9

Please sign in to comment.