refactor(websocket): upgrade socket.io
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { SocketIO } from '../socket-io/interfaces'
|
||||
import { AttributeKeys } from './constants'
|
||||
|
||||
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
||||
|
||||
export class NettyClient extends EventEmitter implements SocketIO.EngineSocket {
|
||||
private _id: string
|
||||
private channel: any
|
||||
|
||||
server: any
|
||||
readyState: string
|
||||
remoteAddress: string
|
||||
upgraded: boolean
|
||||
request: any
|
||||
transport: any
|
||||
|
||||
constructor(server: any, channel: any) {
|
||||
super()
|
||||
this.server = server
|
||||
this.readyState = 'open'
|
||||
this.remoteAddress = channel.remoteAddress() + ''
|
||||
this.upgraded = true
|
||||
this.request = channel.attr(AttributeKeys.Request).get()
|
||||
this.transport = null
|
||||
|
||||
this.channel = channel
|
||||
this._id = channel.id() + ''
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id
|
||||
}
|
||||
send(text: string) {
|
||||
this.channel.writeAndFlush(new TextWebSocketFrame(text))
|
||||
}
|
||||
close() {
|
||||
this.channel.close()
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
const AttributeKey = Java.type('io.netty.util.AttributeKey');
|
||||
|
||||
export enum Keys {
|
||||
Detect = "miao_detect",
|
||||
Handler = "miaowebsocket",
|
||||
Default = "DefaultChannelPipeline"
|
||||
}
|
||||
|
||||
let RequestAttributeKey: any
|
||||
try { RequestAttributeKey = AttributeKey.valueOf('request') } catch (error) { }
|
||||
export enum AttributeKeys {
|
||||
Request = RequestAttributeKey
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
import { HttpRequestHandlerAdapter } from '../netty'
|
||||
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')
|
||||
const HttpHeaders = Java.type('io.netty.handler.codec.http.HttpHeaders')
|
||||
const HttpVersion = Java.type('io.netty.handler.codec.http.HttpVersion')
|
||||
const HttpResponseStatus = Java.type('io.netty.handler.codec.http.HttpResponseStatus')
|
||||
const LastHttpContent = Java.type('io.netty.handler.codec.http.LastHttpContent')
|
||||
|
||||
const File = Java.type('java.io.File')
|
||||
const Runnable = Java.type('java.lang.Runnable')
|
||||
const RandomAccessFile = Java.type('java.io.RandomAccessFile')
|
||||
const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion')
|
||||
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener')
|
||||
|
||||
export class HttpRequestHandler extends HttpRequestHandlerAdapter {
|
||||
private ws: string;
|
||||
private root: string;
|
||||
constructor(options: ServerOptions) {
|
||||
super()
|
||||
this.root = options.root;
|
||||
this.ws = options.path;
|
||||
}
|
||||
channelRead0(ctx: any, request: any) {
|
||||
if (request.getUri().startsWith(this.ws)) {
|
||||
ctx.channel().attr(AttributeKeys.Request).set(request);
|
||||
ctx.fireChannelRead(request.retain())
|
||||
} else {
|
||||
ctx.executor().execute(new Runnable({
|
||||
run: () => {
|
||||
if (HttpHeaders.is100ContinueExpected(request)) {
|
||||
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE))
|
||||
}
|
||||
let filename = request.getUri().split('?')[0].substr(1)
|
||||
let file = new File(this.root, filename || 'index.html')
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND))
|
||||
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE)
|
||||
return
|
||||
}
|
||||
let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK)
|
||||
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html charset=UTF-8")
|
||||
let raf = new RandomAccessFile(file, 'r')
|
||||
let keepAlive = HttpHeaders.isKeepAlive(request)
|
||||
if (keepAlive) {
|
||||
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length())
|
||||
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE)
|
||||
}
|
||||
ctx.write(response)
|
||||
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length()))
|
||||
let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)
|
||||
if (!keepAlive) {
|
||||
future.addListener(ChannelFutureListener.CLOSE)
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
import { ServerOptions } from '../socket-io'
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
|
||||
import { NettyClient } from './client'
|
||||
import { Keys } from './constants'
|
||||
import { WebSocketDetect } from './websocket_detect'
|
||||
import { WebSocketHandler } from './websocket_handler'
|
||||
|
||||
class NettyWebSocketServer extends EventEmitter {
|
||||
private pipeline: any
|
||||
private clients: Map<string, NettyClient>
|
||||
|
||||
constructor(pipeline: any, options: ServerOptions) {
|
||||
super()
|
||||
this.clients = new Map()
|
||||
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())
|
||||
ctx.fireChannelRead(channel)
|
||||
})
|
||||
connectEvent.on(ServerEvent.connect, (ctx) => {
|
||||
let cid = ctx?.channel().id() + ''
|
||||
let nettyClient = new NettyClient(this, ctx.channel())
|
||||
this.clients.set(cid, nettyClient)
|
||||
this.emit(ServerEvent.connect, nettyClient)
|
||||
})
|
||||
connectEvent.on(ServerEvent.message, (ctx, msg) => {
|
||||
let cid = ctx?.channel().id() + ''
|
||||
if (this.clients.has(cid)) {
|
||||
this.emit(ServerEvent.message, this.clients.get(cid), msg.text())
|
||||
} else {
|
||||
console.error(`unknow client ${ctx} reciver message ${msg.text()}`)
|
||||
}
|
||||
})
|
||||
connectEvent.on(ServerEvent.disconnect, (ctx, cause) => {
|
||||
let cid = ctx?.channel().id() + ''
|
||||
if (this.clients.has(cid)) {
|
||||
this.emit(ServerEvent.disconnect, this.clients.get(cid), cause)
|
||||
} else {
|
||||
console.error(`unknow client ${ctx} disconnect cause ${cause}`)
|
||||
}
|
||||
})
|
||||
connectEvent.on(ServerEvent.error, (ctx, cause) => {
|
||||
let cid = ctx?.channel().id() + ''
|
||||
if (this.clients.has(cid)) {
|
||||
this.emit(ServerEvent.error, this.clients.get(cid), cause)
|
||||
} else {
|
||||
console.error(`unknow client ${ctx} cause error ${cause}`)
|
||||
console.ex(cause)
|
||||
}
|
||||
})
|
||||
}
|
||||
close() {
|
||||
if (this.pipeline.names().contains(Keys.Detect)) {
|
||||
this.pipeline.remove(Keys.Detect)
|
||||
}
|
||||
this.clients.forEach(client => client.close())
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
NettyWebSocketServer,
|
||||
ServerEvent,
|
||||
NettyClient
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { ServerOptions } from '../socket-io';
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
import { TextWebSocketFrameHandlerAdapter } from '../netty'
|
||||
|
||||
export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter {
|
||||
private event: EventEmitter;
|
||||
constructor(options: ServerOptions) {
|
||||
super()
|
||||
this.event = options.event;
|
||||
}
|
||||
userEventTriggered(ctx: any, evt: any) {
|
||||
if (evt == 'HANDSHAKE_COMPLETE') {
|
||||
this.event.emit(ServerEvent.connect, ctx)
|
||||
}
|
||||
}
|
||||
channelRead0(ctx: any, msg: any) {
|
||||
this.event.emit(ServerEvent.message, ctx, msg)
|
||||
}
|
||||
exceptionCaught(ctx: any, cause: Error) {
|
||||
this.event.emit(ServerEvent.error, ctx, cause)
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { WebSocketHandlerAdapter } from "../netty"
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
|
||||
export class WebSocketDetect extends WebSocketHandlerAdapter {
|
||||
private event: EventEmitter
|
||||
constructor(event: EventEmitter) {
|
||||
super()
|
||||
this.event = event
|
||||
}
|
||||
channelRead(ctx: any, channel: any) {
|
||||
this.event.emit(ServerEvent.detect, ctx, channel)
|
||||
}
|
||||
channelUnregistered(ctx: any) {
|
||||
this.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
|
||||
ctx.fireChannelUnregistered()
|
||||
}
|
||||
exceptionCaught(ctx: any, cause: Error) {
|
||||
this.event.emit(ServerEvent.error, ctx, cause)
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import { ServerOptions } from '../socket-io'
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
|
||||
import { Keys } from './constants'
|
||||
import { HttpRequestHandler } from './httprequest'
|
||||
import { WebSocketHandlerAdapter } from "../netty"
|
||||
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 {
|
||||
private options: ServerOptions
|
||||
constructor(options: ServerOptions) {
|
||||
super()
|
||||
this.options = options
|
||||
}
|
||||
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))
|
||||
pipeline.addLast('http_request', new HttpRequestHandler(this.options).getHandler())
|
||||
// this.options.path, null, false, 655360, false, true, false, 10000
|
||||
pipeline.addLast('websocket', new WebSocketServerProtocolHandler(this.options.path, true))
|
||||
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler(this.options).getHandler())
|
||||
}
|
||||
pipeline.remove(Keys.Handler)
|
||||
msg.resetReaderIndex()
|
||||
ctx.fireChannelRead(msg)
|
||||
}
|
||||
|
||||
channelUnregistered(ctx: any) {
|
||||
this.options.event.emit(ServerEvent.disconnect, ctx, 'client disconnect')
|
||||
ctx.fireChannelUnregistered()
|
||||
}
|
||||
|
||||
exceptionCaught(ctx: any, cause: Error) {
|
||||
this.options.event.emit(ServerEvent.error, ctx, cause)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user