Skip to content

Commit 6f15fcc

Browse files
committed
fix: display proxy disconnect message to the client when proxy server is being shutdown
1 parent af9da93 commit 6f15fcc

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

pnpm-lock.yaml

+12-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { watchOptionsAfterViewerInit, watchOptionsAfterWorldViewInit } from './w
3232
import downloadAndOpenFile from './downloadAndOpenFile'
3333

3434
import fs from 'fs'
35-
import net from 'net'
35+
import net, { Socket } from 'net'
3636
import mineflayer from 'mineflayer'
3737

3838
import debug from 'debug'
@@ -300,6 +300,7 @@ export async function connect (connectOptions: ConnectOptions) {
300300
let updateDataAfterJoin = () => { }
301301
let localServer
302302
let localReplaySession: ReturnType<typeof startLocalReplayServer> | undefined
303+
let lastKnownKickReason = undefined as string | undefined
303304
try {
304305
const serverOptions = defaultsDeep({}, connectOptions.serverOverrides ?? {}, options.localServerOptions, defaultServerOptions)
305306
Object.assign(serverOptions, connectOptions.serverOverridesFlat ?? {})
@@ -566,6 +567,16 @@ export async function connect (connectOptions: ConnectOptions) {
566567
bot._client.emit('connect')
567568
} else {
568569
const setupConnectHandlers = () => {
570+
Socket.prototype['handleStringMessage'] = function (message: string) {
571+
if (message.startsWith('proxy-message') || message.startsWith('proxy-command:')) { // for future
572+
return false
573+
}
574+
if (message.startsWith('proxy-shutdown:')) {
575+
lastKnownKickReason = message.slice('proxy-shutdown:'.length)
576+
return false
577+
}
578+
return true
579+
}
569580
bot._client.socket.on('connect', () => {
570581
console.log('Proxy WebSocket connection established')
571582
//@ts-expect-error
@@ -643,9 +654,9 @@ export async function connect (connectOptions: ConnectOptions) {
643654
if (ended) return
644655
console.log('disconnected for', endReason)
645656
if (endReason === 'socketClosed') {
646-
endReason = 'Connection with server lost'
657+
endReason = lastKnownKickReason ?? 'Connection with server lost'
647658
}
648-
setLoadingScreenStatus(`You have been disconnected from the server. End reason: ${endReason}`, true)
659+
setLoadingScreenStatus(`You have been disconnected from the server. End reason:\n${endReason}`, true)
649660
appStatusState.showReconnect = true
650661
onPossibleErrorDisconnect()
651662
destroyAll()

src/react/AppStatus.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default ({
4646
<>
4747
<span style={{
4848
wordBreak: 'break-word',
49+
whiteSpace: 'pre-wrap',
4950
}}
5051
>
5152
{status}

0 commit comments

Comments
 (0)