Skip to content

Commit

Permalink
fix: fire close on failed WebSocket connection (#3566)
Browse files Browse the repository at this point in the history
* see #3546, #3548 & #3565

Signed-off-by: eXhumer <[email protected]>
  • Loading branch information
eXhumer committed Sep 20, 2024
1 parent fdeccc2 commit 290e0e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ class WebSocket extends EventTarget {
message: reason
})
}

if (!this.#parser && !this.#handler.receivedClose) {
fireEvent('close', this, (type, init) => new CloseEvent(type, init), {
wasClean: false,
code: 1006,
reason
})
}
}

#onMessage (type, data) {
Expand Down
2 changes: 1 addition & 1 deletion test/websocket/issue-3546.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('first error than close event is fired on failed connection', async (t) => {
const { completed, strictEqual } = tspl(t, { plan: 2 })
const { completed, strictEqual } = tspl(t, { plan: 4 })
const ws = new WebSocket('ws://localhost:1')

let orderOfEvents = 0
Expand Down

0 comments on commit 290e0e1

Please sign in to comment.