Skip to content
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 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions app/durableObjects/ChatRoom.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export class ChatRoom extends Server<Env> {
)
)

if (!this.ctx.storage.getAlarm()) {
if (!(await this.ctx.storage.getAlarm())) {
Copy link
Collaborator Author

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.

// start the alarm to broadcast state every 30 seconds
this.ctx.storage.setAlarm(30000)
this.ctx.storage.setAlarm(Date.now() + 30000)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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> {
Expand All @@ -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)
}
}
14 changes: 13 additions & 1 deletion app/hooks/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,25 @@ export default function useRoom({
},
})

useEffect(() => {
function onBeforeUnload() {
websocket.send(
JSON.stringify({ type: 'userLeft' } satisfies ClientMessage)
)
}
window.addEventListener('beforeunload', onBeforeUnload)
return () => {
window.removeEventListener('beforeunload', onBeforeUnload)
}
}, [websocket])

// setup a simple ping pong
useEffect(() => {
const interval = setInterval(() => {
websocket.send(
JSON.stringify({ type: 'partyserver-ping' } satisfies ClientMessage)
)
}, 10000)
}, 5000)

return () => clearInterval(interval)
}, [websocket])
Expand Down