2020-05-14 11:04:19 +00:00
|
|
|
import { ServerOptions } from '../socket-io'
|
2020-05-26 07:45:40 +00:00
|
|
|
import { ServerEvent } from '../socket-io/constants'
|
|
|
|
|
|
|
|
import { Keys } from './constants'
|
2020-03-21 07:47:42 +00:00
|
|
|
import { HttpRequestHandler } from './httprequest'
|
2020-11-18 08:21:56 +00:00
|
|
|
import { WebSocketHandlerAdapter } from "./adapter"
|
2020-03-21 07:47:42 +00:00
|
|
|
import { TextWebSocketFrameHandler } from './text_websocket_frame'
|
|
|
|
|
|
|
|
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
|
|
|
|
const HttpServerCodec = Java.type('io.netty.handler.codec.http.HttpServerCodec')
|
|
|
|
const ChunkedWriteHandler = Java.type('io.netty.handler.stream.ChunkedWriteHandler')
|
|
|
|
const HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAggregator')
|
|
|
|
const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler')
|
|
|
|
|
|
|
|
export class WebSocketHandler extends WebSocketHandlerAdapter {
|
2020-11-13 01:40:47 +00:00
|
|
|
private options: ServerOptions
|
2020-03-24 10:30:50 +00:00
|
|
|
constructor(options: ServerOptions) {
|
2020-03-21 07:47:42 +00:00
|
|
|
super()
|
2020-11-13 01:40:47 +00:00
|
|
|
this.options = options
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
channelRead(ctx: any, msg: any) {
|
|
|
|
msg.markReaderIndex()
|
|
|
|
let message: string = msg.toString(CharsetUtil.UTF_8)
|
|
|
|
let channel = ctx.channel()
|
|
|
|
let pipeline = channel.pipeline()
|
|
|
|
if (message.indexOf('HTTP/1.1') > 0) {
|
|
|
|
pipeline.names().forEach(f => {
|
|
|
|
if (f == Keys.Handler || f.indexOf(Keys.Default) > -1) { return }
|
|
|
|
pipeline.remove(f)
|
|
|
|
})
|
|
|
|
pipeline.addLast('http', new HttpServerCodec())
|
|
|
|
pipeline.addLast('chunk', new ChunkedWriteHandler())
|
|
|
|
pipeline.addLast('httpobj', new HttpObjectAggregator(64 * 1024))
|
2020-03-24 10:30:50 +00:00
|
|
|
pipeline.addLast('http_request', new HttpRequestHandler(this.options).getHandler())
|
2020-11-13 01:40:47 +00:00
|
|
|
// this.options.path, null, false, 655360, false, true, false, 10000
|
2020-03-22 16:58:53 +00:00
|
|
|
pipeline.addLast('websocket', new WebSocketServerProtocolHandler(this.options.path, true))
|
|
|
|
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler(this.options).getHandler())
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
pipeline.remove(Keys.Handler)
|
|
|
|
msg.resetReaderIndex()
|
|
|
|
ctx.fireChannelRead(msg)
|
|
|
|
}
|
2020-11-13 01:40:47 +00:00
|
|
|
|
2020-11-18 08:21:56 +00:00
|
|
|
channelInactive(ctx: any) {
|
|
|
|
console.debug('WebSocketHandler channelInactive ' + ctx)
|
|
|
|
this.options.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
|
|
|
|
ctx.channelInactive()
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:40:47 +00:00
|
|
|
channelUnregistered(ctx: any) {
|
2020-11-18 08:21:56 +00:00
|
|
|
console.debug('WebSocketHandler channelUnregistered ' + ctx)
|
2020-11-13 01:40:47 +00:00
|
|
|
this.options.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
|
|
|
|
ctx.fireChannelUnregistered()
|
|
|
|
}
|
|
|
|
|
2020-03-31 06:53:23 +00:00
|
|
|
exceptionCaught(ctx: any, cause: Error) {
|
|
|
|
this.options.event.emit(ServerEvent.error, ctx, cause)
|
|
|
|
}
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|