From 78a5ed31b382623f5e31011aee2efd2458ae34b2 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 31 Mar 2020 14:53:23 +0800 Subject: [PATCH] fix: disable server error Signed-off-by: MiaoWoo --- packages/websocket/src/netty/websocket.ts | 4 +++- packages/websocket/src/server/client.ts | 2 +- packages/websocket/src/server/index.ts | 2 +- packages/websocket/src/server/websocket_detect.ts | 3 +++ packages/websocket/src/server/websocket_handler.ts | 5 ++++- packages/websocket/src/socket-io/index.ts | 12 +++++------- packages/websocket/src/socket-io/parser.ts | 8 ++++---- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/websocket/src/netty/websocket.ts b/packages/websocket/src/netty/websocket.ts index aae1b61e..ca953528 100644 --- a/packages/websocket/src/netty/websocket.ts +++ b/packages/websocket/src/netty/websocket.ts @@ -4,11 +4,13 @@ export abstract class WebSocketHandlerAdapter { private _Handler; constructor() { let ChannelInboundHandlerAdapterImpl = Java.extend(ChannelInboundHandlerAdapter, { - channelRead: this.channelRead.bind(this) + channelRead: this.channelRead.bind(this), + exceptionCaught: this.exceptionCaught.bind(this) }) this._Handler = new ChannelInboundHandlerAdapterImpl() } abstract channelRead(ctx: any, channel: any); + abstract exceptionCaught(ctx: any, cause: Error); getHandler() { return this._Handler; } diff --git a/packages/websocket/src/server/client.ts b/packages/websocket/src/server/client.ts index 91c1ac6c..0fa2f45d 100644 --- a/packages/websocket/src/server/client.ts +++ b/packages/websocket/src/server/client.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events' import { SocketIO } from 'socket-io/interfaces'; -import { Keys, AttributeKeys } from './constants'; +import { AttributeKeys } from './constants'; const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame') diff --git a/packages/websocket/src/server/index.ts b/packages/websocket/src/server/index.ts index ee41c1b2..ed59a8f3 100644 --- a/packages/websocket/src/server/index.ts +++ b/packages/websocket/src/server/index.ts @@ -4,7 +4,7 @@ import { ServerEvent, Keys } from './constants' import { WebSocketDetect } from './websocket_detect' import { WebSocketHandler } from './websocket_handler' import { NettyClient } from './client' -import { ServerOptions, Server } from '../socket-io' +import { ServerOptions } from '../socket-io' class NettyWebSocketServer extends EventEmitter { private pipeline: any; diff --git a/packages/websocket/src/server/websocket_detect.ts b/packages/websocket/src/server/websocket_detect.ts index 620be384..db5edc49 100644 --- a/packages/websocket/src/server/websocket_detect.ts +++ b/packages/websocket/src/server/websocket_detect.ts @@ -11,4 +11,7 @@ export class WebSocketDetect extends WebSocketHandlerAdapter { channelRead(ctx: any, channel: any) { this.event.emit(ServerEvent.detect, ctx, channel); } + exceptionCaught(ctx: any, cause: Error) { + this.event.emit(ServerEvent.error, ctx, cause); + } } diff --git a/packages/websocket/src/server/websocket_handler.ts b/packages/websocket/src/server/websocket_handler.ts index 02d7b8b8..886048f7 100644 --- a/packages/websocket/src/server/websocket_handler.ts +++ b/packages/websocket/src/server/websocket_handler.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events' -import { Keys } from './constants' +import { Keys, ServerEvent } from './constants' import { WebSocketHandlerAdapter } from "../netty" import { HttpRequestHandler } from './httprequest' import { TextWebSocketFrameHandler } from './text_websocket_frame' @@ -40,4 +40,7 @@ export class WebSocketHandler extends WebSocketHandlerAdapter { msg.resetReaderIndex() ctx.fireChannelRead(msg) } + exceptionCaught(ctx: any, cause: Error) { + this.options.event.emit(ServerEvent.error, ctx, cause) + } } diff --git a/packages/websocket/src/socket-io/index.ts b/packages/websocket/src/socket-io/index.ts index 33d2d2e7..efd1e641 100644 --- a/packages/websocket/src/socket-io/index.ts +++ b/packages/websocket/src/socket-io/index.ts @@ -17,12 +17,6 @@ interface ServerOptions extends SocketIO.ServerOptions { root?: string; } -const defaultOptions: ServerOptions = { - event: new EventEmitter(), - path: '/socket.io', - root: root + '/wwwroot' -} - class Server implements SocketIO.Server { private nettyServer: NettyWebSocketServer; private allClients: { [key: string]: Client }; @@ -43,7 +37,11 @@ class Server implements SocketIO.Server { this.nsps = {}; this.sockets = new Namespace('/', this); this.nsps['/'] = this.sockets; - this.initNettyServer(pipeline, Object.assign(defaultOptions, options)); + this.initNettyServer(pipeline, Object.assign({ + event: new EventEmitter(), + path: '/socket.io', + root: root + '/wwwroot' + }, options)); } checkRequest(req: any, fn: (err: any, success: boolean) => void): void { diff --git a/packages/websocket/src/socket-io/parser.ts b/packages/websocket/src/socket-io/parser.ts index 871ad489..44df4cc7 100644 --- a/packages/websocket/src/socket-io/parser.ts +++ b/packages/websocket/src/socket-io/parser.ts @@ -39,10 +39,10 @@ export class Parser { return '4"encode error"' } } - console.debug(`encoded ${origin} as ${str}`); + console.trace(`encoded ${origin} as ${str}`); return str; } - tryStringify(str) { + tryStringify(str: any) { try { return JSON.stringify(str); } catch (e) { @@ -136,11 +136,11 @@ export class Parser { } } - console.debug(`decoded ${str} as ${JSON.stringify(p)}`); + console.trace(`decoded ${str} as ${JSON.stringify(p)}`); return p; } - tryParse(str) { + tryParse(str: string) { try { return JSON.parse(str); } catch (e) {