feat: export error handle & merge config

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-03-24 18:30:50 +08:00
parent 73b9c2f163
commit 49b96b3ac4
7 changed files with 44 additions and 41 deletions

View File

@@ -4,17 +4,18 @@ import { ServerEvent, Keys } from './constants'
import { WebSocketDetect } from './websocket_detect'
import { WebSocketHandler } from './websocket_handler'
import { NettyClient } from './client'
import { NettyWebSocketServerOptions } from './config'
import { ServerOptions, Server } from '../socket-io'
class NettyWebSocketServer extends EventEmitter {
private pipeline: any;
private allClients: { [key: string]: NettyClient };
constructor(pipeline: any, options: NettyWebSocketServerOptions) {
constructor(pipeline: any, options: ServerOptions) {
super()
this.allClients = {};
this.pipeline = pipeline;
let connectEvent = options.event;
try { this.pipeline.remove(Keys.Detect) } catch (error) { }
this.pipeline.addFirst(Keys.Detect, new WebSocketDetect(connectEvent).getHandler())
connectEvent.on(ServerEvent.detect, (ctx, channel) => {
channel.pipeline().addFirst(Keys.Handler, new WebSocketHandler(options).getHandler())
@@ -26,8 +27,10 @@ class NettyWebSocketServer extends EventEmitter {
this.emit(ServerEvent.connect, nettyClient);
})
connectEvent.on(ServerEvent.message, (ctx, msg) => {
let channel = ctx.channel();
this.emit(ServerEvent.message, this.allClients[channel.id()], msg.text())
this.emit(ServerEvent.message, this.allClients[ctx.channel().id()], msg.text())
})
connectEvent.on(ServerEvent.error, (ctx, cause) => {
this.emit(ServerEvent.error, this.allClients[ctx.channel().id()], cause)
})
}
close() {