File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments