feat: update websocket server
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
ffa62ee243
commit
64111732fb
@ -29,6 +29,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ms/plugin": "^0.1.1",
|
"@ms/plugin": "^0.1.1",
|
||||||
|
"axios": "^0.19.0",
|
||||||
"es6-map": "^0.1.5",
|
"es6-map": "^0.1.5",
|
||||||
"inversify": "^5.0.1"
|
"inversify": "^5.0.1"
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<!-- QQ应用模式 -->
|
<!-- QQ应用模式 -->
|
||||||
<meta name="x5-page-mode" content="app">
|
<meta name="x5-page-mode" content="app">
|
||||||
<title>MiaoConsole 调试工具</title>
|
<title>MiaoConsole 调试工具</title>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/avalon2@2.2.10/dist/avalon.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/avalon2@2.2.10/dist/avalon.min.js"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
|
||||||
@ -58,11 +59,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="progress progress-striped active" :visible='@classes.total != @classes.loaded'>
|
<div class="progress progress-striped active" :visible='@classes.total != @classes.loaded'>
|
||||||
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="60" aria-valuemin="0"
|
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="60" aria-valuemin="0"
|
||||||
aria-valuemax="100" :css="{width: (@classes.loaded/@classes.total*100) +'%', 'min-width': '30em'}">
|
aria-valuemax="100" :css="{width: (@classes.loaded/@classes.total*100) +'%', 'min-width': '20em'}">
|
||||||
<span>正在加载 {{@type}} 类型定义 {{@classes.loaded}}/{{@classes.total}}({{~~(@classes.loaded/@classes.total*100)|percent}}%) </span>
|
<span>正在加载 {{@type}} 类型定义 {{@classes.loaded}}/{{@classes.total}}({{~~(@classes.loaded/@classes.total*100)|percent}}%) </span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel panel-info" :visible='@classes.total == @classes.loaded'>
|
<div class="panel panel-info" :visible='@classes.total == @classes.loaded && @classes.total != 0'>
|
||||||
<div class="panel-heading">代码编辑器</div>
|
<div class="panel-heading">代码编辑器</div>
|
||||||
<div id="editor" style="height: 450px; width:100%;"></div>
|
<div id="editor" style="height: 450px; width:100%;"></div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4,29 +4,32 @@ let codeStorageKey = "MiaoScript:code";
|
|||||||
let monaco_path = 'https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min'
|
let monaco_path = 'https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min'
|
||||||
require.config({ paths: { 'vs': monaco_path + '/vs' } });
|
require.config({ paths: { 'vs': monaco_path + '/vs' } });
|
||||||
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
|
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
|
||||||
let proxy = URL.createObjectURL(new Blob([`
|
let proxy = URL.createObjectURL(new Blob([`self.MonacoEnvironment = {baseUrl: '${monaco_path}/'};
|
||||||
self.MonacoEnvironment = {
|
importScripts('${monaco_path}/vs/base/worker/workerMain.js');`], { type: 'text/javascript' }));
|
||||||
baseUrl: '${monaco_path}/'
|
|
||||||
};
|
|
||||||
importScripts('${monaco_path}/vs/base/worker/workerMain.js');
|
|
||||||
`], { type: 'text/javascript' }));
|
|
||||||
|
|
||||||
require(["vs/editor/editor.main"], function() {
|
function loadExtraLibs(ts_d_src, filter) {
|
||||||
if (main.type !== 'unknow') {
|
let count = 0;
|
||||||
let ts_d_src = `https://cdn.jsdelivr.net/gh/circlecloud/ms@master/packages/${main.type}/src/typings`
|
axios.get(`${ts_d_src}/index.d.ts`).then(async result => {
|
||||||
$.get(`${ts_d_src}/index.ts`, (res) => {
|
monaco.languages.typescript.javascriptDefaults.addExtraLib(result.data, 'file:///src/typings/index.d.ts')
|
||||||
monaco.languages.typescript.javascriptDefaults.addExtraLib(res, 'file:///src/typings/index.ts')
|
let classes = result.data.split('\n').map(line => line.match(/.*\.\/(.*)".*/)).filter(line => line).map(dts => dts[1])
|
||||||
let classes = res.split('\n').map(line => line.match(/.*\.\/(.*)".*/)).filter(line => line).map(dts => dts[1])
|
if (filter) {
|
||||||
main.classes.total = classes.length
|
classes = classes.filter(line => filter(line))
|
||||||
main.classes.loaded = 0
|
|
||||||
classes.forEach(fname => {
|
|
||||||
$.get(`${ts_d_src}/${fname}`, content => {
|
|
||||||
monaco.languages.typescript.javascriptDefaults.addExtraLib(content, `file:///src/typings/${fname}`)
|
|
||||||
main.classes.loaded++
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
main.classes.total += classes.length
|
||||||
|
for (let fname of classes) {
|
||||||
|
if (count++ % 50 == 0) { await axios.get(`${ts_d_src}/${fname}`) }
|
||||||
|
loadExtraLib(`${ts_d_src}/${fname}`, `file:///src/typings/${fname}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadExtraLib(url, file) {
|
||||||
|
axios.get(url).then(result => monaco.languages.typescript.javascriptDefaults.addExtraLib(result.data, file)).finally(() => main.classes.loaded++)
|
||||||
|
}
|
||||||
|
|
||||||
|
require(["vs/editor/editor.main"], async function() {
|
||||||
|
main.classes.total = 0
|
||||||
|
main.classes.loaded = 0
|
||||||
editor = monaco.editor.create(document.getElementById('editor'), {
|
editor = monaco.editor.create(document.getElementById('editor'), {
|
||||||
value: window.localStorage.getItem(codeStorageKey) || 'org.bukkit.Bukkit.server.version',
|
value: window.localStorage.getItem(codeStorageKey) || 'org.bukkit.Bukkit.server.version',
|
||||||
language: 'javascript',
|
language: 'javascript',
|
||||||
@ -47,6 +50,10 @@ require(["vs/editor/editor.main"], function() {
|
|||||||
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_Q, function() {
|
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_Q, function() {
|
||||||
console.log('switch')
|
console.log('switch')
|
||||||
})
|
})
|
||||||
|
loadExtraLibs(`https://cdn.jsdelivr.net/gh/circlecloud/ms@master/packages/types/dist/typings/jdk`, (line) => line.startsWith('java.lang'))
|
||||||
|
if (main.type !== 'unknow') {
|
||||||
|
loadExtraLibs(`https://cdn.jsdelivr.net/gh/circlecloud/ms@master/packages/types/dist/typings/${main.type}`)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
function getSelectContent(editor) {
|
function getSelectContent(editor) {
|
||||||
let selInfo = editor.getSelection();
|
let selInfo = editor.getSelection();
|
||||||
|
@ -1,64 +1,70 @@
|
|||||||
|
/// <reference types="@ms/types/dist/typings/bukkit" />
|
||||||
|
/// <reference types="@ms/types/dist/typings/sponge" />
|
||||||
import { plugin as pluginApi, server } from '@ms/api'
|
import { plugin as pluginApi, server } from '@ms/api'
|
||||||
import { plugin, interfaces, cmd } from '@ms/plugin'
|
import { plugin, interfaces, cmd } from '@ms/plugin'
|
||||||
import { DefaultContainer as container, inject, postConstruct } from '@ms/container';
|
import { DefaultContainer as container, inject, postConstruct } from '@ms/container'
|
||||||
import * as reflect from '@ms/common/dist/reflect'
|
import * as reflect from '@ms/common/dist/reflect'
|
||||||
|
|
||||||
let clients: any[] = [];
|
let clients: any[] = []
|
||||||
let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
|
let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
|
||||||
const refList: Array<{ server: string, future: string }> = [
|
const refList: Array<{ server: string, future: string }> = [
|
||||||
{ server: 'an', future: 'g' },
|
{ server: 'an', future: 'g' },
|
||||||
{ server: 'getServerConnection', future: 'f' },
|
{ server: 'getServerConnection', future: 'f' },
|
||||||
{ server: 'func_147137_ag', future: 'field_151274_e' }
|
{ server: 'func_147137_ag', future: 'field_151274_e' }
|
||||||
];
|
]
|
||||||
|
|
||||||
|
const Callable = Java.type('java.util.concurrent.Callable')
|
||||||
|
|
||||||
@plugin({ name: 'WebSocket', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
@plugin({ name: 'WebSocket', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||||
export class WebSocket extends interfaces.Plugin {
|
export class WebSocket extends interfaces.Plugin {
|
||||||
@inject(pluginApi.PluginManager)
|
@inject(pluginApi.PluginManager)
|
||||||
private PluginManager: pluginApi.PluginManager;
|
private PluginManager: pluginApi.PluginManager
|
||||||
@inject(server.ServerType)
|
@inject(server.ServerType)
|
||||||
private ServerType: string;
|
private ServerType: string
|
||||||
private pipeline: any;
|
@inject(pluginApi.PluginInstance)
|
||||||
|
private pluginInstance: any
|
||||||
|
private pipeline: any
|
||||||
|
|
||||||
@cmd()
|
@cmd()
|
||||||
ws(sender: any, command: string, args: string[]) {
|
ws(sender: any, command: string, args: string[]) {
|
||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
case "reload":
|
case "reload":
|
||||||
this.PluginManager.reload(this);
|
this.PluginManager.reload(this)
|
||||||
break;
|
break
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disable() {
|
disable() {
|
||||||
if (this.pipeline) {
|
if (this.pipeline) {
|
||||||
this.pipeline.remove('miao_detect');
|
this.pipeline.remove('miao_detect')
|
||||||
clients.forEach(c => c.close())
|
clients.forEach(c => c.close())
|
||||||
container.unbind('onmessage')
|
container.unbind('onmessage')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bukkitenable() {
|
bukkitenable() {
|
||||||
let Bukkit = Java.type('org.bukkit.Bukkit');
|
let Bukkit = Java.type('org.bukkit.Bukkit')
|
||||||
let consoleServer = reflect.on(Bukkit.getServer()).get('console').get();
|
let consoleServer = reflect.on(Bukkit.getServer()).get('console').get()
|
||||||
this.injectMiaoDetect(this.reflectPromise(consoleServer))
|
this.injectMiaoDetect(this.reflectPromise(consoleServer))
|
||||||
}
|
}
|
||||||
|
|
||||||
spongeenable() {
|
spongeenable() {
|
||||||
let Sponge = Java.type('org.spongepowered.api.Sponge');
|
let Sponge = Java.type('org.spongepowered.api.Sponge')
|
||||||
let consoleServer = reflect.on(Sponge.getServer()).get();
|
let consoleServer = reflect.on(Sponge.getServer()).get()
|
||||||
this.injectMiaoDetect(this.reflectPromise(consoleServer))
|
this.injectMiaoDetect(this.reflectPromise(consoleServer))
|
||||||
}
|
}
|
||||||
|
|
||||||
reflectPromise(consoleServer) {
|
reflectPromise(consoleServer) {
|
||||||
for (const ref of refList) {
|
for (const ref of refList) {
|
||||||
try { return reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0); } catch (error) { }
|
try { return reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0) } catch (error) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
injectMiaoDetect(promise) {
|
injectMiaoDetect(promise) {
|
||||||
if (!promise) { throw Error(`Can't found ServerConnection or ChannelFuture !`) };
|
if (!promise) { throw Error(`Can't found ServerConnection or ChannelFuture !`) }
|
||||||
this.pipeline = reflect.on(promise).get('channel').get().pipeline();
|
this.pipeline = reflect.on(promise).get('channel').get().pipeline()
|
||||||
this.pipeline.addFirst('miao_detect', new MiaoDetectHandler());
|
this.pipeline.addFirst('miao_detect', new MiaoDetectHandler())
|
||||||
container.bind('onmessage').toFunction(this.onmessage.bind(this))
|
container.bind('onmessage').toFunction(this.onmessage.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,20 +80,31 @@ export class WebSocket extends interfaces.Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
execCommand(ctx: any, cmd: string) {
|
execCommand(ctx: any, cmd: string) {
|
||||||
|
switch (this.ServerType) {
|
||||||
|
case "bukkit":
|
||||||
org.bukkit.Bukkit.dispatchCommand(org.bukkit.Bukkit.getConsoleSender(), cmd)
|
org.bukkit.Bukkit.dispatchCommand(org.bukkit.Bukkit.getConsoleSender(), cmd)
|
||||||
|
break
|
||||||
|
case "sponge":
|
||||||
|
break
|
||||||
|
}
|
||||||
return `§6命令: §b${cmd} §a执行成功!`
|
return `§6命令: §b${cmd} §a执行成功!`
|
||||||
}
|
}
|
||||||
|
|
||||||
execCode(ctx: any, code: string) {
|
execCode(ctx: any, code: string) {
|
||||||
return eval(code) || '无返回结果'
|
switch (this.ServerType) {
|
||||||
|
case "bukkit":
|
||||||
|
return org.bukkit.Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => eval(code) })).get() || '无返回结果'
|
||||||
|
case "sponge":
|
||||||
|
return ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
execDetect(ctx: any, cmd: string) {
|
execDetect(ctx: any, cmd: string) {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case "type":
|
case "type":
|
||||||
let version = this.ServerType == 'bukkit' ? org.bukkit.Bukkit.getServer().getVersion() : org.spongepowered.api.Sponge.getPlatform().getMinecraftVersion();
|
let version = this.ServerType == 'bukkit' ? org.bukkit.Bukkit.getServer().getVersion() : org.spongepowered.api.Sponge.getPlatform().getMinecraftVersion()
|
||||||
this.sendResult(ctx, "type", this.ServerType)
|
this.sendResult(ctx, "type", this.ServerType)
|
||||||
return `Currect Server Version is ${version}`;
|
return `Currect Server Version is ${version}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,62 +113,63 @@ export class WebSocket extends interfaces.Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter');
|
const ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter')
|
||||||
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
|
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
|
||||||
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
||||||
const MiaoDetectHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
const MiaoDetectHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
||||||
channelRead: function(ctx: any, channel: any) {
|
channelRead: (ctx: any, channel: any) => {
|
||||||
channel.pipeline().addFirst('miaowebsocket', new WebSocketHandler())
|
channel.pipeline().addFirst('miaowebsocket', new WebSocketHandler())
|
||||||
ctx.fireChannelRead(channel);
|
ctx.fireChannelRead(channel)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher')
|
const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher')
|
||||||
const DefaultHttpResponse = Java.type('io.netty.handler.codec.http.DefaultHttpResponse');
|
const DefaultHttpResponse = Java.type('io.netty.handler.codec.http.DefaultHttpResponse')
|
||||||
const DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse');
|
const DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse')
|
||||||
const HttpHeaders = Java.type('io.netty.handler.codec.http.HttpHeaders');
|
const HttpHeaders = Java.type('io.netty.handler.codec.http.HttpHeaders')
|
||||||
const HttpVersion = Java.type('io.netty.handler.codec.http.HttpVersion');
|
const HttpVersion = Java.type('io.netty.handler.codec.http.HttpVersion')
|
||||||
const HttpResponseStatus = Java.type('io.netty.handler.codec.http.HttpResponseStatus');
|
const HttpResponseStatus = Java.type('io.netty.handler.codec.http.HttpResponseStatus')
|
||||||
const LastHttpContent = Java.type('io.netty.handler.codec.http.LastHttpContent');
|
const LastHttpContent = Java.type('io.netty.handler.codec.http.LastHttpContent')
|
||||||
const HttpServerCodec = Java.type('io.netty.handler.codec.http.HttpServerCodec');
|
const HttpServerCodec = Java.type('io.netty.handler.codec.http.HttpServerCodec')
|
||||||
const ChunkedWriteHandler = Java.type('io.netty.handler.stream.ChunkedWriteHandler');
|
const ChunkedWriteHandler = Java.type('io.netty.handler.stream.ChunkedWriteHandler')
|
||||||
const HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAggregator');
|
const HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAggregator')
|
||||||
const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler');
|
const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler')
|
||||||
const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler');
|
const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler')
|
||||||
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
|
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
|
||||||
const File = Java.type('java.io.File');
|
const File = Java.type('java.io.File')
|
||||||
const RandomAccessFile = Java.type('java.io.RandomAccessFile');
|
const RandomAccessFile = Java.type('java.io.RandomAccessFile')
|
||||||
const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion');
|
const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion')
|
||||||
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener');
|
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener')
|
||||||
const HttpRequestHandler = Java.extend(SimpleChannelInboundHandler, {
|
const HttpRequestHandler = Java.extend(SimpleChannelInboundHandler, {
|
||||||
acceptInboundMessage: (msg: any) => {
|
acceptInboundMessage: (msg: any) => {
|
||||||
return FullHttpRequestMatcher.match(msg);
|
return FullHttpRequestMatcher.match(msg)
|
||||||
},
|
},
|
||||||
channelRead0: (ctx: any, request: any) => {
|
channelRead0: (ctx: any, request: any) => {
|
||||||
if ('/ws' == request.getUri()) {
|
if ('/ws' == request.getUri()) {
|
||||||
ctx.fireChannelRead(request.retain())
|
ctx.fireChannelRead(request.retain())
|
||||||
} else {
|
} else {
|
||||||
if (HttpHeaders.is100ContinueExpected(request)) {
|
if (HttpHeaders.is100ContinueExpected(request)) {
|
||||||
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
|
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE))
|
||||||
}
|
}
|
||||||
let file = new File('/home/project/TSWorkSpace/ms/packages/plugins/public', request.getUri().split('?')[0])
|
let filename = request.getUri().split('?')[0].substr(1)
|
||||||
if (!file.exists()) {
|
let file = new File('/home/project/TSWorkSpace/ms/packages/plugins/public', filename || 'index.html')
|
||||||
ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND));
|
if (!file.exists() || !file.isFile()) {
|
||||||
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
|
ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND))
|
||||||
return;
|
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK)
|
let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK)
|
||||||
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
|
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html charset=UTF-8")
|
||||||
let raf = new RandomAccessFile(file, 'r');
|
let raf = new RandomAccessFile(file, 'r')
|
||||||
let keepAlive = HttpHeaders.isKeepAlive(request);
|
let keepAlive = HttpHeaders.isKeepAlive(request)
|
||||||
if (keepAlive) {
|
if (keepAlive) {
|
||||||
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
|
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length())
|
||||||
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
|
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE)
|
||||||
}
|
}
|
||||||
ctx.write(response);
|
ctx.write(response)
|
||||||
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length()));
|
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length()))
|
||||||
let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
|
let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)
|
||||||
if (!keepAlive) {
|
if (!keepAlive) {
|
||||||
future.addListener(ChannelFutureListener.CLOSE);
|
future.addListener(ChannelFutureListener.CLOSE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,38 +178,37 @@ const TextWebSocketFrameMatcher = TypeParameterMatcher.get(base.getClass('io.net
|
|||||||
const TextWebSocketFrameHandler = Java.extend(SimpleChannelInboundHandler, {
|
const TextWebSocketFrameHandler = Java.extend(SimpleChannelInboundHandler, {
|
||||||
userEventTriggered: (ctx: any, evt: any) => {
|
userEventTriggered: (ctx: any, evt: any) => {
|
||||||
if (evt == 'HANDSHAKE_COMPLETE') {
|
if (evt == 'HANDSHAKE_COMPLETE') {
|
||||||
clients.push(ctx.channel());
|
clients.push(ctx.channel())
|
||||||
console.console(`§6[§cMS§6][§bWebSocket§6]§r new client §b${ctx.channel().id()} §aconnected...`)
|
console.console(`§6[§cMS§6][§bWebSocket§6]§r new client §b${ctx.channel().id()} §aconnected...`)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
acceptInboundMessage: (msg: any) => {
|
acceptInboundMessage: (msg: any) => {
|
||||||
return TextWebSocketFrameMatcher.match(msg);
|
return TextWebSocketFrameMatcher.match(msg)
|
||||||
},
|
},
|
||||||
channelRead0: (ctx: any, msg: any) => {
|
channelRead0: (ctx: any, msg: any) => {
|
||||||
container.get<any>('onmessage')(ctx, msg);
|
container.get<any>('onmessage')(ctx, msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
||||||
channelRead: function(ctx: any, msg: any) {
|
channelRead: function(ctx: any, msg: any) {
|
||||||
msg.markReaderIndex();
|
msg.markReaderIndex()
|
||||||
let message: string = msg.toString(CharsetUtil.UTF_8);
|
let message: string = msg.toString(CharsetUtil.UTF_8)
|
||||||
let channel = ctx.channel();
|
let channel = ctx.channel()
|
||||||
let pipeline = channel.pipeline();
|
let pipeline = channel.pipeline()
|
||||||
if (message.indexOf('HTTP/1.1') > 0) {
|
if (message.indexOf('HTTP/1.1') > 0) {
|
||||||
try {
|
channel.pipeline().names().forEach(f => {
|
||||||
'protocol_lib_finish protocol_lib_decoder protocol_lib_encoder'.split(' ').forEach(f => channel.pipeline().remove(f))
|
if (f == 'miaowebsocket' || f.indexOf('DefaultChannelPipeline') > -1) { return }
|
||||||
} catch (error) {
|
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())
|
||||||
|
pipeline.addLast('websocket', new WebSocketServerProtocolHandler("/ws"))
|
||||||
|
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler())
|
||||||
}
|
}
|
||||||
'timeout legacy_query splitter decoder prepender encoder packet_handler'.split(' ').forEach(f => channel.pipeline().remove(f))
|
pipeline.remove('miaowebsocket')
|
||||||
pipeline.addLast('http', new HttpServerCodec());
|
msg.resetReaderIndex()
|
||||||
pipeline.addLast('chunk', new ChunkedWriteHandler());
|
ctx.fireChannelRead(msg)
|
||||||
pipeline.addLast('httpobj', new HttpObjectAggregator(64 * 1024));
|
|
||||||
pipeline.addLast('http_request', new HttpRequestHandler());
|
|
||||||
pipeline.addLast('websocket', new WebSocketServerProtocolHandler("/ws"));
|
|
||||||
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler());
|
|
||||||
}
|
|
||||||
pipeline.remove('miaowebsocket');
|
|
||||||
msg.resetReaderIndex();
|
|
||||||
ctx.fireChannelRead(msg);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user