fix: disable server error

Signed-off-by: MiaoWoo <admin@yumc.pw>
clean
MiaoWoo 2020-03-31 14:53:23 +08:00
parent a2c3eff622
commit ec65d00f28
7 changed files with 21 additions and 15 deletions

View File

@ -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;
}

View File

@ -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')

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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) {