-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
don't broadcast state immediately onClose #75
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,9 @@ export class ChatRoom extends Server<Env> { | |
) | ||
) | ||
|
||
if (!this.ctx.storage.getAlarm()) { | ||
if (!(await this.ctx.storage.getAlarm())) { | ||
// start the alarm to broadcast state every 30 seconds | ||
this.ctx.storage.setAlarm(30000) | ||
this.ctx.storage.setAlarm(Date.now() + 30000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. augh. maybe this should always accept only Dates, it's to oeasy to screw up. |
||
} | ||
|
||
// cleaning out storage used by older versions of this code | ||
|
@@ -74,16 +74,29 @@ export class ChatRoom extends Server<Env> { | |
} | ||
|
||
broadcastState() { | ||
this.broadcast( | ||
JSON.stringify({ | ||
type: 'roomState', | ||
state: { | ||
users: [...this.getConnections<User>()] | ||
.map((connection) => connection.state) | ||
.filter((x) => !!x), | ||
}, | ||
} satisfies ServerMessage) | ||
) | ||
let didSomeoneQuit = false | ||
const roomStateMessage = { | ||
type: 'roomState', | ||
state: { | ||
users: [...this.getConnections<User>()] | ||
.map((connection) => connection.state) | ||
.filter((x) => !!x), | ||
}, | ||
} satisfies ServerMessage | ||
|
||
for (const connection of this.getConnections<User>()) { | ||
try { | ||
connection.send(JSON.stringify(roomStateMessage)) | ||
} catch (err) { | ||
connection.close(1011, 'Failed to broadcast state') | ||
didSomeoneQuit = true | ||
} | ||
} | ||
|
||
if (didSomeoneQuit) { | ||
// broadcast again to remove the user who quit | ||
this.broadcastState() | ||
} | ||
} | ||
|
||
async onMessage( | ||
|
@@ -100,7 +113,8 @@ export class ChatRoom extends Server<Env> { | |
|
||
switch (data.type) { | ||
case 'userLeft': | ||
// TODO: ?? | ||
connection.close(1000, 'User left') | ||
this.broadcastState() | ||
break | ||
case 'userUpdate': | ||
connection.setState(data.user) | ||
|
@@ -176,7 +190,11 @@ export class ChatRoom extends Server<Env> { | |
} | ||
|
||
onClose() { | ||
this.broadcastState() | ||
// while it makes sense to broadcast immediately on close, | ||
// it's possible that the websocket just closed for an instant | ||
// and will reconnect momentarily. | ||
// so let's just let the alarm handler do the broadcasting. | ||
// this.broadcastState() | ||
} | ||
|
||
onError(): void | Promise<void> { | ||
|
@@ -187,6 +205,6 @@ export class ChatRoom extends Server<Env> { | |
// technically we don't need to broadcast state on an alarm, | ||
// but let's keep it for a while and see if it's useful | ||
this.broadcastState() | ||
this.ctx.storage.setAlarm(30000) | ||
this.ctx.storage.setAlarm(Date.now() + 30000) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fml. this would have been caught with a lint rule. I'll work on adding a better lint setup soon.