Skip to content

Commit 4b11b19

Browse files
committed
Fix ping pong sample
1 parent 690efff commit 4b11b19

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

examples/sockets/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h1>Actor Sockets Demo</h1>
3636
const statusElement = document.getElementById('connectionStatus');
3737

3838
// Replace with your deployed Worker URL below:
39-
const wsUrl = `wss://actor-sockets.YOUR-IDENTIFIER.workers.dev/ws`;
39+
const wsUrl = `wss://actor-sockets.brayden-b8b.workers.dev/ws`;
4040

4141
function getUrlParameter(name) {
4242
const urlParams = new URLSearchParams(window.location.search);

examples/sockets/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export class MySocketsActor extends Actor<Env> {
77
sockets: {
88
upgradePath: '/ws', // Also defaults to `/ws` when not present,
99
autoResponse: {
10-
ping: 'pong',
11-
pong: 'ping'
10+
ping: 'ping',
11+
pong: 'pong'
1212
}
1313
}
1414
};
@@ -31,9 +31,8 @@ export class MySocketsActor extends Actor<Env> {
3131
}
3232

3333
protected onSocketMessage(ws: WebSocket, message: any) {
34-
// Echo message back when recieved
35-
const senderSocketId = ws.deserializeAttachment?.()?.connectionId;
36-
this.sockets.message('Received!', ['*'], [senderSocketId]);
34+
// Echo message back to everyone except the sender
35+
this.sockets.message('Received!', '*', [ws]);
3736
}
3837
}
3938

packages/sockets/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class Sockets<P extends DurableObject<any>> {
4242

4343
message(message: string, to?: RecipientType[] | '*', exclude?: RecipientType[]) {
4444
for (const [id, socket] of this.connections.entries()) {
45-
if (to?.length === 0 || !to || to === '*') {
45+
// If no `to` recipient is defined, default send message to everyone
46+
if (to === '*' || !to || to?.length === 0) {
4647
// If the `id` is in the exclude list then skip this iteration
4748
if (exclude?.includes(id)) {
4849
continue;
@@ -53,6 +54,7 @@ export class Sockets<P extends DurableObject<any>> {
5354
continue;
5455
}
5556

57+
// Send message if the `to` is not excluded
5658
socket.send(message);
5759
} else if (to?.includes(id)) {
5860
// When `to` is defined we only send the message to the

0 commit comments

Comments
 (0)