Skip to content

Commit ecdeb75

Browse files
committed
fix: crash on connect() with closing ws
This commit fixes a bug that caused an error upon trying to connect to a new Discord voice gateway when the previous one wasn't totally closed yet.
1 parent 1332a66 commit ecdeb75

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

index.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Connection extends EventEmitter {
9797
return new Promise((resolve) => {
9898
this.udp.once('message', (message) => {
9999
const data = message.readUInt16BE(0)
100-
if (data != 2) return;
100+
if (data !== 2) return;
101101

102102
const packet = Buffer.from(message)
103103

@@ -198,10 +198,10 @@ class Connection extends EventEmitter {
198198

199199
let packet = Buffer.from(Sodium.open(voice, this.nonceBuffer, this.udpInfo.secretKey))
200200

201-
if (packet[0] == 0xbe && packet[1] == 0xde)
201+
if (packet[0] === 0xbe && packet[1] === 0xde)
202202
packet = packet.subarray(4 + 4 * packet.readUInt16BE(2))
203203

204-
if (packet.compare(OPUS_SILENCE_FRAME) == 0) {
204+
if (packet.compare(OPUS_SILENCE_FRAME) === 0) {
205205
if (userData.stream._readableState.ended) return;
206206

207207
this.emit('speakEnd', userData.userId, ssrc)
@@ -322,7 +322,7 @@ class Connection extends EventEmitter {
322322
this.player.timestamp += TIMESTAMP_INCREMENT
323323
if (this.player.timestamp >= MAX_TIMESTAMP) this.player.timestamp = 0
324324
this.player.sequence++
325-
if (this.player.sequence == MAX_SEQUENCE) this.player.sequence = 0
325+
if (this.player.sequence === MAX_SEQUENCE) this.player.sequence = 0
326326

327327
let packet = null
328328

@@ -344,7 +344,7 @@ class Connection extends EventEmitter {
344344
}
345345
case 'xsalsa20_poly1305_lite': {
346346
this.nonce++
347-
if (this.nonce == MAX_NONCE) this.nonce = 0
347+
if (this.nonce === MAX_NONCE) this.nonce = 0
348348
this.nonceBuffer.writeUInt32LE(this.nonce, 0)
349349

350350
const output = Sodium.close(chunk, this.nonceBuffer, this.udpInfo.secretKey)
@@ -422,7 +422,7 @@ class Connection extends EventEmitter {
422422
clearInterval(this.hbInterval)
423423
clearInterval(this.playInterval)
424424

425-
if (this.ws) {
425+
if (this.ws && !this.ws.closing) {
426426
this.ws.close(code, reason)
427427
this.ws.removeAllListeners()
428428
this.ws = null
@@ -457,25 +457,22 @@ class Connection extends EventEmitter {
457457
this.sessionId = obj.session_id
458458
}
459459

460-
_voiceServerUpdate(obj) {
460+
voiceServerUpdate(obj) {
461+
if (this.voiceServer?.token === obj.token && this.voiceServer?.endpoint === obj.endpoint) return;
462+
461463
this.voiceServer = {
462464
token: obj.token,
463465
endpoint: obj.endpoint
464466
}
465467

466-
this._updateState({ status: 'connecting' })
467-
}
468-
469-
voiceServerUpdate(obj) {
470-
if (this.voiceServer?.token == obj.token && this.voiceServer?.endpoint == obj.endpoint) return;
471-
472-
this._voiceServerUpdate(obj)
473-
474468
if (this.ws) {
475469
this._destroyConnection(4015, 'Voice server update')
476470

477-
this.connect(() => this.unpause('reconnected'), false)
478-
}
471+
this.connect(() => {
472+
if (this.audioStream) this.unpause('reconnected')
473+
else this._updatePlayerState({ status: 'idle', reason: 'reconnected' })
474+
}, false)
475+
} else this._updateState({ status: 'connecting' })
479476
}
480477
}
481478

ws.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class WebSocket extends EventEmitter {
255255
}
256256
}
257257

258-
this.socket.write(Buffer.concat([header, data]))
258+
if (this.socket) this.socket.write(Buffer.concat([ header, data ]))
259259

260260
return true
261261
}

0 commit comments

Comments
 (0)