feat: 优化websocket
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
cf9153c134
commit
476d98b9c7
@ -53,13 +53,11 @@ export class WebSocket extends EventEmitter {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.client.on('open', (event) => {
|
this.client.on('open', (event) => {
|
||||||
console.debug('client WebSocket call open', this.onopen)
|
|
||||||
this.onopen?.(event)
|
this.onopen?.(event)
|
||||||
manager.add(this)
|
manager.add(this)
|
||||||
})
|
})
|
||||||
this.client.on('message', (event) => this.onmessage?.(event))
|
this.client.on('message', (event) => this.onmessage?.(event))
|
||||||
this.client.on('close', (event) => {
|
this.client.on('close', (event) => {
|
||||||
console.log('client WebSocket call close', this.onclose)
|
|
||||||
this.onclose?.(event)
|
this.onclose?.(event)
|
||||||
manager.del(this)
|
manager.del(this)
|
||||||
})
|
})
|
||||||
@ -101,3 +99,4 @@ export class WebSocket extends EventEmitter {
|
|||||||
this.removeAllListeners()
|
this.removeAllListeners()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
global.setGlobal('WebSocket', WebSocket)
|
||||||
|
@ -29,7 +29,7 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter {
|
|||||||
}
|
}
|
||||||
channelInactive(ctx: any) {
|
channelInactive(ctx: any) {
|
||||||
console.debug(`${ctx} channelInactive`)
|
console.debug(`${ctx} channelInactive`)
|
||||||
this.client.onclose({ code: 0, reason: 'server connection channel inactive!' })
|
this.client.onclose({ code: 0, reason: 'client connection channel inactive!' })
|
||||||
}
|
}
|
||||||
channelRead0(ctx: any, msg: any) {
|
channelRead0(ctx: any, msg: any) {
|
||||||
console.trace(`${ctx} channelRead0 ${msg}`)
|
console.trace(`${ctx} channelRead0 ${msg}`)
|
||||||
@ -50,7 +50,7 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter {
|
|||||||
if (frame instanceof TextWebSocketFrame) {
|
if (frame instanceof TextWebSocketFrame) {
|
||||||
this.client.onmessage({ data: frame.text() })
|
this.client.onmessage({ data: frame.text() })
|
||||||
} else if (frame instanceof CloseWebSocketFrame) {
|
} else if (frame instanceof CloseWebSocketFrame) {
|
||||||
this.client.onclose({ code: 0, reason: 'server send CloseWebSocketFrame!' })
|
this.client.onclose({ code: 0, reason: 'server close connection!' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exceptionCaught(ctx: any, cause: Error) {
|
exceptionCaught(ctx: any, cause: Error) {
|
||||||
|
@ -100,9 +100,11 @@ export class NettyWebSocket extends Transport {
|
|||||||
handler.handshakeFuture.addListener(new ChannelFutureListener((future: any) => {
|
handler.handshakeFuture.addListener(new ChannelFutureListener((future: any) => {
|
||||||
try {
|
try {
|
||||||
future.sync()
|
future.sync()
|
||||||
|
// only trigger onconnect when not have error
|
||||||
this.onconnect({})
|
this.onconnect({})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.onerror({ error })
|
// ignore error exceptionCaught from handler
|
||||||
|
// this.onerror({ error })
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}))
|
}))
|
||||||
|
@ -57,6 +57,8 @@ export abstract class Transport extends EventEmitter {
|
|||||||
this.doClose(code, reason)
|
this.doClose(code, reason)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.onerror({ error })
|
this.onerror({ error })
|
||||||
|
} finally {
|
||||||
|
this.removeAllListeners()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.debug(`${this.id} call close but state is ${this.readyStatus}`)
|
console.debug(`${this.id} call close but state is ${this.readyStatus}`)
|
||||||
@ -69,7 +71,6 @@ export abstract class Transport extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onconnect(event: Event) {
|
onconnect(event: Event) {
|
||||||
console.debug(`${this.id} call onconnect`)
|
|
||||||
if (this.readyStatus != WebSocket.OPEN) {
|
if (this.readyStatus != WebSocket.OPEN) {
|
||||||
this.readyStatus = WebSocket.OPEN
|
this.readyStatus = WebSocket.OPEN
|
||||||
this.emit('open', event)
|
this.emit('open', event)
|
||||||
|
@ -200,7 +200,7 @@ export class Client<
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private close(): void {
|
private close(): void {
|
||||||
console.debug(`client ${this.id} clise - reason: forcing transport close`)
|
console.debug(`client ${this.id} close - reason: forcing transport close`)
|
||||||
if ("open" === this.conn.readyState) {
|
if ("open" === this.conn.readyState) {
|
||||||
console.debug("forcing transport close")
|
console.debug("forcing transport close")
|
||||||
this.conn.close()
|
this.conn.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user