Skip to content

Commit

Permalink
优化socket
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jul 14, 2022
1 parent 734ea43 commit 1715927
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ export class Client extends EventEmitter {
this.socket.unpipe()
await this.proxy.plugin.hooks.server.prePipeToBackend.promise(this, backend)
if (this.closed) return
Object.assign(this.logger.fields, {backend: pick(backend, 'host', 'port'), username: this.username})

const socket = await pTimeout((async () => {
const socket = connect(backend.port, backend.host)
await once(socket, 'connect')
return socket
})(), ms('10s'))
})(), ms('10s'), 'connect to backend timed out')

socket.on('close', () => {
backend.removeClient(this)
})
socket.on('error', (err) => {
this.logger.error({err})
this.close(`failed to connect backend: ${err.message}`)
})

backend.addClient(this)
if (backend.useProxy) {
Expand Down Expand Up @@ -160,14 +169,6 @@ export class Client extends EventEmitter {
this.socket.pipe(socket)
socket.pipe(this.socket)

socket.on('close', () => {
backend.removeClient(this)
})
socket.on('error', (err) => {
this.logger.error({err})
this.close(`failed to connect backend: ${err.message}`)
})

return socket
}

Expand Down

0 comments on commit 1715927

Please sign in to comment.