perf: optimize websocket logic

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-11-20 14:21:57 +08:00
parent f31726e98b
commit af1e64767a
12 changed files with 273 additions and 228 deletions

View File

@ -0,0 +1,35 @@
import { EventEmitter } from 'events'
export abstract class Transport extends EventEmitter {
protected _id: string
server: any
readyState: 'opening' | 'open' | 'closing' | 'closed'
remoteAddress: string
upgraded: boolean
request: any
constructor(server: any) {
super()
this.server = server
this.readyState = 'open'
this.upgraded = true
}
get id() {
return this._id
}
send(text: string) {
if (this.readyState == 'open') {
this.doSend(text)
} else {
console.debug(`send message ${text} to close client ${this._id}`)
}
}
close() {
if ("closed" === this.readyState || "closing" === this.readyState) { return }
this.doClose()
this.readyState = 'closed'
}
abstract doSend(text: string)
abstract doClose()
}