refactor: optimize websocket server

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2021-03-26 16:42:02 +08:00
parent 784ea2d65a
commit 6fd7174ca5
6 changed files with 60 additions and 8 deletions

View File

@ -46,6 +46,5 @@ io.Instance = Symbol("@ccms/websocket")
export default io
export * from './socket-io'
export * from './client'
export * from './netty'
export * from './tomcat'
export * from './server'
export * from './transport'

View File

@ -3,18 +3,19 @@ const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInb
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
export abstract class HttpRequestHandlerAdapter {
private _Handler;
private _Handler
constructor() {
let HttpRequestHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
isSharable: () => true,
acceptInboundMessage: (msg: any) => {
return FullHttpRequestMatcher.match(msg)
},
channelRead0: this.channelRead0.bind(this)
})
this._Handler = new HttpRequestHandlerAdapterImpl();
this._Handler = new HttpRequestHandlerAdapterImpl()
}
abstract channelRead0(ctx: any, request: any);
abstract channelRead0(ctx: any, request: any)
getHandler() {
return this._Handler;
return this._Handler
}
}

View File

@ -6,6 +6,7 @@ export abstract class TextWebSocketFrameHandlerAdapter {
private _Handler
constructor() {
let TextWebSocketFrameHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
isSharable: () => true,
userEventTriggered: this.userEventTriggered.bind(this),
acceptInboundMessage: (msg: any) => {
return TextWebSocketFrameMatcher.match(msg)

View File

@ -4,6 +4,7 @@ export abstract class WebSocketHandlerAdapter {
private _Handler
constructor() {
let ChannelInboundHandlerAdapterImpl = Java.extend(ChannelInboundHandlerAdapter, {
isSharable: () => true,
channelRead: this.channelRead.bind(this),
channelInactive: this.channelInactive.bind(this),
channelUnregistered: this.exceptionCaught.bind(this),

View File

@ -0,0 +1,52 @@
import { EventEmitter } from 'events'
import { Transport } from '../transport'
interface ServerOptions {
event?: EventEmitter
root?: string
/**
* name of the path to capture
* @default "/socket.io"
*/
path: string
}
interface WebSocketServerImpl extends EventEmitter {
close(): void
}
export class WebSocketServer extends EventEmitter {
options: Partial<ServerOptions>
private websocketServer: WebSocketServerImpl
constructor(instance: any, options: Partial<ServerOptions>) {
super()
if (!instance) { throw new Error('instance can\'t be undefiend!') }
this.options = Object.assign({
event: new EventEmitter(),
path: '/ws',
root: root + '/wwwroot',
}, options)
this.selectServerImpl(instance)
}
on(event: "connect", cb: (transport: Transport) => void): this
on(event: "message", cb: (transport: Transport, text: string) => void): this
on(event: "disconnect", cb: (transport: Transport, reason: string) => void): this
on(event: "error", cb: (transport: Transport, cause: Error) => void): this
on(event: string, cb: (transport: Transport, extra?: any) => void): this {
this.websocketServer.on(event, cb)
return this
}
private selectServerImpl(instance: any) {
let WebSocketServerImpl = undefined
if (instance.class.name.startsWith('io.netty.channel')) {
WebSocketServerImpl = require("../netty").NettyWebSocketServer
} else {
WebSocketServerImpl = require("../tomcat").TomcatWebSocketServer
}
this.websocketServer = new WebSocketServerImpl(instance, this.options)
}
}

View File

@ -4,8 +4,6 @@ import { ServerEvent } from './constants'
import { Namespace } from './namespace'
import { Client } from './client'
import { Parser } from './parser'
import { PacketTypes, SubPacketTypes } from './types'
import { Packet } from './packet'
import { Socket } from './socket'
import { Adapter } from './adapter'
import { Transport } from '../transport'