fix: websocket close logic

This commit is contained in:
MiaoWoo 2023-08-16 11:47:36 +08:00
parent a0866c1085
commit bdad4136ec
4 changed files with 10 additions and 10 deletions

View File

@ -30,7 +30,7 @@
"@ccms/common": "^0.28.0-beta.2", "@ccms/common": "^0.28.0-beta.2",
"@ccms/container": "^0.28.0-beta.2", "@ccms/container": "^0.28.0-beta.2",
"@ccms/i18n": "^0.28.0-beta.2", "@ccms/i18n": "^0.28.0-beta.2",
"@ccms/verify": "^0.25.1", "@ccms/verify": "^0.26.1",
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
} }

View File

@ -89,11 +89,11 @@ export class WebSocket extends EventEmitter {
public onclose: (event: CloseEvent) => void public onclose: (event: CloseEvent) => void
public onerror: (event: ErrorEvent) => void public onerror: (event: ErrorEvent) => void
public on(eventName: string | symbol, listener: (...args: any[]) => void): this { public on(eventName: EventType, listener: (...args: any[]) => void): this {
this.client.on(eventName, listener) this.client.on(eventName, listener)
return this return this
} }
public emit(eventName: string | symbol, ...args: any[]): boolean { public emit(eventName: EventType, ...args: any[]): boolean {
return this.client.emit(eventName, ...args) return this.client.emit(eventName, ...args)
} }

View File

@ -136,7 +136,7 @@ export class NettyWebSocket extends Transport {
console.debug(`Netty Client doClose code: ${code} reason: ${reason}`) console.debug(`Netty Client doClose code: ${code} reason: ${reason}`)
if (this.readyState == WebSocket.CLOSING) { if (this.readyState == WebSocket.CLOSING) {
if (!this._closeFrameSent) { if (!this._closeFrameSent) {
console.debug(`Netty Client doClose send close frame`) console.debug(`Netty Client doClose send close frame code: ${code} reason: ${reason}`)
this.channel?.writeAndFlush(new CloseWebSocketFrame(code, reason)) this.channel?.writeAndFlush(new CloseWebSocketFrame(code, reason))
this._closeFrameSent = true this._closeFrameSent = true
} }

View File

@ -56,11 +56,8 @@ export abstract class Transport extends EventEmitter {
this.abortHandshake(new Error(msg)); this.abortHandshake(new Error(msg));
return; return;
} }
if (this.readyState === WebSocket.CLOSING) { if (code != 1000 && (code < 3000 || code > 4999)) {
if (this._closeFrameSent && this._closeFrameReceived) { throw new Error(`The code must be either 1000, or between 3000 and 4999. ${code} is neither.`)
this.onclose({ code, reason });
}
return;
} }
this.readyState = WebSocket.CLOSING this.readyState = WebSocket.CLOSING
try { try {
@ -104,8 +101,11 @@ export abstract class Transport extends EventEmitter {
receiverClose(code: number, reason: string) { receiverClose(code: number, reason: string) {
console.debug(`Netty Handler receeve close code: ${code} reason: ${reason}`) console.debug(`Netty Handler receeve close code: ${code} reason: ${reason}`)
// if not set code then set code to 1000
if (code === -1) { code = this._closeFrameSent ? 1005 : 1001 }
this.readyState = WebSocket.CLOSING
this._closeFrameReceived = true; this._closeFrameReceived = true;
this.close(code, reason) this.doClose(code, reason)
} }
abstract getId(): string abstract getId(): string