From b1f1f9837ed9de2aa295c38b683cecf3700c0441 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 1 Feb 2020 02:47:39 +0800 Subject: [PATCH] feat: add websocket package Signed-off-by: MiaoWoo --- packages/websocket/package.json | 34 +++++++++++++++++++ packages/websocket/src/index.ts | 1 + packages/websocket/src/netty/httprequest.ts | 21 ++++++++++++ .../src/netty/text_websocket_frame.ts | 23 +++++++++++++ packages/websocket/src/netty/websocket.ts | 18 ++++++++++ packages/websocket/tsconfig.json | 7 ++++ 6 files changed, 104 insertions(+) create mode 100644 packages/websocket/package.json create mode 100644 packages/websocket/src/index.ts create mode 100644 packages/websocket/src/netty/httprequest.ts create mode 100644 packages/websocket/src/netty/text_websocket_frame.ts create mode 100644 packages/websocket/src/netty/websocket.ts create mode 100644 packages/websocket/tsconfig.json diff --git a/packages/websocket/package.json b/packages/websocket/package.json new file mode 100644 index 00000000..033bb3dc --- /dev/null +++ b/packages/websocket/package.json @@ -0,0 +1,34 @@ +{ + "name": "@ms/websocket", + "version": "0.1.1", + "description": "MiaoScript api package", + "keywords": [ + "miaoscript", + "minecraft", + "bukkit", + "sponge" + ], + "author": "MiaoWoo ", + "homepage": "https://github.com/circlecloud/ms.git", + "license": "ISC", + "main": "dist/index.js", + "publishConfig": { + "registry": "https://repo.yumc.pw/repository/npm-hosted/" + }, + "scripts": { + "clean": "rimraf dist", + "watch": "npx tsc --watch", + "build": "yarn clean && npx tsc", + "test": "echo \"Error: run tests from root\" && exit 1" + }, + "devDependencies": { + "reflect-metadata": "^0.1.13", + "rimraf": "^3.0.0", + "typescript": "^3.7.4" + }, + "dependencies": { + "@ms/api": "^0.1.1", + "@ms/common": "^0.1.0" + }, + "gitHead": "781524f83e52cad26d7c480513e3c525df867121" +} \ No newline at end of file diff --git a/packages/websocket/src/index.ts b/packages/websocket/src/index.ts new file mode 100644 index 00000000..d1bccc19 --- /dev/null +++ b/packages/websocket/src/index.ts @@ -0,0 +1 @@ +import '@ms/nashorn' diff --git a/packages/websocket/src/netty/httprequest.ts b/packages/websocket/src/netty/httprequest.ts new file mode 100644 index 00000000..4d8edc72 --- /dev/null +++ b/packages/websocket/src/netty/httprequest.ts @@ -0,0 +1,21 @@ +/// + +const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher') +const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler') +const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest')) + +export default abstract class HttpRequestHandlerAdapter { + private _Handler; + constructor() { + this._Handler == Java.extend(SimpleChannelInboundHandler, { + acceptInboundMessage: (msg: any) => { + return FullHttpRequestMatcher.match(msg) + }, + channelRead0: this.channelRead0 + }) + } + abstract channelRead0(ctx: any, msg: any); + getHandler() { + return this._Handler; + } +} \ No newline at end of file diff --git a/packages/websocket/src/netty/text_websocket_frame.ts b/packages/websocket/src/netty/text_websocket_frame.ts new file mode 100644 index 00000000..88cf4022 --- /dev/null +++ b/packages/websocket/src/netty/text_websocket_frame.ts @@ -0,0 +1,23 @@ +/// + +const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher') +const TextWebSocketFrameMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')) +const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler') + +export default abstract class TextWebSocketFrameHandlerAdapter { + private _Handler; + constructor() { + this._Handler == Java.extend(SimpleChannelInboundHandler, { + userEventTriggered: this.userEventTriggered, + acceptInboundMessage: (msg: any) => { + return TextWebSocketFrameMatcher.match(msg) + }, + channelRead0: this.channelRead0 + }) + } + abstract userEventTriggered(ctx: any, evt: any); + abstract channelRead0(ctx: any, msg: any); + getHandler() { + return this._Handler; + } +} diff --git a/packages/websocket/src/netty/websocket.ts b/packages/websocket/src/netty/websocket.ts new file mode 100644 index 00000000..f6648426 --- /dev/null +++ b/packages/websocket/src/netty/websocket.ts @@ -0,0 +1,18 @@ +import '@ms/api' + +const MiaoWebSocket = 'miaowebsocket' +const CharsetUtil = Java.type('io.netty.util.CharsetUtil') +const ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter') + +export default abstract class WebSocketHandlerAdapter { + private _Handler; + constructor() { + this._Handler = Java.extend(ChannelInboundHandlerAdapter, { + channelRead: this.channelRead + }) + } + abstract channelRead(ctx: any, msg: any); + getHandler() { + return this._Handler; + } +} diff --git a/packages/websocket/tsconfig.json b/packages/websocket/tsconfig.json new file mode 100644 index 00000000..7aae5d2b --- /dev/null +++ b/packages/websocket/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file