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

@ -1,6 +0,0 @@
import { EventEmitter } from 'events'
export interface NettyWebSocketServerOptions {
event: EventEmitter,
path?: string;
}

View File

@ -3,6 +3,7 @@ export enum ServerEvent {
connect = 'connect',
connection = 'connection',
message = 'message',
error = 'error',
disconnect = 'disconnect'
}

View File

@ -1,5 +1,6 @@
import { HttpRequestHandlerAdapter } from '../netty'
import { Keys, AttributeKeys } from './constants'
import { AttributeKeys } from './constants'
import { ServerOptions } from 'socket-io'
const DefaultHttpResponse = Java.type('io.netty.handler.codec.http.DefaultHttpResponse')
const DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse')
@ -14,21 +15,13 @@ const RandomAccessFile = Java.type('java.io.RandomAccessFile')
const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion')
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener')
export type HttpRequestConfig = {
root?: string;
ws?: string;
}
export class HttpRequestHandler extends HttpRequestHandlerAdapter {
private ws: string;
private root: string;
constructor(config: HttpRequestConfig = {
root: root + '/wwwroot',
ws: '/ws'
}) {
constructor(options: ServerOptions) {
super()
this.root = config.root;
this.ws = config.ws;
this.root = options.root;
this.ws = options.path;
}
channelRead0(ctx: any, request: any) {
if (request.getUri().startsWith(this.ws)) {

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

View File

@ -1,11 +1,11 @@
import { TextWebSocketFrameHandlerAdapter } from '../netty'
import { EventEmitter } from 'events'
import { ServerEvent } from './constants'
import { NettyWebSocketServerOptions } from './config';
import { ServerOptions } from '../socket-io';
export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter {
private event: EventEmitter;
constructor(options: NettyWebSocketServerOptions) {
constructor(options: ServerOptions) {
super()
this.event = options.event;
}
@ -18,6 +18,6 @@ export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter
this.event.emit(ServerEvent.message, ctx, msg)
}
exceptionCaught(ctx: any, cause: Error) {
console.ex(cause)
this.event.emit(ServerEvent.error, ctx, cause)
}
}

View File

@ -4,7 +4,7 @@ import { Keys } from './constants'
import { WebSocketHandlerAdapter } from "../netty"
import { HttpRequestHandler } from './httprequest'
import { TextWebSocketFrameHandler } from './text_websocket_frame'
import { NettyWebSocketServerOptions } from './config'
import { ServerOptions } from '../socket-io'
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
@ -14,8 +14,8 @@ const HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAg
const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler')
export class WebSocketHandler extends WebSocketHandlerAdapter {
private options: NettyWebSocketServerOptions;
constructor(options: NettyWebSocketServerOptions) {
private options: ServerOptions;
constructor(options: ServerOptions) {
super()
this.options = options;
}
@ -32,7 +32,7 @@ export class WebSocketHandler extends WebSocketHandlerAdapter {
pipeline.addLast('http', new HttpServerCodec())
pipeline.addLast('chunk', new ChunkedWriteHandler())
pipeline.addLast('httpobj', new HttpObjectAggregator(64 * 1024))
pipeline.addLast('http_request', new HttpRequestHandler().getHandler())
pipeline.addLast('http_request', new HttpRequestHandler(this.options).getHandler())
pipeline.addLast('websocket', new WebSocketServerProtocolHandler(this.options.path, true))
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler(this.options).getHandler())
}