Skip to content

Commit 6818acc

Browse files
author
eXhumer
committed
fix: fire close on failed WebSocket connection
* see #3546, #3548 & #3565 Signed-off-by: eXhumer <[email protected]>
1 parent 57f1b4e commit 6818acc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/web/websocket/websocket.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,14 @@ class WebSocket extends EventTarget {
537537
message: reason
538538
})
539539
}
540+
541+
if (!this.#parser && !this.#handler.receivedClose) {
542+
fireEvent('close', this, (type, init) => new CloseEvent(type, init), {
543+
wasClean: false,
544+
code: 1006,
545+
reason
546+
})
547+
}
540548
}
541549

542550
#onMessage (type, data) {

test/websocket/issue-3546.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { WebSocket } = require('../..')
55
const { tspl } = require('@matteo.collina/tspl')
66

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

1111
let orderOfEvents = 0

0 commit comments

Comments
 (0)