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 default io
|
||||||
export * from './socket-io'
|
export * from './socket-io'
|
||||||
export * from './client'
|
export * from './client'
|
||||||
export * from './netty'
|
export * from './server'
|
||||||
export * from './tomcat'
|
|
||||||
export * from './transport'
|
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'))
|
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
|
||||||
|
|
||||||
export abstract class HttpRequestHandlerAdapter {
|
export abstract class HttpRequestHandlerAdapter {
|
||||||
private _Handler;
|
private _Handler
|
||||||
constructor() {
|
constructor() {
|
||||||
let HttpRequestHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
|
let HttpRequestHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
|
||||||
|
isSharable: () => true,
|
||||||
acceptInboundMessage: (msg: any) => {
|
acceptInboundMessage: (msg: any) => {
|
||||||
return FullHttpRequestMatcher.match(msg)
|
return FullHttpRequestMatcher.match(msg)
|
||||||
},
|
},
|
||||||
channelRead0: this.channelRead0.bind(this)
|
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() {
|
getHandler() {
|
||||||
return this._Handler;
|
return this._Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ export abstract class TextWebSocketFrameHandlerAdapter {
|
|||||||
private _Handler
|
private _Handler
|
||||||
constructor() {
|
constructor() {
|
||||||
let TextWebSocketFrameHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
|
let TextWebSocketFrameHandlerAdapterImpl = Java.extend(SimpleChannelInboundHandler, {
|
||||||
|
isSharable: () => true,
|
||||||
userEventTriggered: this.userEventTriggered.bind(this),
|
userEventTriggered: this.userEventTriggered.bind(this),
|
||||||
acceptInboundMessage: (msg: any) => {
|
acceptInboundMessage: (msg: any) => {
|
||||||
return TextWebSocketFrameMatcher.match(msg)
|
return TextWebSocketFrameMatcher.match(msg)
|
||||||
|
@ -4,6 +4,7 @@ export abstract class WebSocketHandlerAdapter {
|
|||||||
private _Handler
|
private _Handler
|
||||||
constructor() {
|
constructor() {
|
||||||
let ChannelInboundHandlerAdapterImpl = Java.extend(ChannelInboundHandlerAdapter, {
|
let ChannelInboundHandlerAdapterImpl = Java.extend(ChannelInboundHandlerAdapter, {
|
||||||
|
isSharable: () => true,
|
||||||
channelRead: this.channelRead.bind(this),
|
channelRead: this.channelRead.bind(this),
|
||||||
channelInactive: this.channelInactive.bind(this),
|
channelInactive: this.channelInactive.bind(this),
|
||||||
channelUnregistered: this.exceptionCaught.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 { Namespace } from './namespace'
|
||||||
import { Client } from './client'
|
import { Client } from './client'
|
||||||
import { Parser } from './parser'
|
import { Parser } from './parser'
|
||||||
import { PacketTypes, SubPacketTypes } from './types'
|
|
||||||
import { Packet } from './packet'
|
|
||||||
import { Socket } from './socket'
|
import { Socket } from './socket'
|
||||||
import { Adapter } from './adapter'
|
import { Adapter } from './adapter'
|
||||||
import { Transport } from '../transport'
|
import { Transport } from '../transport'
|
||||||
|
Loading…
Reference in New Issue
Block a user