refactor: optimize websocket server
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
784ea2d65a
commit
6fd7174ca5
@ -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'
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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),
|
||||
|
52
packages/websocket/src/server/index.ts
Normal file
52
packages/websocket/src/server/index.ts
Normal 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)
|
||||
}
|
||||
}
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user