Skip to content

Commit

Permalink
fix(core): do not send pongs when cable is not connected
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
palkan committed Feb 27, 2024
1 parent c83d184 commit 429b08a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Do not try to send `pong` if cable is no longer connected. ([@palkan][])

## 0.7.12 (2024-01-08)

- Omit `undefined` within Channels' params to fix invalid JSON serialization. ([@ardecvz][])
Expand Down
5 changes: 4 additions & 1 deletion packages/core/action_cable_ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export class ActionCableExtendedProtocol extends ActionCableProtocol {
// Send pongs asynchrounously—no need to block the main thread
async sendPong() {
await new Promise(resolve => setTimeout(resolve, 0))
this.cable.send({ command: 'pong' })
// Only send pong if the connection is still open
if (this.cable.state === 'connected') {
this.cable.send({ command: 'pong' })
}
}
}
10 changes: 10 additions & 0 deletions packages/core/action_cable_ext/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ describe('receive', () => {
})

it('sends pong in response to ping', async () => {
cable.connected()
protocol.receive({ type: 'ping', message: '42' })
expect(cable.lastPingedAt).toEqual(42)

Expand All @@ -299,6 +300,15 @@ describe('receive', () => {
command: 'pong'
})
})

it('does not try to send pong if cable is no longer connected', async () => {
protocol.receive({ type: 'ping', message: '42' })
expect(cable.lastPingedAt).toEqual(42)
cable.disconnected()
await new Promise(resolve => setTimeout(resolve, 0))

expect(cable.mailbox).toHaveLength(0)
})
})
})

Expand Down

0 comments on commit 429b08a

Please sign in to comment.