Skip to content

Commit ebbfcfa

Browse files
committed
improve unknown client disconnect messages
ref #502
1 parent e4465f7 commit ebbfcfa

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,32 +649,34 @@ export async function connect (connectOptions: ConnectOptions) {
649649
bot._client.emit('connect')
650650
} else {
651651
const setupConnectHandlers = () => {
652-
Socket.prototype['handleStringMessage'] = function (message: string) {
652+
bot._client.socket['handleStringMessage'] = function (message: string) {
653653
if (message.startsWith('proxy-message') || message.startsWith('proxy-command:')) { // for future
654654
return false
655655
}
656656
if (message.startsWith('proxy-shutdown:')) {
657+
console.log('Got current connection proxy shutdown message', message)
657658
lastKnownKickReason = message.slice('proxy-shutdown:'.length)
658659
return false
659660
}
660661
return true
661662
}
663+
const closeUnknownReason = () => {
664+
if (!bot || ended) return
665+
bot.emit('end', lastKnownKickReason ?? 'WebSocket connection closed with unknown reason')
666+
}
667+
662668
bot._client.socket.on('connect', () => {
663669
console.log('Proxy WebSocket connection established')
664670
//@ts-expect-error
665671
bot._client.socket._ws.addEventListener('close', () => {
666672
console.log('WebSocket connection closed')
667673
setTimeout(() => {
668-
if (bot) {
669-
bot.emit('end', 'WebSocket connection closed with unknown reason')
670-
}
674+
closeUnknownReason()
671675
}, 1000)
672676
})
673677
bot._client.socket.on('close', () => {
674678
setTimeout(() => {
675-
if (bot) {
676-
bot.emit('end', 'WebSocket connection closed with unknown reason')
677-
}
679+
closeUnknownReason()
678680
})
679681
})
680682
})

0 commit comments

Comments
 (0)