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 cd57944cb8
commit 894d5d43e6
12 changed files with 273 additions and 228 deletions

View File

@@ -1,45 +1,24 @@
import { EventEmitter } from 'events'
import { InnerClient } from '../interfaces'
import { Transport } from '../transport'
import { AttributeKeys } from './constants'
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
export class NettyClient extends EventEmitter implements InnerClient {
private _id: string
export class NettyClient extends Transport {
private channel: any
server: any
readyState: string
remoteAddress: string
upgraded: boolean
request: any
constructor(server: any, channel: any) {
super()
this.server = server
this.readyState = 'open'
super(server)
this.remoteAddress = channel.remoteAddress() + ''
this.upgraded = true
this.request = channel.attr(AttributeKeys.Request).get()
this.channel = channel
this._id = channel.id() + ''
this.channel = channel
}
get id() {
return this._id
doSend(text: string) {
this.channel.writeAndFlush(new TextWebSocketFrame(text))
}
send(text: string) {
if (this.readyState == 'open') {
this.channel.writeAndFlush(new TextWebSocketFrame(text))
} else {
console.debug(`send message ${text} to close client ${this._id}`)
}
}
close() {
if (this.readyState = 'open') {
this.channel.close()
this.readyState = 'close'
}
doClose() {
this.channel.close()
}
}