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

@ -12,8 +12,12 @@ export abstract class WebSocketHandlerAdapter {
this._Handler = new ChannelInboundHandlerAdapterImpl()
}
abstract channelRead(ctx: any, channel: any)
abstract channelInactive(ctx: any)
abstract channelUnregistered(ctx: any)
channelInactive(ctx: any) {
ctx.fireChannelInactive()
}
channelUnregistered(ctx: any) {
ctx.fireChannelUnregistered()
}
abstract exceptionCaught(ctx: any, cause: Error)
getHandler() {
return this._Handler

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()
}
}

View File

@ -11,16 +11,6 @@ export class WebSocketDetect extends WebSocketHandlerAdapter {
channelRead(ctx: any, channel: any) {
this.event.emit(ServerEvent.detect, ctx, channel)
}
channelInactive(ctx: any) {
console.debug('WebSocketDetect channelUnregistered ' + ctx)
this.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
ctx.channelInactive()
}
channelUnregistered(ctx: any) {
console.debug('WebSocketDetect channelUnregistered ' + ctx)
this.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
ctx.fireChannelUnregistered()
}
exceptionCaught(ctx: any, cause: Error) {
this.event.emit(ServerEvent.error, ctx, cause)
}

View File

@ -43,14 +43,14 @@ export class WebSocketHandler extends WebSocketHandlerAdapter {
channelInactive(ctx: any) {
console.debug('WebSocketHandler channelInactive ' + ctx)
this.options.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
ctx.channelInactive()
this.options.event.emit(ServerEvent.disconnect, ctx, 'netty channelInactive')
super.channelInactive(ctx)
}
channelUnregistered(ctx: any) {
console.debug('WebSocketHandler channelUnregistered ' + ctx)
this.options.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
ctx.fireChannelUnregistered()
this.options.event.emit(ServerEvent.disconnect, ctx, 'netty channelUnregistered')
super.channelUnregistered(ctx)
}
exceptionCaught(ctx: any, cause: Error) {