Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e3378a257e | |||
| 75302195e3 | |||
| 78ab1a73d6 | |||
| 867fc802ec | |||
| ed588e4502 | |||
| cbf00d107e | |||
| b9a9334655 | |||
| 2a58ad46d2 | |||
| b301948583 | |||
| b21aa1051d | |||
| 0f418f39df | |||
| 75cb430230 | |||
| 6aedd8e680 | |||
| cd31f80805 | |||
| 08ba1c1a98 | |||
| d5c2a825fc | |||
| b36b63277f | |||
| 7d02194ac7 | |||
| ce4ad6f046 | |||
| 24691a9ce8 | |||
| 9fa13f49cd | |||
| 64a698089b | |||
| 53843b65d2 | |||
| 5f00431e8b |
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"useWorkspaces": true,
|
||||
"npmClient": "yarn",
|
||||
"packages": [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/amqp",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript amqp package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,17 +19,17 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"@javatypes/amqp-client": "^0.0.3",
|
||||
"@javatypes/spring-amqp": "^0.0.3",
|
||||
"@javatypes/spring-rabbit": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/api",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,9 +19,9 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0",
|
||||
"@ccms/polyfill": "^0.15.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0",
|
||||
"@ccms/polyfill": "^0.17.0",
|
||||
"base64-js": "^1.5.1",
|
||||
"source-map-builder": "^0.0.7"
|
||||
},
|
||||
@@ -29,6 +29,6 @@
|
||||
"@types/base64-js": "^1.3.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as base64 from 'base64-js'
|
||||
const Arrays = Java.type('java.util.Arrays')
|
||||
const Level = Java.type('java.util.logging.Level')
|
||||
const Paths = Java.type('java.nio.file.Paths')
|
||||
const ignoreLogPrefix = ['java.', 'javax.', 'sun.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.', 'io.netty.', 'org.spongepowered.', 'org.apache', 'org.springframework']
|
||||
const ignoreLogPrefix = ['java.', 'javax.', 'sun.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.', 'org.openjdk.nashorn', 'io.netty.', 'org.spongepowered.', 'org.apache', 'org.springframework']
|
||||
|
||||
enum LogLevel {
|
||||
ALL,
|
||||
@@ -19,7 +19,7 @@ enum LogLevel {
|
||||
}
|
||||
|
||||
export class MiaoScriptConsole implements Console {
|
||||
Console: NodeJS.ConsoleConstructor
|
||||
Console: any
|
||||
memory: any
|
||||
|
||||
private static sourceMaps: { [key: string]: SourceMapBuilder } = {}
|
||||
@@ -105,7 +105,7 @@ export class MiaoScriptConsole implements Console {
|
||||
let sourceMappingURL = lastLine.split('sourceMappingURL=', 2)[1]
|
||||
if (sourceMappingURL.startsWith('data:application/json;base64,')) {
|
||||
sourceContent = String.fromCharCode(...Array.from(base64.toByteArray(sourceMappingURL.split(',', 2)[1])))
|
||||
} else if (sourceMappingURL.startsWith('http')) {
|
||||
} else if (sourceMappingURL.startsWith('http://') || sourceMappingURL.startsWith('https://')) {
|
||||
// TODO
|
||||
} else {
|
||||
let file = Paths.get(Paths.get(fileName, '..', sourceMappingURL).toFile().getCanonicalPath()).toFile()
|
||||
@@ -156,7 +156,7 @@ export class MiaoScriptConsole implements Console {
|
||||
let className = trace.className
|
||||
var fileName = trace.fileName as string
|
||||
var lineNumber = trace.lineNumber
|
||||
if (className.startsWith('jdk.nashorn.internal.scripts')) {
|
||||
if (className.startsWith('jdk.nashorn.internal.scripts') || className.startsWith('org.openjdk.nashorn.internal.scripts')) {
|
||||
className = className.substr(className.lastIndexOf('$') + 1)
|
||||
var { fileName, lineNumber } = this.readSourceMap(fileName, lineNumber)
|
||||
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
||||
@@ -190,7 +190,7 @@ export class MiaoScriptConsole implements Console {
|
||||
countReset(label?: string): void {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dir(obj: any, options?: NodeJS.InspectOptions): void {
|
||||
dir(obj: any, options?: any): void {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dirxml(...data: any[]): void {
|
||||
|
||||
@@ -16,15 +16,28 @@ export namespace server {
|
||||
* Runtime Server Instance
|
||||
*/
|
||||
export const ServerInstance = Symbol("ServerInstance")
|
||||
export interface NativePlugin {
|
||||
name: string
|
||||
version: string
|
||||
authors?: string | string[]
|
||||
enable: boolean
|
||||
depends?: string[]
|
||||
softDepends?: string[]
|
||||
/**
|
||||
* 插件本体
|
||||
*/
|
||||
origin: any
|
||||
[key: string]: any
|
||||
}
|
||||
@injectable()
|
||||
export abstract class NativePluginManager {
|
||||
list(): any[] {
|
||||
list(): NativePlugin[] {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
has(name: string): boolean {
|
||||
return true
|
||||
}
|
||||
get(name: string): any {
|
||||
get(name: string): NativePlugin {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
load(name: string): boolean {
|
||||
@@ -60,6 +73,12 @@ export namespace server {
|
||||
getService(service: string): any {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
broadcast(message: string, permission: string) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
broadcastMessage(message: string) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export namespace task {
|
||||
this.cacheTasks.delete(taskId)
|
||||
let ownerName = task.getOwner()?.description.name
|
||||
if (ownerName && this.pluginCacheTasks.has(ownerName)) {
|
||||
this.pluginCacheTasks.get(ownerName).delete(taskId)
|
||||
this.pluginCacheTasks.get(ownerName)?.delete(taskId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/bukkit",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript bukkit package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,11 +22,11 @@
|
||||
"@javatypes/spigot-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,36 @@
|
||||
import { server } from '@ccms/api'
|
||||
|
||||
const Bukkit = org.bukkit.Bukkit
|
||||
const Bukkit: typeof org.bukkit.Bukkit = Java.type('org.bukkit.Bukkit')
|
||||
|
||||
export class BukkitNativePluginManager extends server.NativePluginManager {
|
||||
has(name: string) {
|
||||
return !!this.get(name)
|
||||
private bukkitPluginManager: org.bukkit.plugin.PluginManager
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.bukkitPluginManager = Bukkit.getPluginManager()
|
||||
}
|
||||
get(name: string) {
|
||||
return Bukkit.getPluginManager().getPlugin(name)
|
||||
|
||||
list(): server.NativePlugin[] {
|
||||
return Java.from(this.bukkitPluginManager.getPlugins()).map(plugin => this.convert(plugin))
|
||||
}
|
||||
has(name: string): boolean {
|
||||
return !!this.bukkitPluginManager.getPlugin(name)
|
||||
}
|
||||
get(name: string): server.NativePlugin {
|
||||
return this.convert(this.bukkitPluginManager.getPlugin(name))
|
||||
}
|
||||
|
||||
private convert(plugin: org.bukkit.plugin.Plugin): server.NativePlugin {
|
||||
if (!plugin) return plugin as any
|
||||
let desc = plugin.getDescription()
|
||||
return {
|
||||
name: plugin.getName(),
|
||||
version: desc.getVersion(),
|
||||
authors: Java.from(desc.getAuthors()),
|
||||
depends: Java.from(desc.getDepend()),
|
||||
softDepends: Java.from(desc.getSoftDepend()),
|
||||
enable: plugin.isEnabled(),
|
||||
origin: plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { provideSingleton } from '@ccms/container'
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
import chat from './enhance/chat'
|
||||
|
||||
let Bukkit = org.bukkit.Bukkit
|
||||
let Bukkit: typeof org.bukkit.Bukkit = org.bukkit.Bukkit
|
||||
|
||||
@provideSingleton(server.Server)
|
||||
export class BukkitServer extends server.ReflectServer {
|
||||
@@ -30,6 +30,12 @@ export class BukkitServer extends server.ReflectServer {
|
||||
getService(service: string) {
|
||||
return Bukkit.getServicesManager().getRegistration(base.getClass(service))?.getProvider()
|
||||
}
|
||||
broadcast(message: string, permission: string) {
|
||||
return Bukkit.broadcast(message, permission)
|
||||
}
|
||||
broadcastMessage(message: string) {
|
||||
return Bukkit.broadcastMessage(message)
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
if (typeof sender === 'string') {
|
||||
sender = this.getPlayer(sender)
|
||||
|
||||
@@ -21,11 +21,11 @@ export class BukkitTaskManager extends task.TaskManager {
|
||||
export class BukkitTask extends task.Task {
|
||||
submit0(...args: any[]): task.Cancelable {
|
||||
let run = new BukkitRunnable({ run: () => this.run(...args) })
|
||||
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
||||
let suffix = this.isAsync ? 'Asynchronously' : ''
|
||||
if (this.interval) {
|
||||
return run[funcName](base.getInstance(), this.laterTime, this.interval)
|
||||
return run[`runTaskTimer${suffix}`](base.getInstance(), this.laterTime, this.interval)
|
||||
} else {
|
||||
return run[funcName](base.getInstance(), this.laterTime)
|
||||
return run[`runTaskLater${suffix}`](base.getInstance(), this.laterTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/bungee",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript bungee package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,11 +22,11 @@
|
||||
"@javatypes/bungee-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,34 @@ import { server } from '@ccms/api'
|
||||
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
||||
|
||||
export class BungeeNativePluginManager extends server.NativePluginManager {
|
||||
has(name: string) {
|
||||
return !!this.get(name)
|
||||
private bungeePluginManager: net.md_5.bungee.api.plugin.PluginManager
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.bungeePluginManager = Bungee.getPluginManager()
|
||||
}
|
||||
get(name: string) {
|
||||
return Bungee.getPluginManager().getPlugin(name)
|
||||
|
||||
list(): server.NativePlugin[] {
|
||||
return Java.from(this.bungeePluginManager.getPlugins()).map(plugin => this.convert(plugin))
|
||||
}
|
||||
has(name: string): boolean {
|
||||
return !!this.bungeePluginManager.getPlugin(name)
|
||||
}
|
||||
get(name: string): server.NativePlugin {
|
||||
return this.convert(this.bungeePluginManager.getPlugin(name))
|
||||
}
|
||||
|
||||
private convert(plugin: net.md_5.bungee.api.plugin.Plugin): server.NativePlugin {
|
||||
if (!plugin) return plugin as any
|
||||
let desc = plugin.getDescription()
|
||||
return {
|
||||
name: desc.getName(),
|
||||
version: desc.getVersion(),
|
||||
authors: [desc.getAuthor()],
|
||||
depends: Java.from(desc.getDepends()) as any,
|
||||
softDepends: Java.from(desc.getSoftDepends()) as any,
|
||||
enable: true,
|
||||
origin: plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,12 @@ export class BungeeServer implements server.Server {
|
||||
getService(service: string) {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
broadcast(message: string, permission: string) {
|
||||
return Bungee.broadcast(message)
|
||||
}
|
||||
broadcastMessage(message: string) {
|
||||
return Bungee.broadcast(message)
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
if (typeof sender === 'string') {
|
||||
sender = this.getPlayer(sender)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "@ccms/client",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript client package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -25,8 +25,8 @@
|
||||
"minecraft-protocol": "^1.25.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^15.12.4",
|
||||
"@types/node": "^16.4.12",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/common",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,11 +19,11 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"@javatypes/jdk": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"gitHead": "562e2d00175c9d3a99c8b672aa07e6d92706a027"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/compile",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript compile package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -21,6 +21,6 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/container",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript container package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,10 +19,10 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"inversify": "^5.1.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/core",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -21,11 +21,11 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
},
|
||||
"gitHead": "781524f83e52cad26d7c480513e3c525df867121"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
let containerStartTime = Date.now()
|
||||
console.i18n("ms.core.ioc.initialize", { scope: global.scope })
|
||||
import { plugin, server, task, constants } from '@ccms/api'
|
||||
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule, Autowired } from '@ccms/container'
|
||||
import { DefaultContainer as container, provideSingleton, ContainerInstance, buildProviderModule, Autowired } from '@ccms/container'
|
||||
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
|
||||
import http from '@ccms/common/dist/http'
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/database",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript database package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,10 +22,10 @@
|
||||
"@javatypes/spring-jdbc": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/i18n",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript i18n package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,11 +19,11 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@types/js-yaml": "^4.0.1",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"@types/js-yaml": "^4.0.2",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"js-yaml": "^4.1.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/keyvalue",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript keyvalue package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,18 +19,18 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"@javatypes/amqp-client": "^0.0.3",
|
||||
"@javatypes/spring-amqp": "^0.0.3",
|
||||
"@javatypes/spring-rabbit": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"gitHead": "2589633069d24f646ac09261b1b2304c21d4ea75"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nashorn",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,6 +22,6 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nodejs",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript nodejs package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,11 +19,11 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"tslib": "^2.3.0",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"gitHead": "781524f83e52cad26d7c480513e3c525df867121"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/nukkit",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript nukkit package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,11 +22,11 @@
|
||||
"@javatypes/nukkit-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,34 @@ import { server } from '@ccms/api'
|
||||
let Nukkit: cn.nukkit.Server = base.getInstance().getServer()
|
||||
|
||||
export class NukkitNativePluginManager extends server.NativePluginManager {
|
||||
private nukkitPluginManager: cn.nukkit.plugin.PluginManager
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.nukkitPluginManager = Nukkit.getPluginManager()
|
||||
}
|
||||
|
||||
list(): server.NativePlugin[] {
|
||||
return Java.from(this.nukkitPluginManager.getPlugins().values()).map(plugin => this.convert(plugin))
|
||||
}
|
||||
has(name: string) {
|
||||
return !!this.get(name)
|
||||
return !!this.nukkitPluginManager.getPlugin(name)
|
||||
}
|
||||
get(name: string) {
|
||||
return Nukkit.getPluginManager().getPlugin(name)
|
||||
return this.convert(this.nukkitPluginManager.getPlugin(name))
|
||||
}
|
||||
|
||||
private convert(plugin: cn.nukkit.plugin.Plugin): server.NativePlugin {
|
||||
if (!plugin) return plugin as any
|
||||
let desc = plugin.getDescription()
|
||||
return {
|
||||
name: plugin.getName(),
|
||||
version: desc.getVersion(),
|
||||
authors: Java.from(desc.getAuthors() as string[]),
|
||||
depends: Java.from(desc.getDepend() as string[]),
|
||||
softDepends: Java.from(desc.getSoftDepend() as string[]),
|
||||
enable: plugin.isEnabled(),
|
||||
origin: plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,12 @@ export class NukkitServer implements server.Server {
|
||||
getService(service: string) {
|
||||
return Nukkit.getServiceManager().getProvider(base.getClass(service))
|
||||
}
|
||||
broadcast(message: string, permission: string) {
|
||||
return Nukkit.broadcast(message, permission)
|
||||
}
|
||||
broadcastMessage(message: string) {
|
||||
return Nukkit.broadcastMessage(message)
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
if (typeof sender === 'string') {
|
||||
sender = this.getPlayer(sender)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/plugin",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -19,16 +19,16 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-yaml": "^4.0.1",
|
||||
"@types/js-yaml": "^4.0.2",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0",
|
||||
"@ccms/i18n": "^0.15.0",
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0",
|
||||
"@ccms/i18n": "^0.17.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
"yaml": "^1.10.2"
|
||||
}
|
||||
|
||||
@@ -92,18 +92,10 @@ export class PluginConfigManager {
|
||||
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: metadata.name, format: metadata.format })
|
||||
} else {
|
||||
configValue = configLoader.load(base.read(metadata.file)) || {}
|
||||
if (defaultValue) {
|
||||
let needSave = false
|
||||
for (const key of Object.keys(defaultValue)) {
|
||||
// 当配置文件不存在当前属性时才进行赋值
|
||||
if (!Object.prototype.hasOwnProperty.call(configValue, key)) {
|
||||
configValue[key] = defaultValue[key]
|
||||
needSave = true
|
||||
}
|
||||
}
|
||||
needSave && base.save(metadata.file, configLoader.dump(configValue))
|
||||
if (defaultValue && this.setDefaultValue(configValue, defaultValue)) {
|
||||
base.save(metadata.file, configLoader.dump(configValue))
|
||||
}
|
||||
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>\n${JSON.stringify(configValue, undefined, 4)}`)
|
||||
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>\n${JSON.stringify(configValue, undefined, 4).substr(0, 500)}`)
|
||||
}
|
||||
this.defienConfigProp(plugin, metadata, configValue)
|
||||
} catch (error) {
|
||||
@@ -112,12 +104,27 @@ export class PluginConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
private setDefaultValue(configValue, defaultValue) {
|
||||
let needSave = false
|
||||
for (const key of Object.keys(defaultValue)) {
|
||||
// 当配置文件不存在当前属性时才进行赋值
|
||||
if (!Object.prototype.hasOwnProperty.call(configValue, key)) {
|
||||
configValue[key] = defaultValue[key]
|
||||
needSave = true
|
||||
} else if (Object.prototype.toString.call(configValue[key]) == "[object Object]") {
|
||||
// 对象需要递归检测
|
||||
needSave ||= this.setDefaultValue(configValue[key], defaultValue[key])
|
||||
}
|
||||
}
|
||||
return needSave
|
||||
}
|
||||
|
||||
private saveConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) {
|
||||
try {
|
||||
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
|
||||
let result = this.getConfigLoader(metadata.format).dump(plugin[metadata.variable])
|
||||
base.save(metadata.file, result)
|
||||
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>\n${result}`)
|
||||
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>\n${result.substr(0, 500)}`)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.i18n("ms.plugin.manager.config.save.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
|
||||
|
||||
@@ -8,9 +8,8 @@ import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata
|
||||
* MiaoScript plugin
|
||||
* @param metadata PluginMetadata
|
||||
*/
|
||||
export function plugin(metadata: pluginApi.PluginMetadata | any) {
|
||||
export function plugin(metadata: pluginApi.PluginMetadata) {
|
||||
return function (target: any) {
|
||||
if (!metadata.source) metadata = { souece: metadata }
|
||||
metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata }
|
||||
decorate(injectable(), target)
|
||||
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target)
|
||||
|
||||
@@ -19,7 +19,7 @@ export class JSFileScanner implements plugin.PluginScanner {
|
||||
load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
|
||||
if (metadata.type !== this.type) { return }
|
||||
this.updatePlugin(metadata.file)
|
||||
//@ts-ignore
|
||||
//@ts-ignore load plugin not use cache
|
||||
metadata.instance = require(metadata.file.toString(), { cache: false })
|
||||
return metadata
|
||||
}
|
||||
|
||||
35
packages/plugins/docs/MiaoLink.md
Normal file
35
packages/plugins/docs/MiaoLink.md
Normal file
@@ -0,0 +1,35 @@
|
||||
[综合|前置]MiaoLink —— 喵式映射 用于无公网环境的自动化端口映射[1.7.10+全版本]
|
||||
# MiaoLink
|
||||
|
||||
## 插件介绍
|
||||
|
||||
> 自动化端口公网映射
|
||||
|
||||
## 图片展示
|
||||
|
||||
- 
|
||||
|
||||
## 使用方式
|
||||
|
||||
- 本插件依赖于 `MiaoScript` 请前往 [站内帖子](https://www.mcbbs.net/thread-774401-1-1.html) 完成安装
|
||||
- 执行 `/mspm install MiaoLink` 安装 MiaoLink 脚本插件
|
||||
- 访问 [圈云映射](https://nps.yumc.pw) 申请一键映射指令
|
||||
- 
|
||||
- 执行网页上提供的指令 等待客户端上线
|
||||
- 
|
||||
- 使用访问地址即可链接服务器
|
||||
- 
|
||||
|
||||
### Roadmap
|
||||
|
||||
- 支持Bukkit端自动化映射(已完成)
|
||||
- 支持Bungee端自动化映射(开发中)
|
||||
- 支持Sponge端自动化映射(开发中)
|
||||
|
||||
### 感谢
|
||||
|
||||
- [NPS](https://github.com/ehang-io/nps) 开源项目
|
||||
- [蓝科数据](https://www.lankodata.com/aff.php?aff=32) 提供的映射节点
|
||||
- [AkkoCloud](https://www.akkocloud.com/aff.php?aff=698) 提供的映射节点
|
||||
|
||||
#### 本插件所用所有代码均为原创,不存在借用/抄袭等行为
|
||||
@@ -1,5 +1,6 @@
|
||||
### Miao系列插件
|
||||
|
||||
- [[综合|前置]MiaoLink —— 喵式映射 用于无公网环境的自动化端口映射[全版本]](https://www.mcbbs.net/thread-1121423-1-1.html)
|
||||
- [[经济]MiaoReward —— 喵式奖励 让玩家看广告为服务器提供收入吧[1.7.10+全版本]](https://www.mcbbs.net/thread-1121423-1-1.html)
|
||||
- [[编程]MiaoBlockly —— 喵式积木 用简单的积木来写插件吧[1.12.2+全版本]](https://www.mcbbs.net/thread-1129411-1-1.html)
|
||||
- [[编程]MiaoConsole —— 喵式终端 通过MC端口直接控制服务器 调试插件[1.12.2+全版本]](https://www.mcbbs.net/thread-1129227-1-1.html)
|
||||
|
||||
37
packages/plugins/docs/MiaoRGBSupport.md
Normal file
37
packages/plugins/docs/MiaoRGBSupport.md
Normal file
@@ -0,0 +1,37 @@
|
||||
[综合|信息]MiaoRGBSupport —— 喵式RGB 支持任意插件的RGB展示[1.16+]
|
||||
|
||||
# MiaoRGBSupport
|
||||
|
||||
## 插件简介
|
||||
- 支持1.16+彩色字体展示
|
||||
- 兼容原版聊天以及任意聊天插件
|
||||
- 兼容任意记分板插件
|
||||
- 仅需安装插件配置权限即可生效
|
||||
|
||||
## 插件权限
|
||||
- `MiaoRGBSupport.color` 默认玩家没权限 需要手动添加
|
||||
|
||||
## 安装方式
|
||||
- 本插件基于 MiaoScript 开发
|
||||
- 请先安装 MiaoScript [坛内地址](https://www.mcbbs.net/thread-774401-1-1.html)
|
||||
- 然后安装 ProtocolLib 自己解决
|
||||
- 执行 `/mspm install MiaoChatRGBSupport`
|
||||
|
||||
## 使用方式
|
||||
- 颜色格式 `#FFFFFF` 标准 HTML 的色彩格式
|
||||
|
||||
### 聊天插件配置
|
||||
- 例如 配置 `MiaoChat` 的 `format.yml`
|
||||
- 
|
||||
- 配置完成后 重载插件
|
||||
|
||||
### 聊天时使用
|
||||
- 聊天时 直接输入颜色代码即可
|
||||
- 
|
||||
|
||||
## 星球特供版本
|
||||
- 
|
||||
- 此版本为知识星球特供
|
||||
- 支持 聊天和记分板
|
||||
- 支持 彩虹字
|
||||
- 详情加群 650545561
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "@ccms/plugins",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript plugins package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -26,16 +26,16 @@
|
||||
"@javatypes/spring-data-redis": "^0.0.3",
|
||||
"@javatypes/spring-web": "^0.0.3",
|
||||
"@javatypes/tomcat": "^0.0.3",
|
||||
"@types/crypto-js": "^4.0.1",
|
||||
"@types/crypto-js": "^4.0.2",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/standalone": "^7.14.7",
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0",
|
||||
"@ccms/plugin": "^0.15.0",
|
||||
"crypto-js": "^4.0.0"
|
||||
"@babel/standalone": "^7.15.0",
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0",
|
||||
"@ccms/plugin": "^0.17.0",
|
||||
"crypto-js": "^4.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
import { plugin as pluginApi, server, task, constants, command } from '@ccms/api'
|
||||
import { plugin, interfaces, cmd, tab, enable, config, disable, PluginConfig } from '@ccms/plugin'
|
||||
import { ContainerInstance, Container, Autowired } from '@ccms/container'
|
||||
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket'
|
||||
import io from '@ccms/websocket'
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
|
||||
import type { Namespace, Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket'
|
||||
|
||||
const suffixMap = {
|
||||
ts: 'typescript',
|
||||
js: 'javascript',
|
||||
@@ -58,14 +60,14 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
this.token = Java.type('java.util.UUID').randomUUID().toString()
|
||||
this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`)
|
||||
}
|
||||
process.on('message', (msg) => {
|
||||
process.on('message', (msg: string) => {
|
||||
this.logCache.push(msg)
|
||||
if (this.logCache.length > 100) {
|
||||
this.logCache = this.logCache.slice(this.logCache.length - 100, this.logCache.length)
|
||||
}
|
||||
})
|
||||
this.task.create(() => {
|
||||
if (!this.babel) {
|
||||
if (!this.babel && this.serverType != constants.ServerType.Bungee) {
|
||||
try {
|
||||
this.logger.console('§3脚本 Babel 引擎初始化中 请稍候...')
|
||||
let startTime = Date.now()
|
||||
@@ -162,7 +164,11 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
if (this.rootLogger) {
|
||||
let AbstractHandler = Java.type('java.util.logging.Handler')
|
||||
let ProxyHandler = Java.extend(AbstractHandler, {
|
||||
publish: (record) => process.emit('message', record.getMessage()),
|
||||
publish: (record) => {
|
||||
if (record.getLevel().intValue() > 500) {
|
||||
process.emit('message', record.getMessage())
|
||||
}
|
||||
},
|
||||
flush: () => { },
|
||||
close: () => { }
|
||||
})
|
||||
@@ -191,6 +197,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
disable() {
|
||||
if (this.socketIOServer) {
|
||||
this.socketIOServer.close()
|
||||
process.removeAllListeners('websocket.create')
|
||||
process.removeAllListeners('message')
|
||||
}
|
||||
if (this.container.isBound(io.Instance)) {
|
||||
@@ -230,30 +237,46 @@ export class MiaoConsole extends interfaces.Plugin {
|
||||
this.socketIOServer = io(this.instance, {
|
||||
path: '/ws',
|
||||
root: fs.concat(root, 'wwwroot')
|
||||
})
|
||||
} as any)
|
||||
this.container.bind(io.Instance).toConstantValue(this.socketIOServer)
|
||||
process.emit('websocket.create', this.socketIOServer)
|
||||
}
|
||||
|
||||
registryWebSocketNamespace(namespace: string, initialization: (namespace: Namespace) => void) {
|
||||
if (this.socketIOServer) {
|
||||
initialization(this.socketIOServer.of(namespace))
|
||||
} else {
|
||||
process.once('websocket.create', (server) => initialization(server.of(namespace)))
|
||||
}
|
||||
}
|
||||
|
||||
checkWebSocketClient(client: SocketIOSocket) {
|
||||
if (!this.token) {
|
||||
return this.notifyDisconnect(client, `§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
||||
}
|
||||
if (this.token != client.handshake.query.token) {
|
||||
return this.notifyDisconnect(client, `§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private notifyDisconnect(client: SocketIOSocket, reason: string) {
|
||||
this.logger.console(reason)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
setTimeout(() => { if (client.connected) { client.disconnect(true) } }, 5)
|
||||
return false
|
||||
}
|
||||
|
||||
startSocketIOServer() {
|
||||
let namespace = this.socketIOServer.of('/MiaoConsole')
|
||||
process.on('message', (msg) => namespace.emit('log', msg))
|
||||
namespace.on('connection', (client: SocketIOSocket) => {
|
||||
if (!this.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
if (this.checkWebSocketClient(client)) {
|
||||
this.initWebSocketClient(client)
|
||||
this.logCache.forEach(msg => client.emit('log', msg))
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
||||
}
|
||||
if (this.token != client.handshake.query.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
this.initWebSocketClient(client)
|
||||
this.logCache.forEach(msg => client.emit('log', msg))
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
||||
})
|
||||
process.emit('websocket.start', this.socketIOServer)
|
||||
}
|
||||
|
||||
private initWebSocketClient(client: SocketIOSocket) {
|
||||
|
||||
344
packages/plugins/src/MiaoDashboard.ts
Normal file
344
packages/plugins/src/MiaoDashboard.ts
Normal file
@@ -0,0 +1,344 @@
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { constants, plugin, server, task } from '@ccms/api'
|
||||
import { Autowired } from '@ccms/container'
|
||||
import { Config, disable, enable, interfaces, JSPlugin, PluginConfig } from '@ccms/plugin'
|
||||
import { client } from '@ccms/websocket'
|
||||
|
||||
import http from '@ccms/common/dist/http'
|
||||
|
||||
import type { Socket as SocketIOSocket, Namespace } from '@ccms/websocket'
|
||||
import type { MiaoConsole } from './MiaoConsole'
|
||||
|
||||
const defaultConfig = {
|
||||
statistics: {
|
||||
max: 300
|
||||
}
|
||||
}
|
||||
|
||||
const defaultDataConfig = {
|
||||
server_online: "%server_online%",
|
||||
server_tps: "%server_tps_1%",
|
||||
server_ram_used: "%server_ram_used%",
|
||||
server_total_chunks: "%server_total_chunks%",
|
||||
server_total_living_entities: "%server_total_living_entities%",
|
||||
server_total_entities: "%server_total_entities%",
|
||||
}
|
||||
|
||||
@JSPlugin({ prefix: 'Dashboard', version: '1.0.0', author: 'MiaoWoo', depends: ['MiaoConsole'], source: __filename })
|
||||
export class MiaoDashboard extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private server: server.Server
|
||||
@Autowired()
|
||||
private nativePluginManager: server.NativePluginManager
|
||||
@Autowired()
|
||||
private pluginManager: plugin.PluginManager
|
||||
@Autowired()
|
||||
private taskManager: task.TaskManager
|
||||
|
||||
private namespace: Namespace
|
||||
|
||||
@Config()
|
||||
private config: PluginConfig & typeof defaultConfig = defaultConfig
|
||||
@Config()
|
||||
private dataConfig: PluginConfig & typeof defaultDataConfig = defaultDataConfig
|
||||
@Config({ autosave: true })
|
||||
private dataCache: PluginConfig & { [key: string]: { time: string, value: Number }[] } = {}
|
||||
|
||||
private statisticTimer: task.Task
|
||||
|
||||
private PlaceholderAPI: { setPlaceholders: (player: any, str: string) => string }
|
||||
|
||||
load() {
|
||||
for (const key of Object.keys(this.dataConfig)) {
|
||||
this.dataCache[key] = this.dataCache[key] ?? []
|
||||
}
|
||||
}
|
||||
|
||||
enable() {
|
||||
let consolePlugin: MiaoConsole = this.pluginManager.getPlugin('MiaoConsole') as MiaoConsole
|
||||
consolePlugin.registryWebSocketNamespace('/MiaoDashboard', (namespace: Namespace) => {
|
||||
this.namespace = namespace
|
||||
this.namespace.on('connection', (client: SocketIOSocket) => {
|
||||
if (consolePlugin.checkWebSocketClient(client)) {
|
||||
this.initWebSocketClient(client)
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@enable({ servers: [constants.ServerType.Bukkit] })
|
||||
enableBukkit() {
|
||||
this.PlaceholderAPI = base.getClass("me.clip.placeholderapi.PlaceholderAPI").static
|
||||
this.statisticTimer = this.taskManager.create(() => {
|
||||
for (const key of Object.keys(this.dataConfig)) {
|
||||
let dataArray = this.dataCache[key]
|
||||
dataArray.push({
|
||||
time: this.dateFormat('HH:MM:SS'),
|
||||
value: parseFloat(this.taskManager.callSyncMethod(() => this.PlaceholderAPI['setPlaceholders(Player,String)'](null, this.dataConfig[key]))) ?? 0
|
||||
})
|
||||
if (dataArray.length > this.config.statistics.max) {
|
||||
this.dataCache[key] = dataArray.slice(dataArray.length - this.config.statistics.max, dataArray.length)
|
||||
}
|
||||
}
|
||||
}, this).async().timer(20 * 10).submit()
|
||||
this.proxys = client.io('ws://192.168.2.25:25577/MiaoConsole?access_token=325325', {
|
||||
path: "/ws"
|
||||
})
|
||||
this.proxys.on('connect', () => {
|
||||
this.logger.info('connect')
|
||||
this.proxys.emit('type', (type) => {
|
||||
console.log('server type is ' + type)
|
||||
})
|
||||
})
|
||||
this.proxys.on('log', (msg) => {
|
||||
console.log(msg)
|
||||
})
|
||||
}
|
||||
|
||||
private proxys
|
||||
|
||||
@enable({ servers: [constants.ServerType.Bungee] })
|
||||
enbaleBungee() {
|
||||
this.proxys = client.io('ws://192.168.2.25:25565/MiaoConsole?access_token=325325', {
|
||||
path: "/ws"
|
||||
})
|
||||
this.proxys.on('connect', () => {
|
||||
this.logger.info('connect')
|
||||
this.proxys.emit('type', (type) => {
|
||||
console.log('server type is ' + type)
|
||||
})
|
||||
})
|
||||
this.proxys.on('log', (msg) => {
|
||||
console.log(msg)
|
||||
})
|
||||
}
|
||||
|
||||
@disable({ servers: [constants.ServerType.Bungee] })
|
||||
disableBungee() {
|
||||
|
||||
}
|
||||
|
||||
private dateFormat(fmt: string, date = new Date()) {
|
||||
let ret: RegExpExecArray
|
||||
const opt = {
|
||||
"Y+": date.getFullYear().toString(), // 年
|
||||
"m+": (date.getMonth() + 1).toString(), // 月
|
||||
"d+": date.getDate().toString(), // 日
|
||||
"H+": date.getHours().toString(), // 时
|
||||
"M+": date.getMinutes().toString(), // 分
|
||||
"S+": date.getSeconds().toString() // 秒
|
||||
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
||||
}
|
||||
for (let k in opt) {
|
||||
ret = new RegExp("(" + k + ")").exec(fmt)
|
||||
if (ret) {
|
||||
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
|
||||
};
|
||||
};
|
||||
return fmt
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.namespace?.close()
|
||||
this.statisticTimer?.cancel()
|
||||
}
|
||||
|
||||
private wrapper(fn, data) {
|
||||
fn({ status: 0, data })
|
||||
}
|
||||
|
||||
private initWebSocketClient(client: SocketIOSocket) {
|
||||
client.on('message', (...args) => {
|
||||
console.log(args)
|
||||
})
|
||||
this.initStatistics(client)
|
||||
this.initPlayers(client)
|
||||
this.initPlugins(client)
|
||||
this.initServices(client)
|
||||
client.on('error', (error) => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
||||
this.logger.error(error)
|
||||
})
|
||||
client.on('disconnect', () => {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
private initStatistics(client: SocketIOSocket) {
|
||||
client.on('statistics', (data, callback) => {
|
||||
let result = {}
|
||||
data.keys.map((key: string) => result[key] = this.dataCache[key])
|
||||
this.wrapper(callback, { time: Date.now(), data: result })
|
||||
})
|
||||
}
|
||||
private initPlayers(client: SocketIOSocket) {
|
||||
client.on('players', (callback) => {
|
||||
let players = Java.from(this.server.getOnlinePlayers()).map((player: org.bukkit.entity.Player) => {
|
||||
let loc = player.getLocation()
|
||||
return {
|
||||
name: player.getName(),
|
||||
location: {
|
||||
world: loc.getWorld().getName(),
|
||||
x: loc.getX().toFixed(0),
|
||||
y: loc.getY().toFixed(0),
|
||||
z: loc.getZ().toFixed(0)
|
||||
},
|
||||
health: player.getHealth().toFixed(0),
|
||||
foodlevel: player.getFoodLevel().toFixed(0),
|
||||
gamemode: player.getGameMode().name(),
|
||||
ip: player.getAddress().getAddress().getHostAddress()
|
||||
}
|
||||
})
|
||||
this.wrapper(callback, {
|
||||
items: players
|
||||
})
|
||||
})
|
||||
this.initPlayerModify(client)
|
||||
client.on('players.heal', ({ name }, callback) => {
|
||||
let player: org.bukkit.entity.Player = this.server.getPlayer(name)
|
||||
if (!player) {
|
||||
return callback({
|
||||
status: 1,
|
||||
msg: '玩家 ' + name + ' 不在线!'
|
||||
})
|
||||
}
|
||||
player.setHealth(player.getMaxHealth())
|
||||
player.setFoodLevel(20)
|
||||
player.sendMessage('§a您已被治疗 §7- 来自MiaoDashboard')
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: '操作成功!'
|
||||
})
|
||||
})
|
||||
client.on('players.kick', ({ name, reason }, callback) => {
|
||||
let player: org.bukkit.entity.Player = this.server.getPlayer(name)
|
||||
if (!player) {
|
||||
return callback({
|
||||
status: 1,
|
||||
msg: '玩家 ' + name + ' 不在线!'
|
||||
})
|
||||
}
|
||||
player.kickPlayer(reason)
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: '操作成功!'
|
||||
})
|
||||
})
|
||||
}
|
||||
private initPlayerModify(client: SocketIOSocket) {
|
||||
client.on('players.modify.gamemode', ({ name, gamemode }, callback) => {
|
||||
let player: org.bukkit.entity.Player = this.server.getPlayer(name)
|
||||
if (!player) {
|
||||
return callback({
|
||||
status: 1,
|
||||
msg: '玩家 ' + name + ' 不在线!'
|
||||
})
|
||||
}
|
||||
let GameMode = Java.type('org.bukkit.GameMode')
|
||||
let mode = GameMode.valueOf(gamemode)
|
||||
this.taskManager.callSyncMethod(() => player.setGameMode(mode))
|
||||
player.sendMessage('§a您的游戏模式已被修改为 ' + gamemode + ' §7- 来自MiaoDashboard')
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: '操作成功!'
|
||||
})
|
||||
})
|
||||
}
|
||||
private initPlugins(client: SocketIOSocket) {
|
||||
this.initNativePlugins(client)
|
||||
this.initScriptPlugins(client)
|
||||
}
|
||||
private initServices(client: SocketIOSocket) {
|
||||
client.on('services.plugins', (callback) => {
|
||||
this.wrapper(callback, http.get('http://w.yumc.pw/api/free_plugin/find').data.map(plugin => {
|
||||
let installed = this.nativePluginManager.get(plugin.name)
|
||||
if (installed) {
|
||||
plugin.installed = true
|
||||
plugin.installedVersion = installed.version
|
||||
}
|
||||
plugin.installed = this.nativePluginManager.has(plugin.name)
|
||||
return plugin
|
||||
}))
|
||||
})
|
||||
client.on('services.plugins.install', ({ name }, callback) => {
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 安装成功!`
|
||||
})
|
||||
})
|
||||
client.on('services.plugins.update', ({ name }, callback) => {
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 安装成功!`
|
||||
})
|
||||
})
|
||||
}
|
||||
private initNativePlugins(client: SocketIOSocket) {
|
||||
client.on('plugins.natives', callback => {
|
||||
this.wrapper(callback, this.nativePluginManager.list().map(plugin => {
|
||||
plugin.status = plugin.enable ? 1 : 0
|
||||
return plugin
|
||||
}))
|
||||
})
|
||||
client.on('plugins.natives.reload', ({ name }, callback) => {
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 不存在`
|
||||
})
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 安装完成`
|
||||
})
|
||||
})
|
||||
client.on('plugins.natives.install', ({ name }, callback) => {
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 不存在`
|
||||
})
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 安装完成`
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
private initScriptPlugins(client: SocketIOSocket) {
|
||||
client.on('plugins.scripts', (callback) => {
|
||||
let plugins = []
|
||||
this.pluginManager.getPlugins().forEach((plugin) => {
|
||||
plugins.push({
|
||||
name: plugin.description.name,
|
||||
version: plugin.description.version,
|
||||
author: plugin.description.author,
|
||||
source: plugin.description.source.toString().replace(root, ''),
|
||||
type: plugin.description.type,
|
||||
scanner: plugin.description.loadMetadata.scanner.type,
|
||||
loader: plugin.description.loadMetadata.loader.type,
|
||||
status: 1
|
||||
})
|
||||
})
|
||||
this.wrapper(callback, {
|
||||
items: plugins,
|
||||
total: plugins.length
|
||||
})
|
||||
})
|
||||
client.on('plugins.scripts.reload', ({ name }, callback) => {
|
||||
let plugin: any = this.pluginManager.getPlugin(name)
|
||||
if (!plugin) {
|
||||
return callback({
|
||||
status: 404,
|
||||
msg: `插件 ${name} 不存在`
|
||||
})
|
||||
}
|
||||
this.pluginManager.reload(plugin)
|
||||
return callback({
|
||||
status: 0,
|
||||
msg: `插件 ${name} 重载完成`
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import { Server as SocketIOServer, Socket as SocketIOSocket, Namespace } from '@
|
||||
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
|
||||
import type { MiaoConsole } from './MiaoConsole'
|
||||
|
||||
const FileFilter = Java.type('java.io.FileFilter')
|
||||
const ByteArrayInputStream = java.io.ByteArrayInputStream
|
||||
const ByteArrayOutputStream = java.io.ByteArrayOutputStream
|
||||
@@ -16,14 +18,13 @@ const StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
|
||||
const GZIPInputStream = Java.type('java.util.zip.GZIPInputStream')
|
||||
const ByteArray = Java.type("byte[]")
|
||||
|
||||
@JSPlugin({ prefix: 'Explorer', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
@JSPlugin({ prefix: 'Explorer', version: '1.0.0', author: 'MiaoWoo', source: __filename, depends: ['MiaoConsole'] })
|
||||
export class MiaoExplorer extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private Server: server.Server
|
||||
@Autowired()
|
||||
private pluginManager: plugin.PluginManager
|
||||
|
||||
private token: string
|
||||
private namespace: any
|
||||
private chunkCacheMap: Map<string, Array<string>>
|
||||
|
||||
@@ -32,39 +33,20 @@ export class MiaoExplorer extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
enable() {
|
||||
let consolePlugin: any = this.pluginManager.getPlugin('MiaoConsole')
|
||||
if (consolePlugin.socketIOServer) {
|
||||
this.startWebSocketServer(consolePlugin.socketIOServer)
|
||||
} else {
|
||||
process.on('websocket.create', (server: SocketIOServer) => {
|
||||
this.startWebSocketServer(server)
|
||||
let consolePlugin: MiaoConsole = this.pluginManager.getPlugin('MiaoConsole') as MiaoConsole
|
||||
consolePlugin.registryWebSocketNamespace('/MiaoExplorer', (namespace: Namespace) => {
|
||||
this.namespace = namespace
|
||||
this.namespace.on('connection', (client: SocketIOSocket) => {
|
||||
if (consolePlugin.checkWebSocketClient(client)) {
|
||||
this.initWebSocketClient(client)
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private startWebSocketServer(server: SocketIOServer) {
|
||||
let consolePlugin: any = this.pluginManager.getPlugin('MiaoConsole')
|
||||
this.token = consolePlugin.token
|
||||
this.namespace = server.of('/MiaoExplorer')
|
||||
this.namespace.on('connection', (client: SocketIOSocket) => {
|
||||
if (!this.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
if (this.token != client.handshake.query.token) {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
||||
client.emit('unauthorized', () => client.disconnect(true))
|
||||
return
|
||||
}
|
||||
this.initWebSocketClient(client)
|
||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.namespace.removeAllListeners('connect')
|
||||
this.namespace.close()
|
||||
this.namespace?.close()
|
||||
}
|
||||
|
||||
private readDir(dir) {
|
||||
@@ -161,15 +143,4 @@ export class MiaoExplorer extends interfaces.Plugin {
|
||||
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
||||
})
|
||||
}
|
||||
|
||||
@Cmd()
|
||||
msme(sender: any, command: string, args: string[]) {
|
||||
this.logger.log(sender, command, args)
|
||||
sender.sendMessage(JSON.stringify({ command, ...args }))
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmsme(_sender: any, _command: string, _args: string[]) {
|
||||
return ['world']
|
||||
}
|
||||
}
|
||||
|
||||
155
packages/plugins/src/MiaoLink.ts
Normal file
155
packages/plugins/src/MiaoLink.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { server, task } from '@ccms/api'
|
||||
import { Autowired } from '@ccms/container'
|
||||
import { Cmd, JSPlugin, Tab, interfaces, PluginConfig, Config } from '@ccms/plugin'
|
||||
|
||||
import * as fs from '@ccms/common/dist/fs'
|
||||
import http from '@ccms/common/dist/http'
|
||||
|
||||
import * as base64 from 'base64-js'
|
||||
|
||||
const Runtime: typeof java.lang.Runtime = Java.type('java.lang.Runtime')
|
||||
const Thread = Java.type('java.lang.Thread')
|
||||
|
||||
const defaultConfig = {
|
||||
id: 0,
|
||||
vkey: ''
|
||||
}
|
||||
|
||||
@JSPlugin({ name: 'MiaoLink', version: '1.0.2', author: 'MiaoWoo', source: __filename })
|
||||
export class MiaoLink extends interfaces.Plugin {
|
||||
@Autowired(task.TaskManager)
|
||||
private task: task.TaskManager
|
||||
@Autowired(server.Server)
|
||||
private server: server.Server
|
||||
|
||||
@Config()
|
||||
private config: PluginConfig & typeof defaultConfig = defaultConfig
|
||||
|
||||
private isWindows = false
|
||||
private clientName: string = 'npc'
|
||||
private client: string = ''
|
||||
private port: number = 0
|
||||
private npc: any
|
||||
|
||||
load() {
|
||||
this.isWindows = process.platform == 'win32' || process.platform.toLowerCase().startsWith('windows')
|
||||
if (this.isWindows) {
|
||||
this.logger.console('§a当前运行于Windows服务器...')
|
||||
this.clientName = "npc.exe"
|
||||
} else {
|
||||
this.logger.console('§a当前运行于Linux服务器...')
|
||||
}
|
||||
}
|
||||
|
||||
bukkitload() {
|
||||
this.port = org.bukkit.Bukkit.getPort()
|
||||
}
|
||||
|
||||
spongeload() {
|
||||
this.logger.console('§4Sponge暂不支持端口映射!')
|
||||
}
|
||||
|
||||
bungeeload() {
|
||||
let server: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxyServer()
|
||||
this.port = server.getConfig().getListeners()[0].getQueryPort()
|
||||
}
|
||||
|
||||
enable() {
|
||||
if (!this.config.vkey) {
|
||||
return this.logger.console('§4服务器尚未绑定 取消自动映射!')
|
||||
}
|
||||
this.cmdconnect(this.server.getConsoleSender())
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.cmddisconnect(this.server.getConsoleSender())
|
||||
}
|
||||
|
||||
@Cmd({ autoMain: true })
|
||||
mlink() { }
|
||||
|
||||
cmdconnect(sender: any, secret?: string) {
|
||||
if (secret) {
|
||||
let configStr = String.fromCharCode(...Array.from(base64.toByteArray(secret)))
|
||||
let config = JSON.parse(configStr)
|
||||
this.config.id = config.id
|
||||
this.config.vkey = config.vkey
|
||||
this.config.save()
|
||||
}
|
||||
this.startClient(sender)
|
||||
}
|
||||
|
||||
cmddisconnect(sender: any) {
|
||||
if (!this.npc || !this.npc.isAlive()) {
|
||||
return this.logger.sender(sender, '§4客户端尚未运行 跳过关闭流程...')
|
||||
}
|
||||
this.logger.sender(sender, '§6已发送关闭客户端指令...')
|
||||
this.npc.destroy()
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmlink(_sender: any, _command: string, _args: string[]) {
|
||||
}
|
||||
|
||||
startClient(sender: any, id: number = this.config.id, vkey: string = this.config.vkey) {
|
||||
if (!this.port) {
|
||||
return this.logger.sender(sender, '§4服务器端口获取失败 取消自动映射!')
|
||||
}
|
||||
if (!id || !vkey) {
|
||||
return this.logger.sender(sender, '§4服务器尚未配置 取消自动映射!')
|
||||
}
|
||||
if (this.npc && this.npc.isAlive()) {
|
||||
this.npc.destroy()
|
||||
}
|
||||
this.task.create(() => {
|
||||
this.logger.sender(sender, `§6获取到服务器端口: §3${this.port} §a开始映射端口!`)
|
||||
let client = this.query(id, vkey, this.port)
|
||||
let node = client.node
|
||||
let tunnel = client.tunnel
|
||||
this.client = fs.concat(__dirname, 'MiaoLink', this.clientName)
|
||||
this.download(sender)
|
||||
try {
|
||||
this.npc = Runtime.getRuntime().exec(`${this.client} -server=${node.bridge} -vkey=${vkey} -type=tcp`)
|
||||
this.logger.sender(sender, `§a服务器端口映射成功! §6访问地址: §3${node.address}:${tunnel.port}`)
|
||||
return this.logger.console(`§4客户端已结束运行 退出代码: ${this.npc.waitFor()} 映射关闭!`)
|
||||
} catch (error) {
|
||||
this.logger.sender(sender, `§c服务器端口映射失败! §4ERROR: ${error}`)
|
||||
console.ex(error)
|
||||
}
|
||||
}, this).async().later(5).submit()
|
||||
}
|
||||
|
||||
download(sender: any) {
|
||||
try {
|
||||
if (!fs.exists(this.client)) {
|
||||
this.logger.sender(sender, '§c客户端文件不存在 开始下载客户端...')
|
||||
let temp = this.client + '.tmp'
|
||||
http.download("https://static.c5mc.cn/" + this.clientName, temp)
|
||||
fs.move(temp, this.client, true)
|
||||
if (!this.isWindows) {
|
||||
this.logger.sender(sender, '§a当前处于Linux环境 赋予可执行权限...')
|
||||
Runtime.getRuntime().exec(`chmod +x ${this.client}`)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
Thread.sleep(500)
|
||||
this.download(sender)
|
||||
}
|
||||
}
|
||||
|
||||
query(id: number, vkey: string, target: number) {
|
||||
let result = this.post(`/client?id=${id}&vkey=${vkey}&target=${target}`)
|
||||
if (result.code != 200) {
|
||||
throw new Error('§4客户端查询失败: ' + result.msg)
|
||||
}
|
||||
return result.data
|
||||
}
|
||||
|
||||
post(path, data = {}) {
|
||||
return http.post("https://nps.yumc.pw/api" + path, data)
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,11 @@
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { particle } from '@ccms/api'
|
||||
import { constants, particle } from '@ccms/api'
|
||||
import { Autowired } from '@ccms/container'
|
||||
import { Cmd, JSPlugin, Tab, interfaces } from '@ccms/plugin'
|
||||
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename })
|
||||
export class MiaoParticle extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private particleManager: particle.ParticleManager
|
||||
|
||||
@@ -10,13 +10,8 @@ import type { MiaoReward } from './MiaoReward'
|
||||
import http from '@ccms/common/dist/http'
|
||||
import * as CryptoJS from "crypto-js"
|
||||
|
||||
const Thread = java.lang.Thread
|
||||
const Thread = Java.type('java.lang.Thread')
|
||||
|
||||
interface PlayerPointsAPI {
|
||||
look(name: string)
|
||||
give(name: string, amount: number)
|
||||
take(name: string, amount: number)
|
||||
}
|
||||
interface App {
|
||||
appid: string
|
||||
appname: string
|
||||
@@ -78,7 +73,7 @@ const defaultConfig = {
|
||||
}
|
||||
}
|
||||
|
||||
@JSPlugin({ version: '1.5.0', author: 'MiaoWoo', source: __filename, servers: [constants.ServerType.Bukkit], depends: ['MiaoReward'], nativeDepends: ['PlaceholderAPI', 'ProtocolLib'] })
|
||||
@JSPlugin({ version: '1.6.6', author: 'MiaoWoo', source: __filename, servers: [constants.ServerType.Bukkit], depends: ['MiaoReward'], nativeDepends: ['PlaceholderAPI', 'ProtocolLib'] })
|
||||
export class MiaoPay extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private server: server.Server
|
||||
@@ -109,14 +104,22 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
enable() {
|
||||
if (!this.MiaoReward) { return this.logger.error('当前脚本插件需要 MiaoReward 作为前置脚本插件!') }
|
||||
if (!this.config.id || !this.config.secret) { return this.logger.console('§4尚未配置商户信息 将无法正常收款!') }
|
||||
let info = this.httpPost('/apps', { id: this.config.id })
|
||||
this.initAppInfo()
|
||||
}
|
||||
|
||||
private initAppInfo() {
|
||||
let info = this.httpPost('/apps', { id: this.config.id }, 10)
|
||||
if (info.code == 200) {
|
||||
this.appInfo = info.data
|
||||
this.config.ratio = this.appInfo.ratio
|
||||
this.config.coinName = this.appInfo.coin_name
|
||||
if (this.config.name == this.appInfo.appname) {
|
||||
this.config.name = ''
|
||||
this.config.save()
|
||||
}
|
||||
} else {
|
||||
this.logger.console('§4初始化支付系统失败 请检查配置是否正确!')
|
||||
this.logger.console('§c服务器返回异常: §4' + info.msg)
|
||||
this.logger.console('§4初始化支付系统失败 请检查配置或网络是否正常!')
|
||||
this.logger.console('§c返回异常: §4' + info.msg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,14 +135,17 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
|
||||
cmdpay(sender: org.bukkit.entity.Player, amount: number = 0) {
|
||||
if (!sender.getItemInHand) { return this.logger.sender(sender, '§4控制台无法执行此命令!') }
|
||||
if (!this.appInfo) {
|
||||
return this.logger.sender(sender, '§4当前服务器尚未配置 请联系管理员配置MiaoPay!')
|
||||
}
|
||||
if (!this.config.id || !this.config.secret) { return this.logger.sender(sender, '§c当前服务器尚未配置 请联系管理员配置支付密钥!') }
|
||||
if (!this.appInfo) {
|
||||
this.initAppInfo()
|
||||
return this.logger.sender(sender, '§6支付系统初始化中 请稍候重试...')
|
||||
}
|
||||
if (this.cacheMap.has(sender.getName())) {
|
||||
this.logger.sender(sender, '§c您有一笔订单尚未完成 请完成支付或等待订单超时!')
|
||||
let sync = this.cacheSyncMap.get(sender.getName())
|
||||
if (!sync.cancelled) { return }
|
||||
if (sync.scaned) { return }
|
||||
sync.scaned = true
|
||||
Thread.sleep(1100)
|
||||
sync.scaned = false
|
||||
sync.left = (sync.paying ? 100 : 55) - (Math.round(Date.now() / 1000) - sync.start)
|
||||
let order = this.cacheMap.get(sender.getName())
|
||||
@@ -147,13 +153,17 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
return
|
||||
}
|
||||
if (amount < 1) { return this.logger.sender(sender, `§c充值异常 §4充值金额不得小于 1 ${this.config.coinName}!`) }
|
||||
if (amount / this.config.ratio > 5000) { return this.logger.sender(sender, `§c充值异常 §4充值金额不得大于 ${this.config.ratio * 5000} ${this.config.coinName}!`) }
|
||||
if (amount / this.config.ratio > 5000) { return this.logger.sender(sender, `§c充值异常 §4充值金额不得大于 ${this.config.ratio * 3000} ${this.config.coinName}!`) }
|
||||
if (amount != Math.round(amount)) { return this.logger.sender(sender, `§c充值异常 §4充值金额必须为整数!`) }
|
||||
try {
|
||||
this.getPlayerAmount(sender)
|
||||
} catch (error) {
|
||||
return this.logger.sender(sender, error.message || error)
|
||||
}
|
||||
this.taskManager.create(() => this.createOrderByPlayer(sender, amount)).async().submit()
|
||||
}
|
||||
|
||||
private createOrderByPlayer(sender: org.bukkit.entity.Player, amount: number = 0) {
|
||||
this.MiaoReward.sendTitle(sender, `§6充值 §a${amount} §6${this.config.coinName}`, '§c正在请求充值二维码 请稍候...')
|
||||
let sync: any = { scaned: false, start: Math.round(Date.now() / 1000) }
|
||||
let order = this.createOrder(sender, amount)
|
||||
@@ -193,27 +203,6 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
this.MiaoReward.clearTitle(sender)
|
||||
}
|
||||
|
||||
cmdquery(sender: org.bukkit.entity.Player, id: string) {
|
||||
if (!id) { return this.logger.sender(sender, '§c请输入订单号!') }
|
||||
this.taskManager.create(() => {
|
||||
let result = this.queryOrder(id, sender.getName(), sender.getUniqueId().toString())
|
||||
if (result.code != 200) { return this.logger.sender(sender, `§c查询异常! §4ERROR: ${result.msg}`) }
|
||||
let order = result.data
|
||||
this.logger.sender(sender, [
|
||||
`§6商户名称: §3${order.appname}`,
|
||||
`§6订单号: §3${id}`,
|
||||
`§6商品: §b${order.subject}`,
|
||||
`§6金额: §e${order.amount}`,
|
||||
`§6玩家: §a${order.username}`,
|
||||
`§6状态: §c${order.status}`,
|
||||
])
|
||||
if (order.status > 1 && order.status < 4) {
|
||||
this.logger.sender(sender, `§3当前订单已支付 尚未完成充值 开始补单操作...`)
|
||||
this.recharge(sender, order)
|
||||
}
|
||||
}).async().submit()
|
||||
}
|
||||
|
||||
cmdcheck(sender: org.bukkit.entity.Player, force = 1) {
|
||||
if (this.checkSet.has(sender.getName())) {
|
||||
return this.logger.sender(sender, '§c检查任务执行中 请稍候...')
|
||||
@@ -246,16 +235,18 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
let order_id = order.order_id
|
||||
let amount = order.amount
|
||||
let point = this.safeMultiply(amount, this.config.ratio)
|
||||
if (!sender.isOnline()) { return }
|
||||
let finish = this.preFinishOrder(order_id)
|
||||
if (finish.code != 200) {
|
||||
this.sendError(sender, order_id, amount, '§4充值预标记异常!')
|
||||
this.sendError(sender, order_id, amount, '充值预标记异常!')
|
||||
return this.logger.console(`§c充值系统异常 订单 §3${order_id} 预标记异常! §4${this.config.coinName}已停止充值 §c请手动补单!`)
|
||||
}
|
||||
this.taskManager.callSyncMethod(() => {
|
||||
if (!sender.isOnline()) { return this.errorOrder(order_id, "充值前玩家掉线 请重置标记!") }
|
||||
let prePoint = this.getPlayerAmount(sender)
|
||||
let command = this.config.command.replace('%player_name%', sender.getName()).replace('%amount%', `${point}`).replace('%remark%', `${order_id}`)
|
||||
if (!this.server.dispatchConsoleCommand(command)) {
|
||||
return this.sendError(sender, order_id, amount, '§4充值命令执行异常!')
|
||||
return this.sendError(sender, order_id, amount, '充值命令执行异常!')
|
||||
}
|
||||
this.checkRecharge(sender, order_id, amount, prePoint, point)
|
||||
})
|
||||
@@ -269,10 +260,9 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
|
||||
private checkRecharge(sender: org.bukkit.entity.Player, order_id: string, amount: number, prePoint: number, point: number) {
|
||||
this.taskManager.create(() => {
|
||||
if (!sender.isOnline()) { return this.errorOrder(order_id, "充值后玩家掉线 请标记已兑换!") }
|
||||
let nowPoint = this.checkNowPoint(sender, point, prePoint)
|
||||
if (nowPoint === false) {
|
||||
return this.sendError(sender, order_id, amount, '§4充值结果检测异常!')
|
||||
}
|
||||
if (nowPoint === false) { return this.sendError(sender, order_id, amount, '充值结果检测异常!') }
|
||||
this.logger.sender(sender, [
|
||||
`§6充值 §a${point} §6${this.config.coinName} §a成功 §6当前账户余额: §3${nowPoint} §6${this.config.coinName}`,
|
||||
`§c如出现未到账的情况 请联系管理员!`
|
||||
@@ -280,29 +270,18 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
this.rewardOrder(sender, order_id, point)
|
||||
let finish = this.finishOrder(order_id)
|
||||
if (finish.code != 200) {
|
||||
return this.logger.console(`§c充值系统异常 订单 §3${order_id} 完成标记异常! §4${this.config.coinName}可能重复到账!`)
|
||||
this.errorOrder(order_id, '充值完成标记异常 请到后台标记为已兑换!')
|
||||
return this.logger.console(`§c充值系统异常 订单 §3${order_id} §c完成标记异常! §a请到后台标记为已兑换! §4否则${this.config.coinName}可能重复到账!`)
|
||||
}
|
||||
}).async().submit()
|
||||
}
|
||||
|
||||
private checkNowPoint(sender: org.bukkit.entity.Player, point: number, prePoint: number) {
|
||||
private checkNowPoint(sender: org.bukkit.entity.Player, point: number, prePoint: number, times: number = 1) {
|
||||
if (times > 3) { return false }
|
||||
let nowPoint = this.getPlayerAmount(sender)
|
||||
if (nowPoint == prePoint + point) {
|
||||
return nowPoint
|
||||
}
|
||||
Thread.sleep(100)
|
||||
nowPoint = this.getPlayerAmount(sender)
|
||||
if (nowPoint == prePoint + point) {
|
||||
return nowPoint
|
||||
}
|
||||
Thread.sleep(200)
|
||||
nowPoint = this.getPlayerAmount(sender)
|
||||
if (nowPoint == prePoint + point) {
|
||||
return nowPoint
|
||||
}
|
||||
Thread.sleep(300)
|
||||
nowPoint = this.getPlayerAmount(sender)
|
||||
return false
|
||||
if (nowPoint == prePoint + point) { return nowPoint }
|
||||
Thread.sleep(times * 100)
|
||||
return this.checkNowPoint(sender, point, prePoint, times++)
|
||||
}
|
||||
|
||||
private rewardOrder(sender, order_id, point) {
|
||||
@@ -333,7 +312,7 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
sendError(sender: org.bukkit.entity.Player, order_id: string, amount: number, error: string) {
|
||||
return this.logger.sender(sender, [
|
||||
this.logger.sender(sender, [
|
||||
`§c========== ${this.config.prefix}§4充值异常 §c==========`,
|
||||
`§6异常订单: §3${order_id}`,
|
||||
`§6订单金额: §3${amount}`,
|
||||
@@ -341,8 +320,10 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
`§6异常账号: §b${sender.getName()}`,
|
||||
`§6异常时间: §a${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`,
|
||||
`§c如果已付款但${this.config.coinName}未到账 请截图发给腐竹!`,
|
||||
`§c可尝试重新登录 或 执行 §3/mpay check §c手动补单!`,
|
||||
`§c========== ${this.config.prefix}§4充值异常 §c==========`,
|
||||
])
|
||||
this.errorOrder(order_id, error)
|
||||
}
|
||||
|
||||
@Tab()
|
||||
@@ -367,15 +348,19 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
private preFinishOrder(id: string) {
|
||||
return this.httpPost('/preFinish', { id })
|
||||
return this.httpPost('/preFinish', { id }, 3)
|
||||
}
|
||||
|
||||
private errorOrder(id: string, error: string) {
|
||||
return this.httpPost('/error', { id, error }, 3)
|
||||
}
|
||||
|
||||
private finishOrder(id: string) {
|
||||
return this.httpPost('/finish', { id })
|
||||
return this.httpPost('/finish', { id }, 3)
|
||||
}
|
||||
|
||||
private createOrder(sender: org.bukkit.entity.Player, amount: number): Order {
|
||||
let serverName = this.appInfo?.appname
|
||||
let serverName = this.appInfo.appname
|
||||
if (this.config.name) { serverName = `${serverName}(${this.config.name})` }
|
||||
let result = this.httpPost('/create', {
|
||||
subject: `${serverName} 充值 ${amount} ${this.config.coinName}`,
|
||||
@@ -390,28 +375,38 @@ export class MiaoPay extends interfaces.Plugin {
|
||||
}
|
||||
|
||||
private queryOrder(id: string, username: string, uuid: string) {
|
||||
return this.httpPost('/query', { id, username, uuid })
|
||||
return this.httpPost('/query', { id, username, uuid }, 2)
|
||||
}
|
||||
|
||||
private queryUnconverted(username: string, force: number) {
|
||||
return this.httpPost('/unconverted', { username, force })
|
||||
return this.httpPost('/unconverted', { username, force }, 2)
|
||||
}
|
||||
|
||||
private httpPost(method: string, data: any) {
|
||||
private httpPost(method: string, data: any, retry = 0) {
|
||||
let startTime = Date.now()
|
||||
data.appid = this.config.id
|
||||
data.timestamp = Math.round(Date.now() / 1000)
|
||||
data.nonce = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/x/g, () => (Math.random() * 16 | 0).toString(16))
|
||||
data.sign = this.sign(data)
|
||||
let url = `${this.apiGateWay}/api${method}`
|
||||
let result = http.post(url, data)
|
||||
console.debug(`
|
||||
try {
|
||||
let result = http.post(url, data)
|
||||
console.debug(`
|
||||
====== HTTP POST ======
|
||||
REQUEST URL : ${url}
|
||||
REQUEST DATA: ${JSON.stringify(data)}
|
||||
RESPONSE : ${JSON.stringify(result)}
|
||||
CAST TIME : ${Date.now() - startTime}`)
|
||||
return result
|
||||
return result
|
||||
} catch (error) {
|
||||
if (retry) {
|
||||
Thread.sleep(retry * 10)
|
||||
return this.httpPost(method, data, --retry)
|
||||
}
|
||||
console.console('§4请求支付中心发生异常 请联系管理员处理此问题!')
|
||||
console.ex(error)
|
||||
return { code: 500, msg: '本地网络错误: ' + error.message, data: error }
|
||||
}
|
||||
}
|
||||
|
||||
private http_build_query(params: any) {
|
||||
|
||||
157
packages/plugins/src/MiaoRGBSupport.ts
Normal file
157
packages/plugins/src/MiaoRGBSupport.ts
Normal file
@@ -0,0 +1,157 @@
|
||||
import { constants } from "@ccms/api"
|
||||
import { JSClass } from "@ccms/container"
|
||||
import { interfaces, JSPlugin } from "@ccms/plugin"
|
||||
|
||||
let createPacketAdapterFunction = eval(`
|
||||
function(cls, plugin, type, onPacketSending){
|
||||
return new cls(plugin, type) {
|
||||
onPacketSending: onPacketSending
|
||||
}
|
||||
}
|
||||
`)
|
||||
const Color = Java.type('java.awt.Color')
|
||||
const Pattern = Java.type('java.util.regex.Pattern')
|
||||
const ChatColor = Java.type('net.md_5.bungee.api.ChatColor')
|
||||
|
||||
@JSPlugin({ prefix: 'MRS', version: '1.0.0', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], nativeDepends: ['ProtocolLib'], source: __filename })
|
||||
export class MiaoRGBSupport extends interfaces.Plugin {
|
||||
private supportRGB: boolean = false
|
||||
// 用于匹配 '#FFFFFF' 颜色格式
|
||||
private RGBCOLOR_PATTERN = Pattern.compile("(#[a-fA-F0-9]{6}?)([^#?]*)")
|
||||
// 用于匹配彩虹格式
|
||||
private RAINBOW_PATTERN = Pattern.compile("#RAINBOW([0-9]{1,3})([^#?]*)")
|
||||
|
||||
@JSClass('com.comphenix.protocol.events.PacketAdapter')
|
||||
private PacketAdapter: any
|
||||
@JSClass('com.comphenix.protocol.PacketType')
|
||||
private PacketType: any
|
||||
@JSClass('com.comphenix.protocol.ProtocolLibrary')
|
||||
private ProtocolLibrary: any
|
||||
|
||||
private adapter: any
|
||||
|
||||
load() {
|
||||
try {
|
||||
ChatColor.of('#FFFFFF').toString()
|
||||
this.supportRGB = true
|
||||
this.logger.console('§a检测到兼容RGB的服务端 已启动相关支持...')
|
||||
} catch (error) {
|
||||
this.logger.console('§c当前服务端不支持RGB色彩 Error: ' + error)
|
||||
}
|
||||
}
|
||||
|
||||
enable() {
|
||||
if (this.supportRGB) {
|
||||
this.initPacketAdapter()
|
||||
}
|
||||
}
|
||||
|
||||
disable() {
|
||||
if (this.supportRGB) {
|
||||
this.ProtocolLibrary.getProtocolManager().removePacketListener(this.adapter)
|
||||
}
|
||||
}
|
||||
|
||||
createPacketAdapter(onPacketSending: (event) => void) {
|
||||
return createPacketAdapterFunction(this.PacketAdapter, base.getInstance(), [
|
||||
this.PacketType.Play.Server.CHAT,
|
||||
this.PacketType.Play.Server.SCOREBOARD_OBJECTIVE,
|
||||
this.PacketType.Play.Server.SCOREBOARD_SCORE,
|
||||
this.PacketType.Play.Server.SCOREBOARD_TEAM
|
||||
], onPacketSending)
|
||||
}
|
||||
|
||||
colorJson(jsonObj) {
|
||||
return this.processJson(jsonObj, this.RGBCOLOR_PATTERN, (extras, colors) => {
|
||||
return (value, index) => {
|
||||
extras.push({
|
||||
text: value,
|
||||
color: colors[index]
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
rainbowJson(jsonObj) {
|
||||
return this.processJson(jsonObj, this.RAINBOW_PATTERN, (extras, colors) => {
|
||||
return (value: string, index) => {
|
||||
let textArr = value.split("")
|
||||
let rainbowColors = this.createRainbow(textArr.length, colors[index])
|
||||
textArr.forEach((value, index) => {
|
||||
extras.push({
|
||||
text: value,
|
||||
color: rainbowColors[index]
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private processJson(jsonObj, PATTERN, process: (extras, colors) => (value, index) => void) {
|
||||
let text: string = jsonObj.text
|
||||
if (jsonObj.extra && jsonObj.extra.length) {
|
||||
for (const extra of jsonObj.extra) {
|
||||
this.processJson(extra, PATTERN, process)
|
||||
}
|
||||
}
|
||||
if (!text) { return jsonObj }
|
||||
var matcher = PATTERN.matcher(text)
|
||||
let colors = []
|
||||
let texts = []
|
||||
let lastStart = 0
|
||||
while (matcher.find()) {
|
||||
if (lastStart == 0) {
|
||||
texts.push(text.substr(lastStart, matcher.start()))
|
||||
lastStart = matcher.end()
|
||||
}
|
||||
colors.push(matcher.group(1))
|
||||
texts.push(matcher.group(2))
|
||||
}
|
||||
if (colors.length) {
|
||||
jsonObj.text = ''
|
||||
let extras = []
|
||||
let firstText = texts.shift()
|
||||
if (firstText) { extras.push({ text: firstText }) }
|
||||
texts.forEach(process(extras, colors))
|
||||
if (jsonObj.extra) {
|
||||
jsonObj.extra = extras.concat(jsonObj.extra)
|
||||
} else {
|
||||
jsonObj.extra = extras
|
||||
}
|
||||
}
|
||||
return jsonObj
|
||||
}
|
||||
|
||||
private createRainbow(step, saturation) {
|
||||
var colors = []
|
||||
var colorStep = (1.00 / step)
|
||||
for (var i = 0; i < step; i++) {
|
||||
colors.push("#" + java.lang.String.format("%08x", Color.getHSBColor((colorStep * i), saturation, saturation).getRGB()).substring(2))
|
||||
}
|
||||
return colors
|
||||
}
|
||||
|
||||
initPacketAdapter() {
|
||||
this.adapter = this.createPacketAdapter((event) => {
|
||||
try {
|
||||
if (!event.getPlayer().hasPermission('MiaoRGBSupport.color')) { return }
|
||||
let ccs = event.getPacket().getChatComponents()
|
||||
let size = ccs.size()
|
||||
for (let i = 0; i < size; i++) {
|
||||
let wcc = ccs.read(i)
|
||||
if (wcc == null) { continue }
|
||||
let json = JSON.parse(wcc.getJson())
|
||||
json = this.colorJson(json)
|
||||
if (event.getPlayer().hasPermission('MiaoRGBSupport.rainbow')) {
|
||||
json = this.rainbowJson(json)
|
||||
}
|
||||
wcc.setJson(JSON.stringify(json))
|
||||
ccs.write(i, wcc)
|
||||
}
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
})
|
||||
this.ProtocolLibrary.getProtocolManager().addPacketListener(this.adapter)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
|
||||
import { plugin, server, task } from '@ccms/api'
|
||||
import { constants, plugin, server, task } from '@ccms/api'
|
||||
import { Autowired, JSClass } from '@ccms/container'
|
||||
import { Cmd, Config, interfaces, JSPlugin, Listener, PluginConfig, Tab } from '@ccms/plugin'
|
||||
|
||||
@@ -37,7 +37,7 @@ const defaultConfig = {
|
||||
}
|
||||
}
|
||||
|
||||
@JSPlugin({ version: '1.0.1', author: 'MiaoWoo', source: __filename, depends: ['MiaoReward'], nativeDepends: ['PlaceholderAPI'] })
|
||||
@JSPlugin({ version: '1.0.1', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], depends: ['MiaoReward'], nativeDepends: ['PlaceholderAPI'], source: __filename })
|
||||
export class MiaoRebate extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private server: server.Server
|
||||
|
||||
@@ -68,7 +68,7 @@ const defaultConfig = {
|
||||
joinTip: true
|
||||
}
|
||||
|
||||
@JSPlugin({ prefix: 'MRD', version: '1.5.9', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], nativeDepends: ['ProtocolLib', 'PlaceholderAPI'], source: __filename })
|
||||
@JSPlugin({ prefix: 'MRD', version: '1.6.0', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], nativeDepends: ['ProtocolLib', 'PlaceholderAPI'], source: __filename })
|
||||
export class MiaoReward extends interfaces.Plugin {
|
||||
public serverInfo: ServerInfo
|
||||
private notifyError = true
|
||||
@@ -79,6 +79,7 @@ export class MiaoReward extends interfaces.Plugin {
|
||||
private playerInfoCache = new Map<string, UserInfo>()
|
||||
|
||||
private downgrade = false
|
||||
private subversion = 0
|
||||
|
||||
@Autowired()
|
||||
private chat: chat.Chat
|
||||
@@ -132,6 +133,7 @@ export class MiaoReward extends interfaces.Plugin {
|
||||
//@ts-ignore
|
||||
this.logger.prefix = this.config.prefix
|
||||
this.downgrade = this.Bukkit.server.class.name.split('.')[3] == "v1_7_R4"
|
||||
this.subversion = parseInt(this.Bukkit.server.class.name.split('.')[3].split('_')[1])
|
||||
this.updateServerInfo(null, () => this.updateOnlinePlayersInfo())
|
||||
}
|
||||
|
||||
@@ -248,29 +250,50 @@ export class MiaoReward extends interfaces.Plugin {
|
||||
if (!this.ProtocolLibrary) {
|
||||
return this.logger.console(`§4服务器未安装 ProtocolLib 无法扫码功能 请安装后重试!`)
|
||||
}
|
||||
this.adapter = this.createPacketAdapter((event) => {
|
||||
let writer = undefined
|
||||
if (this.downgrade) {
|
||||
writer = (packet, bytes) => {
|
||||
// let xbytes = new Bytes(131)
|
||||
let origin = packet.getByteArrays().read(0)
|
||||
// xbytes[1] = origin[1]
|
||||
// xbytes[2] = origin[2]
|
||||
for (let y = 0; y < 128; ++y) {
|
||||
origin[y + 3] = bytes[y * 128 + origin[1]]
|
||||
}
|
||||
packet.getByteArrays().write(0, origin)
|
||||
}
|
||||
} else if (this.subversion < 17) {
|
||||
writer = (packet, bytes) => {
|
||||
packet.getByteArrays().write(0, bytes)
|
||||
packet.getIntegers().write(3, 128)
|
||||
packet.getIntegers().write(4, 128)
|
||||
}
|
||||
} else if (this.subversion > 16) {
|
||||
writer = (packet, bytes) => {
|
||||
let b = packet.getModifier().read(4)
|
||||
if (b) {
|
||||
let bi = Java.type(b.class.name)
|
||||
packet.getModifier().write(4, new bi(b.a, b.b, 128, 128, bytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
if (writer) {
|
||||
this.adapter = this.createPacketAdapter(this.getPacketAdapter(writer))
|
||||
this.ProtocolLibrary.getProtocolManager().addPacketListener(this.adapter)
|
||||
} else {
|
||||
console.console('§4当前服务器不支持虚拟地图发包 将无法使用扫码功能!')
|
||||
}
|
||||
}
|
||||
|
||||
private getPacketAdapter(writer: (packet: any, bytes: number[]) => void) {
|
||||
return (event) => {
|
||||
let integers = event.getPacket().getIntegers().getValues()
|
||||
let mapId = integers.get(0)
|
||||
let player = event.getPlayer()
|
||||
if (mapId == this.zeroMapView.getId() && this.playerImageCache.has(player.getName())) {
|
||||
let bytes = this.playerImageCache.get(player.getName())
|
||||
if (!this.downgrade) {
|
||||
event.getPacket().getByteArrays().write(0, bytes)
|
||||
event.getPacket().getIntegers().write(3, 128)
|
||||
event.getPacket().getIntegers().write(4, 128)
|
||||
} else {
|
||||
// let xbytes = new Bytes(131)
|
||||
let origin = event.getPacket().getByteArrays().read(0)
|
||||
// xbytes[1] = origin[1]
|
||||
// xbytes[2] = origin[2]
|
||||
for (let y = 0; y < 128; ++y) {
|
||||
origin[y + 3] = bytes[y * 128 + origin[1]]
|
||||
}
|
||||
event.getPacket().getByteArrays().write(0, origin)
|
||||
}
|
||||
writer(event.getPacket(), this.playerImageCache.get(player.getName()))
|
||||
}
|
||||
})
|
||||
this.ProtocolLibrary.getProtocolManager().addPacketListener(this.adapter)
|
||||
}
|
||||
}
|
||||
|
||||
private sendWindowItems(player: org.bukkit.entity.Player, mapItem: any) {
|
||||
@@ -640,6 +663,7 @@ export class MiaoReward extends interfaces.Plugin {
|
||||
sync.cancelled = false
|
||||
let task = this.taskManager.create(() => {
|
||||
try {
|
||||
console.log(JSON.stringify(sync))
|
||||
if (sync.scaned || !sender.isOnline() || !this.isHoldQrCodeItem(sender) || --sync.left < 0) {
|
||||
if (sync.left < 0) {
|
||||
this.logger.sender(sender, '§c二维码已过期 请重新获取 如已扫码请忽略!')
|
||||
|
||||
@@ -1,41 +1,152 @@
|
||||
/// <reference types="@javatypes/bungee-api" />
|
||||
/// <reference types="@javatypes/bukkit-api" />
|
||||
/// <reference types="@javatypes/sponge-api" />
|
||||
/// <reference types="typescript" />
|
||||
// @ts-ignore
|
||||
require.clear('websocket/client')
|
||||
import { server } from '@ccms/api'
|
||||
import { Autowired, Container, ContainerInstance } from '@ccms/container'
|
||||
import { Cmd, JSPlugin, Tab, interfaces, PluginConfig, Config } from '@ccms/plugin'
|
||||
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
import { constants, server } from '@ccms/api'
|
||||
import { Autowired, JSClass } from '@ccms/container'
|
||||
import { Cmd, JSPlugin, Tab, interfaces, PluginConfig, Config, Listener } from '@ccms/plugin'
|
||||
import { WebSocket } from '@ccms/websocket'
|
||||
|
||||
const Thread = Java.type('java.lang.Thread')
|
||||
const ChatColor = Java.type('org.bukkit.ChatColor')
|
||||
|
||||
const defaultConfig = {
|
||||
version: 1,
|
||||
address: '',
|
||||
token: ''
|
||||
token: '',
|
||||
group_id: '',
|
||||
admin_id: '',
|
||||
message: {
|
||||
join: "玩家: %player_name% 加入了服务器!",
|
||||
quit: "玩家: %player_name% 退出了服务器!",
|
||||
chat: "%player_name%: ",
|
||||
group: "&6[&c服务器群&6] &b%sender_nickname%&6(&a%sender_user_id%&6)&r: "
|
||||
}
|
||||
}
|
||||
//https://github3.mk-proxy.ml/-----https://github.com/Mrs4s/go-cqhttp/releases/download/v0.9.34/go-cqhttp-v0.9.34-linux-amd64
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
||||
|
||||
interface RobotConfig {
|
||||
address: string,
|
||||
token: string,
|
||||
timeout: number
|
||||
}
|
||||
|
||||
interface PlaceholderAPI {
|
||||
registerPlaceholderHook: (key: string, onPlaceholderRequest: (player, s) => string) => void
|
||||
unregisterPlaceholderHook: (key: string) => void
|
||||
setPlaceholders: (player: any, str: string) => string
|
||||
}
|
||||
|
||||
class Robot extends EventEmitter {
|
||||
private config: RobotConfig
|
||||
|
||||
private websocket: WebSocket
|
||||
|
||||
private invokeCount = 1;
|
||||
private apiResultCache = [];
|
||||
|
||||
constructor(config: RobotConfig) {
|
||||
super()
|
||||
this.config = config
|
||||
}
|
||||
|
||||
sleep(ms) {
|
||||
Thread.sleep(ms)
|
||||
}
|
||||
|
||||
invoke(action, params) {
|
||||
if (this.websocket.readyState != WebSocket.OPEN) { throw new Error('client not connect!') }
|
||||
let startTime: number = new Date().getTime()
|
||||
let request = { action, params, echo: this.invokeCount++ }
|
||||
this.websocket.send(JSON.stringify(request))
|
||||
while (startTime + this.config.timeout > new Date().getTime()) {
|
||||
if (this.apiResultCache[request.echo]) {
|
||||
let result = this.apiResultCache[request.echo]
|
||||
delete this.apiResultCache[request.echo]
|
||||
if ((result.status === "ok" && result.retcode !== 0) && (result.status === "async" && result !== 1)) {
|
||||
throw Error(`Invoke API Error! Response ${JSON.stringify(result)}`)
|
||||
}
|
||||
return result.data
|
||||
}
|
||||
this.sleep(50)
|
||||
}
|
||||
throw Error(`Invoke API Timeout! Request ${JSON.stringify(request)}`)
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.websocket = new WebSocket(this.config.address, '', { Authorization: `Bearer ${this.config.token}` })
|
||||
this.websocket.onopen = () => {
|
||||
this.emit('connect')
|
||||
}
|
||||
this.websocket.onmessage = (event) => {
|
||||
let robotEvent = JSON.parse(event.data)
|
||||
if (robotEvent.post_type == "meta_event") { return }
|
||||
if (robotEvent.post_type) {
|
||||
this.emit(robotEvent.post_type, robotEvent)
|
||||
}
|
||||
}
|
||||
this.websocket.onclose = (event) => {
|
||||
this.emit('close', event)
|
||||
}
|
||||
this.websocket.onerror = (event) => {
|
||||
this.emit('error', event)
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(reason = '') {
|
||||
if (this.websocket) {
|
||||
this.websocket.close(0, reason)
|
||||
}
|
||||
}
|
||||
|
||||
sendGroupMessage(group_id, message) {
|
||||
this.websocket.send(JSON.stringify({
|
||||
action: "send_msg",
|
||||
params: { group_id, message }
|
||||
}))
|
||||
}
|
||||
|
||||
sendPrivateMessage(user_id, message) {
|
||||
this.websocket.send(JSON.stringify({
|
||||
action: "send_msg",
|
||||
params: { user_id, message }
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@JSPlugin({ version: '1.0.0', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename, nativeDepends: ['PlaceholderAPI'] })
|
||||
export class MiaoRobot extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private server: server.Server
|
||||
|
||||
private client: WebSocket
|
||||
@JSClass('me.clip.placeholderapi.PlaceholderAPI')
|
||||
private PlaceholderAPI: PlaceholderAPI
|
||||
|
||||
private robot: Robot
|
||||
|
||||
@Config()
|
||||
private config: PluginConfig & typeof defaultConfig = defaultConfig
|
||||
|
||||
load() {
|
||||
}
|
||||
|
||||
private downloadRobot() {
|
||||
//https://api.github.com/repos/Mrs4s/go-cqhttp/releases?per_page=1&page=1
|
||||
this.logger.prefix = ''
|
||||
}
|
||||
|
||||
enable() {
|
||||
if (this.config.address && this.config.token) {
|
||||
this.cmdconnect(this.server.getConsoleSender())
|
||||
if (!this.config.group_id) {
|
||||
this.logger.console('§c机器人尚未配置绑定服务器群 部分功能将无法使用!')
|
||||
}
|
||||
} else {
|
||||
this.logger.console('§c机器人尚未配置 请参照帖子内容配置机器人!')
|
||||
}
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.cmdclose(this.server.getConsoleSender())
|
||||
if (this.robot) {
|
||||
this.cmdclose(this.server.getConsoleSender())
|
||||
}
|
||||
}
|
||||
|
||||
@Cmd({ autoMain: true })
|
||||
@@ -46,50 +157,64 @@ export class MiaoRobot extends interfaces.Plugin {
|
||||
return this.logger.sender(sender, '§4错误 请配置服务器地址和Token!')
|
||||
}
|
||||
this.cmdclose(sender)
|
||||
try {
|
||||
this.client = new WebSocket(address, '', { Authorization: `Bearer ${token}` })
|
||||
this.initRobot(this.client)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
this.initRobot(sender)
|
||||
}
|
||||
|
||||
private initRobot(client: WebSocket) {
|
||||
client.onopen = () => {
|
||||
this.logger.console(`§3连接到 §b${client.url} §a成功!`)
|
||||
}
|
||||
client.onmessage = (event) => {
|
||||
let messageEvent = JSON.parse(event.data)
|
||||
switch (messageEvent.post_type) {
|
||||
case "message":
|
||||
this.logger.console(`§6接收到 §3群 §b${messageEvent.group_id} §2成员 §a${messageEvent.sender.nickname} §6的消息: §r${messageEvent.message}`)
|
||||
break
|
||||
initRobot(sender) {
|
||||
this.robot = new Robot({ ...this.config, timeout: 60 })
|
||||
this.robot.on('connect', () => {
|
||||
this.logger.sender(sender, '§a机器人链接成功!')
|
||||
})
|
||||
this.robot.on('message', (event) => {
|
||||
if (event.message_type == "group" && event.group_id == this.config.group_id) {
|
||||
let message: string = event.message
|
||||
message = message.replace(/.*\[CQ:image\,file=(.*),url=(.*),.*]/g, '[图片]')
|
||||
message = this.config.message.group
|
||||
.replace(/%sender_nickname%/g, event.sender.nickname)
|
||||
.replace(/%sender_card%/g, event.sender.card)
|
||||
.replace(/%sender_title%/g, event.sender.title)
|
||||
.replace(/%sender_user_id%/g, event.sender.user_id) + message
|
||||
message = ChatColor.translateAlternateColorCodes('&', message)
|
||||
this.server.getOnlinePlayers().forEach(p => this.logger.sender(p, message))
|
||||
this.logger.console(message)
|
||||
}
|
||||
}
|
||||
client.onclose = (event) => {
|
||||
this.logger.console(`§4连接已断开 §6Code: §3${event.code} §6原因: §c${event.reason}!`)
|
||||
}
|
||||
client.onerror = (event) => {
|
||||
this.logger.console(`§4发生错误: §r${event.error}`)
|
||||
console.ex(event.error)
|
||||
}
|
||||
})
|
||||
this.robot.connect()
|
||||
}
|
||||
|
||||
cmdclose(sender: org.bukkit.entity.Player) {
|
||||
if (this.client && this.client.readyState != WebSocket.CLOSED) {
|
||||
this.client.close(0, 'plugin close socket')
|
||||
if (this.robot) {
|
||||
this.robot.disconnect()
|
||||
this.logger.sender(sender, '§c机器人已断开链接!')
|
||||
}
|
||||
}
|
||||
|
||||
cmdsend(sender: org.bukkit.entity.Player, text: string) {
|
||||
if (this.client) {
|
||||
this.client.send(text)
|
||||
this.logger.sender(sender, '§a发送成功!')
|
||||
}
|
||||
this?.robot.sendGroupMessage(this.config.group_id, text)
|
||||
this.logger.sender(sender, '§a发送成功!')
|
||||
}
|
||||
|
||||
@Tab()
|
||||
tabmbot(_sender: any, _command: string, _args: string[]) {
|
||||
return []
|
||||
}
|
||||
|
||||
@Listener()
|
||||
private PlayerJoinEvent(event: org.bukkit.event.player.PlayerJoinEvent) {
|
||||
if (this.robot && this.config.group_id) {
|
||||
this.robot.sendGroupMessage(this.config.group_id, this.PlaceholderAPI.setPlaceholders(event.getPlayer(), this.config.message.join))
|
||||
}
|
||||
}
|
||||
@Listener()
|
||||
private PlayerQuitEvent(event: org.bukkit.event.player.PlayerQuitEvent) {
|
||||
if (this.robot && this.config.group_id) {
|
||||
this.robot.sendGroupMessage(this.config.group_id, this.PlaceholderAPI.setPlaceholders(event.getPlayer(), this.config.message.quit))
|
||||
}
|
||||
}
|
||||
@Listener()
|
||||
private AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) {
|
||||
if (this.robot && this.config.group_id) {
|
||||
this.robot.sendGroupMessage(this.config.group_id, this.PlaceholderAPI.setPlaceholders(event.getPlayer(), this.config.message.chat) + event.getMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ class SpongeFakeSender extends FakeSender {
|
||||
}
|
||||
}
|
||||
|
||||
@JSPlugin({ prefix: 'PM', version: '1.5.0', author: 'MiaoWoo', source: __filename })
|
||||
@JSPlugin({ prefix: 'PM', version: '1.5.1', author: 'MiaoWoo', source: __filename })
|
||||
export class MiaoScriptPackageManager extends interfaces.Plugin {
|
||||
@Autowired()
|
||||
private pluginManager: pluginApi.PluginManager
|
||||
@@ -233,7 +233,7 @@ export class MiaoScriptPackageManager extends interfaces.Plugin {
|
||||
this.logger.sender(sender, `§6[§3BPM§6][§a${this.serverName}§6] §6命令 §b/mspm ${args.join?.(' ')} §a发布成功!`)
|
||||
}
|
||||
|
||||
@Cmd({ servers: [constants.ServerType.Bungee] })
|
||||
@Cmd({ alias: ["bmspm"], servers: [constants.ServerType.Bungee] })
|
||||
bungeemspm(sender: any, command: string, args: string[]) {
|
||||
if (!sender.hasPermission('mspm.admin')) { return this.i18n(sender, 'main.command.no.permission') }
|
||||
this.taskManager.create(() => this.main(sender, command, args)).async().submit()
|
||||
@@ -437,12 +437,23 @@ export class MiaoScriptPackageManager extends interfaces.Plugin {
|
||||
this.i18n(sender, 'prun.script', { name })
|
||||
this.i18n(sender, 'run.script', { script })
|
||||
let result = this.runCode(script, sender, this.pluginManager.getPlugins().get(name))
|
||||
this.i18n(sender, 'run.result', { result: result == undefined ? this.translate.translate('run.noresult') : typeof result == "string" ? result : JSON.stringify(result) })
|
||||
this.i18n(sender, 'run.result', { result: result == undefined ? this.translate.translate('run.noresult') : typeof result == "string" ? result : this.stringify(result) })
|
||||
} catch (ex) {
|
||||
this.logger.sender(sender, this.logger.stack(ex))
|
||||
}
|
||||
}
|
||||
|
||||
private stringify(object) {
|
||||
let seen = []
|
||||
return JSON.stringify(object, function (key, val) {
|
||||
if (typeof val == "object") {
|
||||
if (seen.indexOf(val) >= 0) return
|
||||
seen.push(val)
|
||||
}
|
||||
return val
|
||||
})
|
||||
}
|
||||
|
||||
private runCode(code: string, sender: any, _this: any) {
|
||||
let paramNames = [
|
||||
'sender',
|
||||
@@ -468,7 +479,7 @@ return eval(${JSON.stringify(code)});`)
|
||||
return tfunc.apply(_this, params)
|
||||
}
|
||||
|
||||
cmddeploy(sender: any, name: any) {
|
||||
cmddeploy(sender: string, name: string, changelog: string = '') {
|
||||
if (!process.env.AccessToken) { return this.i18n(sender, 'deploy.token.not.exists') }
|
||||
this.taskManager.create(() => {
|
||||
if (this.checkPlugin(sender, name)) {
|
||||
@@ -477,7 +488,8 @@ return eval(${JSON.stringify(code)});`)
|
||||
name,
|
||||
author: plugin.description.author,
|
||||
version: plugin.description.version,
|
||||
source: base.read((plugin.description.source || plugin.description.loadMetadata.file).toString())
|
||||
source: base.read((plugin.description.source || plugin.description.loadMetadata.file).toString()),
|
||||
changelog: changelog.replace('&', '§')
|
||||
})
|
||||
this.i18n(sender, result.code == 200 ? 'deploy.success' : 'deploy.fail', { name, version: plugin.description.version, msg: result.msg })
|
||||
}
|
||||
@@ -532,9 +544,9 @@ return eval(${JSON.stringify(code)});`)
|
||||
for (const pl of result.data) { this.packageCache[pl.name] = pl }
|
||||
this.packageNameCache = Object.keys(this.packageCache)
|
||||
this.i18n(sender, 'cloud.update.finish', { length: this.packageNameCache.length })
|
||||
let updateCount = 0
|
||||
this.pluginManager.getPlugins().forEach(p => {
|
||||
let cloudPlugin = this.packageCache[p.description.name]
|
||||
let updateCount = 0
|
||||
//§6插件名称: §b{name}\n§6版本: §a{version}\n§6作者: §3{author}\§6更新时间: §9{updated_at}
|
||||
if (cloudPlugin && cloudPlugin.version != p.description.version) {
|
||||
this.i18n(sender, 'cloud.update.exists', {
|
||||
@@ -545,10 +557,10 @@ return eval(${JSON.stringify(code)});`)
|
||||
})
|
||||
updateCount++
|
||||
}
|
||||
if (updateCount) {
|
||||
this.i18n(sender, 'cloud.update.tip', { count: updateCount })
|
||||
}
|
||||
})
|
||||
if (updateCount) {
|
||||
this.i18n(sender, 'cloud.update.tip', { count: updateCount })
|
||||
}
|
||||
}).async().submit()
|
||||
}
|
||||
|
||||
@@ -558,7 +570,7 @@ return eval(${JSON.stringify(code)});`)
|
||||
this.i18n(sender, 'download.start', { name, version: pluginPkg.version })
|
||||
this.i18n(sender, 'download.url', { url: pluginPkg.url })
|
||||
let pluginFile = update ? fs.concat(root, this.pluginFolder, 'update', name + '.js') : fs.concat(root, this.pluginFolder, name + '.js')
|
||||
http.download(pluginPkg.url, pluginFile)
|
||||
http.download(pluginPkg.url + '?t=' + Date.now(), pluginFile)
|
||||
this.i18n(sender, 'download.finish', { name, version: pluginPkg.version })
|
||||
callback?.()
|
||||
}).async().submit()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/polyfill",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript polyfill package",
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/circlecloud/ms.git",
|
||||
@@ -14,14 +14,14 @@
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/i18n": "^0.15.0",
|
||||
"@ccms/nodejs": "^0.15.0",
|
||||
"core-js": "^3.15.0"
|
||||
"@ccms/i18n": "^0.17.0",
|
||||
"@ccms/nodejs": "^0.17.0",
|
||||
"core-js": "^3.16.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,17 +39,29 @@ class Process extends EventEmitter {
|
||||
return super.on(event, (...args) => {
|
||||
try {
|
||||
listener(...args)
|
||||
} catch (error) {
|
||||
} catch (origin) {
|
||||
try {
|
||||
super.emit('error', error)
|
||||
super.emit('error', origin)
|
||||
} catch (error) {
|
||||
console.ex(origin)
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
nextTick(func: Function) {
|
||||
microTaskPool.execute(func)
|
||||
nextTick(func: Function, ...args: any[]) {
|
||||
microTaskPool.execute(() => {
|
||||
try {
|
||||
func(args)
|
||||
} catch (origin) {
|
||||
try {
|
||||
super.emit('error', origin)
|
||||
} catch (error) {
|
||||
console.ex(origin)
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
exit(code: number) {
|
||||
console.log(`process exit by code ${code}!`)
|
||||
@@ -198,7 +210,7 @@ Object.defineProperty(process, require('core-js/es/symbol/to-string-tag'), { val
|
||||
const eventLoop = new EventLoop()
|
||||
Object.defineProperty(process, 'eventLoop', { value: eventLoop })
|
||||
eventLoop.startEventLoop()
|
||||
global.setGlobal('queueMicrotask', (func: any) => microTaskPool.execute(func), {})
|
||||
global.setGlobal('queueMicrotask', (func: any, ...args: any[]) => process.nextTick(func, args), {})
|
||||
global.setGlobal('setTimeout', eventLoop.setTimeout.bind(eventLoop), {})
|
||||
global.setGlobal('clearTimeout', eventLoop.clearTimeout.bind(eventLoop), {})
|
||||
global.setGlobal('setInterval', eventLoop.setInterval.bind(eventLoop), {})
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import '@ccms/nashorn'
|
||||
|
||||
const URL = Java.type("java.net.URL")
|
||||
const Files = Java.type("java.nio.file.Files")
|
||||
const StandardCopyOption = Java.type("java.nio.file.StandardCopyOption")
|
||||
const JavaString = Java.type("java.lang.String")
|
||||
const SecureRandom = Java.type("java.security.SecureRandom")
|
||||
const SSLContext = Java.type("javax.net.ssl.SSLContext")
|
||||
@@ -13,8 +11,12 @@ const X509TrustManager = Java.type("javax.net.ssl.X509TrustManager")
|
||||
const SocketTimeoutException = Java.type('java.net.SocketTimeoutException')
|
||||
|
||||
const Callable = Java.type('java.util.concurrent.Callable')
|
||||
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')
|
||||
const Executors = Java.type('java.util.concurrent.Executors')
|
||||
|
||||
const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream")
|
||||
const ByteArray = Java.type("byte[]")
|
||||
|
||||
const UTF_8 = "UTF-8"
|
||||
|
||||
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
|
||||
@@ -72,7 +74,7 @@ type HttpHeader = { [key: string]: string }
|
||||
const executor = Executors.newCachedThreadPool()
|
||||
|
||||
export class XMLHttpRequest {
|
||||
private _timeout: number
|
||||
private _timeout: number = 120000;
|
||||
private _responseType: ResponseType = 'text';
|
||||
private _withCredentials: boolean
|
||||
|
||||
@@ -170,6 +172,7 @@ export class XMLHttpRequest {
|
||||
this._connection.setConnectTimeout(this._timeout)
|
||||
this._connection.setReadTimeout(this._timeout)
|
||||
|
||||
this.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
|
||||
this.setReadyState(ReadyState.OPENED)
|
||||
}
|
||||
send(body?: string | object): Future<string> {
|
||||
@@ -178,7 +181,7 @@ export class XMLHttpRequest {
|
||||
}
|
||||
if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) }
|
||||
let future = executor.submit(new Callable({ call: () => this._send(body) }))
|
||||
if (!this._async) { future.get() }
|
||||
if (!this._async) { future.get(this._timeout, TimeUnit.MILLISECONDS) }
|
||||
return future
|
||||
}
|
||||
get() {
|
||||
@@ -248,8 +251,16 @@ export class XMLHttpRequest {
|
||||
}
|
||||
|
||||
private readOutput(input: any) {
|
||||
var tempFile = Files.createTempFile('xhr', '.response')
|
||||
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit()
|
||||
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8')
|
||||
let output = new ByteArrayOutputStream()
|
||||
let buffer = new ByteArray(1024)
|
||||
try {
|
||||
let n: number
|
||||
while ((n = input.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, n)
|
||||
}
|
||||
return output.toString(UTF_8)
|
||||
} finally {
|
||||
output.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/protocol",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript protocol package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,6 +22,6 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/sponge",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript api package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -22,11 +22,11 @@
|
||||
"@javatypes/sponge-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,35 @@
|
||||
import { server } from '@ccms/api'
|
||||
|
||||
const Sponge = org.spongepowered.api.Sponge
|
||||
const Sponge: typeof org.spongepowered.api.Sponge = Java.type('org.spongepowered.api.Sponge')
|
||||
|
||||
export class SpongeNativePluginManager extends server.NativePluginManager {
|
||||
private spongePluginManager: org.spongepowered.api.plugin.PluginManager
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.spongePluginManager = Sponge.getPluginManager()
|
||||
}
|
||||
|
||||
list(): server.NativePlugin[] {
|
||||
return Java.from(this.spongePluginManager.getPlugins()).map(plugin => this.convert(plugin))
|
||||
}
|
||||
has(name: string) {
|
||||
return !!this.get(name)
|
||||
return !!this.spongePluginManager.getPlugin(name).orElse(null)
|
||||
}
|
||||
get(name: string) {
|
||||
return Sponge.getPluginManager().getPlugin(name).orElse(null)
|
||||
return this.convert(this.spongePluginManager.getPlugin(name).orElse(null))
|
||||
}
|
||||
|
||||
private convert(plugin: org.spongepowered.api.plugin.PluginContainer): server.NativePlugin {
|
||||
if (!plugin) return plugin as any
|
||||
return {
|
||||
name: plugin.getName(),
|
||||
version: plugin.getVersion().get() as string,
|
||||
authors: Java.from(plugin.getAuthors() as string[]),
|
||||
depends: Java.from(plugin.getDependencies()),
|
||||
softDepends: [],
|
||||
enable: true,
|
||||
origin: plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ import { provideSingleton } from '@ccms/container'
|
||||
|
||||
import * as reflect from '@ccms/common/dist/reflect'
|
||||
|
||||
const Sponge = org.spongepowered.api.Sponge
|
||||
const Sponge: typeof org.spongepowered.api.Sponge = org.spongepowered.api.Sponge
|
||||
const Text: typeof org.spongepowered.api.text.Text = org.spongepowered.api.text.Text
|
||||
const File = Java.type("java.io.File")
|
||||
|
||||
@provideSingleton(server.Server)
|
||||
@@ -30,6 +31,12 @@ export class SpongeServer extends server.ReflectServer {
|
||||
getService(service: string) {
|
||||
return Sponge.getServiceManager().provide(base.getClass(service)).orElse(null)
|
||||
}
|
||||
broadcast(message: string, permission: string) {
|
||||
return Sponge.getServer().getBroadcastChannel().permission(permission).send(Text.of(message) as any)
|
||||
}
|
||||
broadcastMessage(message: string) {
|
||||
return Sponge.getServer().getBroadcastChannel().TO_ALL.send(Text.of(message) as any)
|
||||
}
|
||||
dispatchCommand(sender: string | any, command: string): boolean {
|
||||
if (typeof sender === 'string') {
|
||||
sender = this.getPlayer(sender)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/spring",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript spring package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -21,12 +21,12 @@
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/common": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0",
|
||||
"@ccms/database": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/common": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0",
|
||||
"@ccms/database": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/web",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript web package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -26,10 +26,10 @@
|
||||
"@javatypes/tomcat": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.15.0",
|
||||
"@ccms/container": "^0.15.0"
|
||||
"@ccms/api": "^0.17.0",
|
||||
"@ccms/container": "^0.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ccms/websocket",
|
||||
"version": "0.15.0",
|
||||
"version": "0.17.0",
|
||||
"description": "MiaoScript websocket package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
@@ -18,11 +18,15 @@
|
||||
"build": "yarn clean && tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"backo2": "^1.0.2",
|
||||
"parseuri": "^0.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ccms/nashorn": "^0.15.0",
|
||||
"@ccms/nashorn": "^0.17.0",
|
||||
"@javatypes/tomcat-websocket-api": "^0.0.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.3.4"
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export class WebSocketManager {
|
||||
}
|
||||
}
|
||||
|
||||
export const managers = new WebSocketManager()
|
||||
export const manager = new WebSocketManager()
|
||||
|
||||
export class WebSocket extends EventEmitter {
|
||||
public static CONNECTING = 0
|
||||
@@ -31,6 +31,7 @@ export class WebSocket extends EventEmitter {
|
||||
public static CLOSING = 2
|
||||
public static CLOSED = 3
|
||||
public binaryType: 'blob' | 'arraybuffer'
|
||||
protected manager: WebSocketManager
|
||||
|
||||
protected _url: string
|
||||
protected _headers: WebSocketHeader = {}
|
||||
@@ -39,11 +40,13 @@ export class WebSocket extends EventEmitter {
|
||||
|
||||
constructor(url: string, subProtocol: string = '', headers: WebSocketHeader = {}) {
|
||||
super()
|
||||
this.manager = manager
|
||||
this._url = url
|
||||
this._headers = headers
|
||||
try {
|
||||
let TransportImpl = require('./netty').NettyWebSocket
|
||||
this.client = new TransportImpl(url, subProtocol, headers)
|
||||
console.debug('create websocket from ' + this.client.constructor.name)
|
||||
} catch (error) {
|
||||
console.error('create websocket impl error: ' + error)
|
||||
console.ex(error)
|
||||
@@ -51,12 +54,12 @@ export class WebSocket extends EventEmitter {
|
||||
}
|
||||
this.client.on('open', (event) => {
|
||||
this.onopen?.(event)
|
||||
managers.add(this)
|
||||
manager.add(this)
|
||||
})
|
||||
this.client.on('message', (event) => this.onmessage?.(event))
|
||||
this.client.on('close', (event) => {
|
||||
this.onclose?.(event)
|
||||
managers.del(this)
|
||||
manager.del(this)
|
||||
})
|
||||
this.client.on('error', (event) => this.onerror?.(event))
|
||||
setTimeout(() => this.client.connect(), 20)
|
||||
@@ -96,3 +99,4 @@ export class WebSocket extends EventEmitter {
|
||||
this.removeAllListeners()
|
||||
}
|
||||
}
|
||||
global.setGlobal('WebSocket', WebSocket)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { NettyWebSocket } from '.'
|
||||
import { WebSocketClientHandlerAdapter } from './adapter/handler'
|
||||
|
||||
@@ -6,6 +5,7 @@ const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
|
||||
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
||||
const CloseWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.CloseWebSocketFrame')
|
||||
const FullHttpResponse = Java.type('io.netty.handler.codec.http.FullHttpResponse')
|
||||
const DefaultChannelPromise = Java.type('io.netty.channel.DefaultChannelPromise')
|
||||
|
||||
export class WebSocketClientHandler extends WebSocketClientHandlerAdapter {
|
||||
public handshaker: any
|
||||
@@ -20,16 +20,20 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter {
|
||||
return true
|
||||
}
|
||||
handlerAdded(ctx: any) {
|
||||
console.trace(`${ctx} handlerAdded`)
|
||||
this.handshakeFuture = ctx.newPromise()
|
||||
console.debug(`${ctx} handlerAdded`)
|
||||
if (ctx.newPromise) {
|
||||
this.handshakeFuture = ctx.newPromise()
|
||||
} else {
|
||||
this.handshakeFuture = new DefaultChannelPromise(ctx.channel(), ctx.executor())
|
||||
}
|
||||
}
|
||||
channelActive(ctx: any) {
|
||||
console.trace(`${ctx} channelActive`)
|
||||
console.debug(`${ctx} channelActive`)
|
||||
this.handshaker.handshake(ctx.channel())
|
||||
}
|
||||
channelInactive(ctx: any) {
|
||||
console.trace(`${ctx} channelInactive`)
|
||||
this.client.onclose({ code: 0, reason: 'server connection channel inactive!' })
|
||||
console.debug(`${ctx} channelInactive`)
|
||||
this.client.onclose({ code: 0, reason: 'client connection channel inactive!' })
|
||||
}
|
||||
channelRead0(ctx: any, msg: any) {
|
||||
console.trace(`${ctx} channelRead0 ${msg}`)
|
||||
@@ -50,11 +54,11 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter {
|
||||
if (frame instanceof TextWebSocketFrame) {
|
||||
this.client.onmessage({ data: frame.text() })
|
||||
} else if (frame instanceof CloseWebSocketFrame) {
|
||||
this.client.onclose({ code: 0, reason: 'server send CloseWebSocketFrame!' })
|
||||
this.client.onclose({ code: 0, reason: 'server close connection!' })
|
||||
}
|
||||
}
|
||||
exceptionCaught(ctx: any, cause: Error) {
|
||||
console.trace(`${ctx} exceptionCaught ${cause}`)
|
||||
console.debug(`${ctx} exceptionCaught ${cause}`)
|
||||
this.client.onerror({ error: cause })
|
||||
if (!this.handshakeFuture.isDone()) {
|
||||
this.handshakeFuture.setFailure(cause)
|
||||
|
||||
@@ -4,16 +4,12 @@ import { Transport } from '../transport'
|
||||
import { WebSocketClientHandler } from './handler'
|
||||
|
||||
const URI = Java.type('java.net.URI')
|
||||
const Epoll = Java.type('io.netty.channel.epoll.Epoll')
|
||||
const Bootstrap = Java.type('io.netty.bootstrap.Bootstrap')
|
||||
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener')
|
||||
|
||||
const NioEventLoopGroup = Java.type('io.netty.channel.nio.NioEventLoopGroup')
|
||||
const NioSocketChannel = Java.type('io.netty.channel.socket.nio.NioSocketChannel')
|
||||
|
||||
const EpollEventLoopGroup = Java.type('io.netty.channel.epoll.EpollEventLoopGroup')
|
||||
const EpollSocketChannel = Java.type('io.netty.channel.epoll.EpollSocketChannel')
|
||||
|
||||
const WebSocketClientHandshakerFactory = Java.type('io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory')
|
||||
const WebSocketVersion = Java.type('io.netty.handler.codec.http.websocketx.WebSocketVersion')
|
||||
|
||||
@@ -25,22 +21,77 @@ const CloseWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.Cl
|
||||
const ChannelInitializer = Java.type('io.netty.channel.ChannelInitializer')
|
||||
const DefaultHttpHeaders = Java.type('io.netty.handler.codec.http.DefaultHttpHeaders')
|
||||
|
||||
const epull = Epoll.isAvailable()
|
||||
const group = epull ? new EpollEventLoopGroup() : new NioEventLoopGroup()
|
||||
const socketChannelClass = epull ? EpollSocketChannel.class : NioSocketChannel.class
|
||||
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
|
||||
const channelCount = new AtomicInteger(0)
|
||||
|
||||
var SslContextBuilder: any
|
||||
var InsecureTrustManagerFactory: any
|
||||
var SSLContext: any
|
||||
var SslHandler: any
|
||||
try {
|
||||
SslContextBuilder = Java.type('io.netty.handler.ssl.SslContextBuilder')
|
||||
InsecureTrustManagerFactory = Java.type('io.netty.handler.ssl.util.InsecureTrustManagerFactory')
|
||||
} catch (error) {
|
||||
SSLContext = Java.type('javax.net.ssl.SSLContext')
|
||||
SslHandler = Java.type('io.netty.handler.ssl.SslHandler')
|
||||
}
|
||||
|
||||
var group: any
|
||||
var socketChannelClass: any
|
||||
try {
|
||||
const Epoll = Java.type('io.netty.channel.epoll.Epoll')
|
||||
const epull = Epoll.isAvailable()
|
||||
const EpollEventLoopGroup = Java.type('io.netty.channel.epoll.EpollEventLoopGroup')
|
||||
const EpollSocketChannel = Java.type('io.netty.channel.epoll.EpollSocketChannel')
|
||||
group = epull ? new EpollEventLoopGroup() : new NioEventLoopGroup()
|
||||
socketChannelClass = epull ? EpollSocketChannel.class : NioSocketChannel.class
|
||||
} catch (error) {
|
||||
group = new NioEventLoopGroup()
|
||||
socketChannelClass = NioSocketChannel.class
|
||||
}
|
||||
|
||||
process.on('exit', () => group.shutdownGracefully())
|
||||
|
||||
export class NettyWebSocket extends Transport {
|
||||
private _uri: any
|
||||
private _schema: string
|
||||
private _host: string
|
||||
private _port: number
|
||||
private channel: any
|
||||
private b = new Bootstrap();
|
||||
|
||||
constructor(url: string, subProtocol: string = '', headers: WebSocketHeader = {}) {
|
||||
super(url, subProtocol, headers)
|
||||
if (!url) {
|
||||
throw new Error("Failed to construct 'WebSocket': The URL '" + url + "' is invalid.")
|
||||
}
|
||||
this._uri = URI.create(this._url)
|
||||
this._schema = this._uri.getScheme() ?? 'ws'
|
||||
if (["wss", "ws"].indexOf(this._schema) == -1) {
|
||||
throw new Error("Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. '" + this._schema + "' is not allowed.")
|
||||
}
|
||||
this._host = this._uri.getHost()
|
||||
if (!this._host) {
|
||||
throw new Error("Failed to construct 'WebSocket': The Host '" + this._host + "' is invalid.")
|
||||
}
|
||||
this._port = this._uri.getPort()
|
||||
if (this._port == -1) {
|
||||
if (this._schema == "wss") {
|
||||
this._port = 443
|
||||
} else if (this._schema == "ws") {
|
||||
this._port = 80
|
||||
}
|
||||
}
|
||||
console.debug(`constructor NettyWebSocket url: ${url} scheme: ${this._schema} host: ${this._host} port: ${this._port} header: ${JSON.stringify(headers)}`)
|
||||
}
|
||||
getId() {
|
||||
return this.channel?.id() + ''
|
||||
if (this.channel?.id) {
|
||||
return this.channel?.id() + ''
|
||||
}
|
||||
return 'NettyWebSocket#' + channelCount.incrementAndGet()
|
||||
}
|
||||
doConnect() {
|
||||
console.debug('client NettyWebSocket doConnect', this._url)
|
||||
let uri = URI.create(this._url)
|
||||
let headers = new DefaultHttpHeaders()
|
||||
for (const key of Object.getOwnPropertyNames(this._headers || {})) {
|
||||
@@ -56,20 +107,32 @@ export class NettyWebSocket extends Transport {
|
||||
.handler(new ChannelInitializer({
|
||||
initChannel: (ch: any) => {
|
||||
let pipeline = ch.pipeline()
|
||||
if (this._schema == "wss") {
|
||||
if (SslContextBuilder) {
|
||||
let sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build()
|
||||
pipeline.addLast(sslCtx.newHandler(ch.alloc(), this._host, this._port))
|
||||
} else {
|
||||
let sslEngine = SSLContext.getDefault().createSSLEngine()
|
||||
sslEngine.setUseClientMode(true)
|
||||
pipeline.addLast("ssl", new SslHandler(sslEngine))
|
||||
}
|
||||
}
|
||||
pipeline.addLast("http-codec", new HttpClientCodec())
|
||||
pipeline.addLast("aggregator", new HttpObjectAggregator(65536))
|
||||
pipeline.addLast("websocket", handler.getHandler())
|
||||
}
|
||||
}))
|
||||
this.b.connect(uri.getHost(), uri.getPort()).addListener(new ChannelFutureListener((future: any) => {
|
||||
this.b.connect(this._host, this._port).addListener(new ChannelFutureListener((future: any) => {
|
||||
this.channel = future.sync().channel()
|
||||
this.onconnection({})
|
||||
handler.handshakeFuture.addListener(new ChannelFutureListener((future: any) => {
|
||||
try {
|
||||
future.sync()
|
||||
// only trigger onconnect when not have error
|
||||
this.onconnect({})
|
||||
} catch (error) {
|
||||
console.debug(error)
|
||||
// ignore error exceptionCaught from handler
|
||||
// this.onerror({ error })
|
||||
}
|
||||
}))
|
||||
}))
|
||||
|
||||
@@ -32,9 +32,11 @@ export abstract class Transport extends EventEmitter {
|
||||
}
|
||||
|
||||
connect() {
|
||||
console.debug(`client Transport connect`)
|
||||
try {
|
||||
this.doConnect()
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
this.onerror({ error })
|
||||
}
|
||||
}
|
||||
@@ -55,6 +57,8 @@ export abstract class Transport extends EventEmitter {
|
||||
this.doClose(code, reason)
|
||||
} catch (error) {
|
||||
this.onerror({ error })
|
||||
} finally {
|
||||
this.removeAllListeners()
|
||||
}
|
||||
} else {
|
||||
console.debug(`${this.id} call close but state is ${this.readyStatus}`)
|
||||
@@ -67,7 +71,6 @@ export abstract class Transport extends EventEmitter {
|
||||
}
|
||||
|
||||
onconnect(event: Event) {
|
||||
console.debug(`${this.id} call onconnect`)
|
||||
if (this.readyStatus != WebSocket.OPEN) {
|
||||
this.readyStatus = WebSocket.OPEN
|
||||
this.emit('open', event)
|
||||
|
||||
1
packages/websocket/src/debug.ts
Normal file
1
packages/websocket/src/debug.ts
Normal file
@@ -0,0 +1 @@
|
||||
export = (namepsace) => (...args) => { }//console.debug(namepsace, ...args)
|
||||
16
packages/websocket/src/engine.io-client/index.ts
Normal file
16
packages/websocket/src/engine.io-client/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Socket } from './socket'
|
||||
|
||||
export default (uri, opts) => new Socket(uri, opts)
|
||||
|
||||
/**
|
||||
* Expose deps for legacy compatibility
|
||||
* and standalone browser access.
|
||||
*/
|
||||
const protocol = Socket.protocol // this is an int
|
||||
export { Socket, protocol }
|
||||
// module.exports.Transport = require("./transport")
|
||||
// module.exports.transports = require("./transports/index")
|
||||
// module.exports.parser = require("../engine.io-parser")
|
||||
export * from './transport'
|
||||
export * from './transports/index'
|
||||
export * from '../engine.io-parser'
|
||||
688
packages/websocket/src/engine.io-client/socket.ts
Normal file
688
packages/websocket/src/engine.io-client/socket.ts
Normal file
@@ -0,0 +1,688 @@
|
||||
import transports from "./transports"
|
||||
// const transports = require("./transports/index")
|
||||
const Emitter = require("component-emitter")
|
||||
const debug = (...args: any) => console.debug('engine.io-client:socket', ...args)//require("debug")("engine.io-client:socket")
|
||||
import parser from "../engine.io-parser"
|
||||
const parseuri = require("parseuri")
|
||||
const parseqs = require("parseqs")
|
||||
import { installTimerFunctions } from "./util"
|
||||
|
||||
export class Socket extends Emitter {
|
||||
/**
|
||||
* Socket constructor.
|
||||
*
|
||||
* @param {String|Object} uri or options
|
||||
* @param {Object} options
|
||||
* @api public
|
||||
*/
|
||||
constructor(uri, opts: any = {}) {
|
||||
super()
|
||||
|
||||
if (uri && "object" === typeof uri) {
|
||||
opts = uri
|
||||
uri = null
|
||||
}
|
||||
|
||||
if (uri) {
|
||||
uri = parseuri(uri)
|
||||
opts.hostname = uri.host
|
||||
opts.secure = uri.protocol === "https" || uri.protocol === "wss"
|
||||
opts.port = uri.port
|
||||
if (uri.query) opts.query = uri.query
|
||||
} else if (opts.host) {
|
||||
opts.hostname = parseuri(opts.host).host
|
||||
}
|
||||
|
||||
installTimerFunctions(this, opts)
|
||||
|
||||
this.secure =
|
||||
null != opts.secure
|
||||
? opts.secure
|
||||
: typeof location !== "undefined" && "https:" === location.protocol
|
||||
|
||||
if (opts.hostname && !opts.port) {
|
||||
// if no port is specified manually, use the protocol default
|
||||
opts.port = this.secure ? "443" : "80"
|
||||
}
|
||||
|
||||
this.hostname =
|
||||
opts.hostname ||
|
||||
(typeof location !== "undefined" ? location.hostname : "localhost")
|
||||
this.port =
|
||||
opts.port ||
|
||||
(typeof location !== "undefined" && location.port
|
||||
? location.port
|
||||
: this.secure
|
||||
? 443
|
||||
: 80)
|
||||
|
||||
this.transports = ["websocket"]
|
||||
this.readyState = ""
|
||||
this.writeBuffer = []
|
||||
this.prevBufferLen = 0
|
||||
|
||||
this.opts = Object.assign(
|
||||
{
|
||||
path: "/engine.io",
|
||||
agent: false,
|
||||
withCredentials: false,
|
||||
upgrade: true,
|
||||
jsonp: true,
|
||||
timestampParam: "t",
|
||||
rememberUpgrade: false,
|
||||
rejectUnauthorized: true,
|
||||
perMessageDeflate: {
|
||||
threshold: 1024
|
||||
},
|
||||
transportOptions: {},
|
||||
closeOnBeforeunload: true
|
||||
},
|
||||
opts
|
||||
)
|
||||
|
||||
this.opts.path = this.opts.path.replace(/\/$/, "") + "/"
|
||||
|
||||
if (typeof this.opts.query === "string") {
|
||||
this.opts.query = parseqs.decode(this.opts.query)
|
||||
}
|
||||
|
||||
// set on handshake
|
||||
this.id = null
|
||||
this.upgrades = null
|
||||
this.pingInterval = null
|
||||
this.pingTimeout = null
|
||||
|
||||
// set on heartbeat
|
||||
this.pingTimeoutTimer = null
|
||||
|
||||
if (typeof addEventListener === "function") {
|
||||
if (this.opts.closeOnBeforeunload) {
|
||||
// Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
|
||||
// ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
|
||||
// closed/reloaded)
|
||||
addEventListener(
|
||||
"beforeunload",
|
||||
() => {
|
||||
if (this.transport) {
|
||||
// silently close the transport
|
||||
this.transport.removeAllListeners()
|
||||
this.transport.close()
|
||||
}
|
||||
},
|
||||
false
|
||||
)
|
||||
}
|
||||
if (this.hostname !== "localhost") {
|
||||
this.offlineEventListener = () => {
|
||||
this.onClose("transport close")
|
||||
}
|
||||
addEventListener("offline", this.offlineEventListener, false)
|
||||
}
|
||||
}
|
||||
|
||||
this.open()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates transport of the given type.
|
||||
*
|
||||
* @param {String} transport name
|
||||
* @return {Transport}
|
||||
* @api private
|
||||
*/
|
||||
createTransport(name, opt?) {
|
||||
if (name != 'websocket') {
|
||||
throw new Error('Only Support WebSocket in MiaoScript!')
|
||||
}
|
||||
debug('creating transport "%s"', name)
|
||||
const query: any = clone(this.opts.query)
|
||||
|
||||
// append engine.io protocol identifier
|
||||
query.EIO = parser.protocol
|
||||
|
||||
// transport name
|
||||
query.transport = name
|
||||
|
||||
// session id if we already have one
|
||||
if (this.id) query.sid = this.id
|
||||
|
||||
const opts = Object.assign(
|
||||
{},
|
||||
this.opts.transportOptions[name],
|
||||
this.opts,
|
||||
{
|
||||
query,
|
||||
socket: this,
|
||||
hostname: this.hostname,
|
||||
secure: this.secure,
|
||||
port: this.port
|
||||
}
|
||||
)
|
||||
|
||||
debug("options: %j", JSON.stringify(opts))
|
||||
debug("new func", transports[name])
|
||||
return new transports[name](opts)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes transport to use and starts probe.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
open() {
|
||||
let transport
|
||||
if (
|
||||
this.opts.rememberUpgrade &&
|
||||
Socket.priorWebsocketSuccess &&
|
||||
this.transports.indexOf("websocket") !== -1
|
||||
) {
|
||||
transport = "websocket"
|
||||
} else if (0 === this.transports.length) {
|
||||
// Emit error on next tick so it can be listened to
|
||||
this.setTimeoutFn(() => {
|
||||
this.emit("error", "No transports available")
|
||||
}, 0)
|
||||
return
|
||||
} else {
|
||||
transport = this.transports[0]
|
||||
}
|
||||
this.readyState = "opening"
|
||||
|
||||
// Retry with the next transport if the transport is disabled (jsonp: false)
|
||||
try {
|
||||
transport = this.createTransport(transport)
|
||||
} catch (e) {
|
||||
debug("error while creating transport: %s", e)
|
||||
this.transports.shift()
|
||||
this.open()
|
||||
return
|
||||
}
|
||||
|
||||
transport.open()
|
||||
this.setTransport(transport)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current transport. Disables the existing one (if any).
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
setTransport(transport) {
|
||||
debug("setting transport %s", transport.name)
|
||||
|
||||
if (this.transport) {
|
||||
debug("clearing existing transport %s", this.transport.name)
|
||||
this.transport.removeAllListeners()
|
||||
}
|
||||
|
||||
// set up transport
|
||||
this.transport = transport
|
||||
|
||||
// set up transport listeners
|
||||
transport
|
||||
.on("drain", this.onDrain.bind(this))
|
||||
.on("packet", this.onPacket.bind(this))
|
||||
.on("error", this.onError.bind(this))
|
||||
.on("close", () => {
|
||||
this.onClose("transport close")
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Probes a transport.
|
||||
*
|
||||
* @param {String} transport name
|
||||
* @api private
|
||||
*/
|
||||
probe(name) {
|
||||
debug('probing transport "%s"', name)
|
||||
let transport = this.createTransport(name, { probe: 1 })
|
||||
let failed = false
|
||||
|
||||
Socket.priorWebsocketSuccess = false
|
||||
|
||||
const onTransportOpen = () => {
|
||||
if (failed) return
|
||||
|
||||
debug('probe transport "%s" opened', name)
|
||||
transport.send([{ type: "ping", data: "probe" }])
|
||||
transport.once("packet", msg => {
|
||||
if (failed) return
|
||||
if ("pong" === msg.type && "probe" === msg.data) {
|
||||
debug('probe transport "%s" pong', name)
|
||||
this.upgrading = true
|
||||
this.emit("upgrading", transport)
|
||||
if (!transport) return
|
||||
Socket.priorWebsocketSuccess = "websocket" === transport.name
|
||||
|
||||
debug('pausing current transport "%s"', this.transport.name)
|
||||
this.transport.pause(() => {
|
||||
if (failed) return
|
||||
if ("closed" === this.readyState) return
|
||||
debug("changing transport and sending upgrade packet")
|
||||
|
||||
cleanup()
|
||||
|
||||
this.setTransport(transport)
|
||||
transport.send([{ type: "upgrade" }])
|
||||
this.emit("upgrade", transport)
|
||||
transport = null
|
||||
this.upgrading = false
|
||||
this.flush()
|
||||
})
|
||||
} else {
|
||||
debug('probe transport "%s" failed', name)
|
||||
const err: any = new Error("probe error")
|
||||
err.transport = transport.name
|
||||
this.emit("upgradeError", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function freezeTransport() {
|
||||
if (failed) return
|
||||
|
||||
// Any callback called by transport should be ignored since now
|
||||
failed = true
|
||||
|
||||
cleanup()
|
||||
|
||||
transport.close()
|
||||
transport = null
|
||||
}
|
||||
|
||||
// Handle any error that happens while probing
|
||||
const onerror = err => {
|
||||
const error: any = new Error("probe error: " + err)
|
||||
error.transport = transport.name
|
||||
|
||||
freezeTransport()
|
||||
|
||||
debug('probe transport "%s" failed because of error: %s', name, err)
|
||||
|
||||
this.emit("upgradeError", error)
|
||||
}
|
||||
|
||||
function onTransportClose() {
|
||||
onerror("transport closed")
|
||||
}
|
||||
|
||||
// When the socket is closed while we're probing
|
||||
function onclose() {
|
||||
onerror("socket closed")
|
||||
}
|
||||
|
||||
// When the socket is upgraded while we're probing
|
||||
function onupgrade(to) {
|
||||
if (transport && to.name !== transport.name) {
|
||||
debug('"%s" works - aborting "%s"', to.name, transport.name)
|
||||
freezeTransport()
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all listeners on the transport and on self
|
||||
const cleanup = () => {
|
||||
transport.removeListener("open", onTransportOpen)
|
||||
transport.removeListener("error", onerror)
|
||||
transport.removeListener("close", onTransportClose)
|
||||
this.removeListener("close", onclose)
|
||||
this.removeListener("upgrading", onupgrade)
|
||||
}
|
||||
|
||||
transport.once("open", onTransportOpen)
|
||||
transport.once("error", onerror)
|
||||
transport.once("close", onTransportClose)
|
||||
|
||||
this.once("close", onclose)
|
||||
this.once("upgrading", onupgrade)
|
||||
|
||||
transport.open()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when connection is deemed open.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
onOpen() {
|
||||
debug("socket open")
|
||||
this.readyState = "open"
|
||||
Socket.priorWebsocketSuccess = "websocket" === this.transport.name
|
||||
this.emit("open")
|
||||
this.flush()
|
||||
|
||||
// we check for `readyState` in case an `open`
|
||||
// listener already closed the socket
|
||||
if (
|
||||
"open" === this.readyState &&
|
||||
this.opts.upgrade &&
|
||||
this.transport.pause
|
||||
) {
|
||||
debug("starting upgrade probes")
|
||||
let i = 0
|
||||
const l = this.upgrades.length
|
||||
for (; i < l; i++) {
|
||||
this.probe(this.upgrades[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a packet.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onPacket(packet) {
|
||||
if (
|
||||
"opening" === this.readyState ||
|
||||
"open" === this.readyState ||
|
||||
"closing" === this.readyState
|
||||
) {
|
||||
debug('socket receive: type "%s", data "%s"', packet.type, packet.data)
|
||||
|
||||
this.emit("packet", packet)
|
||||
|
||||
// Socket is live - any packet counts
|
||||
this.emit("heartbeat")
|
||||
|
||||
switch (packet.type) {
|
||||
case "open":
|
||||
this.onHandshake(JSON.parse(packet.data))
|
||||
break
|
||||
|
||||
case "ping":
|
||||
this.resetPingTimeout()
|
||||
this.sendPacket("pong")
|
||||
this.emit("ping")
|
||||
this.emit("pong")
|
||||
break
|
||||
|
||||
case "error":
|
||||
const err: any = new Error("server error")
|
||||
err.code = packet.data
|
||||
this.onError(err)
|
||||
break
|
||||
|
||||
case "message":
|
||||
this.emit("data", packet.data)
|
||||
this.emit("message", packet.data)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
debug('packet received with socket readyState "%s"', this.readyState)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon handshake completion.
|
||||
*
|
||||
* @param {Object} handshake obj
|
||||
* @api private
|
||||
*/
|
||||
onHandshake(data) {
|
||||
this.emit("handshake", data)
|
||||
this.id = data.sid
|
||||
this.transport.query.sid = data.sid
|
||||
this.upgrades = this.filterUpgrades(data.upgrades)
|
||||
this.pingInterval = data.pingInterval
|
||||
this.pingTimeout = data.pingTimeout
|
||||
this.onOpen()
|
||||
// In case open handler closes socket
|
||||
if ("closed" === this.readyState) return
|
||||
this.resetPingTimeout()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets and resets ping timeout timer based on server pings.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
resetPingTimeout() {
|
||||
this.clearTimeoutFn(this.pingTimeoutTimer)
|
||||
this.pingTimeoutTimer = this.setTimeoutFn(() => {
|
||||
this.onClose("ping timeout")
|
||||
}, this.pingInterval + this.pingTimeout)
|
||||
if (this.opts.autoUnref) {
|
||||
this.pingTimeoutTimer.unref()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on `drain` event
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onDrain() {
|
||||
this.writeBuffer.splice(0, this.prevBufferLen)
|
||||
|
||||
// setting prevBufferLen = 0 is very important
|
||||
// for example, when upgrading, upgrade packet is sent over,
|
||||
// and a nonzero prevBufferLen could cause problems on `drain`
|
||||
this.prevBufferLen = 0
|
||||
|
||||
if (0 === this.writeBuffer.length) {
|
||||
this.emit("drain")
|
||||
} else {
|
||||
this.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush write buffers.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
flush() {
|
||||
if (
|
||||
"closed" !== this.readyState &&
|
||||
this.transport.writable &&
|
||||
!this.upgrading &&
|
||||
this.writeBuffer.length
|
||||
) {
|
||||
debug("flushing %d packets in socket", this.writeBuffer.length)
|
||||
this.transport.send(this.writeBuffer)
|
||||
// keep track of current length of writeBuffer
|
||||
// splice writeBuffer and callbackBuffer on `drain`
|
||||
this.prevBufferLen = this.writeBuffer.length
|
||||
this.emit("flush")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message.
|
||||
*
|
||||
* @param {String} message.
|
||||
* @param {Function} callback function.
|
||||
* @param {Object} options.
|
||||
* @return {Socket} for chaining.
|
||||
* @api public
|
||||
*/
|
||||
write(msg, options, fn) {
|
||||
this.sendPacket("message", msg, options, fn)
|
||||
return this
|
||||
}
|
||||
|
||||
send(msg, options, fn) {
|
||||
this.sendPacket("message", msg, options, fn)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet.
|
||||
*
|
||||
* @param {String} packet type.
|
||||
* @param {String} data.
|
||||
* @param {Object} options.
|
||||
* @param {Function} callback function.
|
||||
* @api private
|
||||
*/
|
||||
sendPacket(type, data?, options?, fn?) {
|
||||
if ("function" === typeof data) {
|
||||
fn = data
|
||||
data = undefined
|
||||
}
|
||||
|
||||
if ("function" === typeof options) {
|
||||
fn = options
|
||||
options = null
|
||||
}
|
||||
|
||||
if ("closing" === this.readyState || "closed" === this.readyState) {
|
||||
return
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
options.compress = false !== options.compress
|
||||
|
||||
const packet = {
|
||||
type: type,
|
||||
data: data,
|
||||
options: options
|
||||
}
|
||||
this.emit("packetCreate", packet)
|
||||
this.writeBuffer.push(packet)
|
||||
if (fn) this.once("flush", fn)
|
||||
this.flush()
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
close() {
|
||||
const close = () => {
|
||||
this.onClose("forced close")
|
||||
debug("socket closing - telling transport to close")
|
||||
this.transport.close()
|
||||
}
|
||||
|
||||
const cleanupAndClose = () => {
|
||||
this.removeListener("upgrade", cleanupAndClose)
|
||||
this.removeListener("upgradeError", cleanupAndClose)
|
||||
close()
|
||||
}
|
||||
|
||||
const waitForUpgrade = () => {
|
||||
// wait for upgrade to finish since we can't send packets while pausing a transport
|
||||
this.once("upgrade", cleanupAndClose)
|
||||
this.once("upgradeError", cleanupAndClose)
|
||||
}
|
||||
|
||||
if ("opening" === this.readyState || "open" === this.readyState) {
|
||||
this.readyState = "closing"
|
||||
|
||||
if (this.writeBuffer.length) {
|
||||
this.once("drain", () => {
|
||||
if (this.upgrading) {
|
||||
waitForUpgrade()
|
||||
} else {
|
||||
close()
|
||||
}
|
||||
})
|
||||
} else if (this.upgrading) {
|
||||
waitForUpgrade()
|
||||
} else {
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport error
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onError(err) {
|
||||
debug("socket error %j", err)
|
||||
Socket.priorWebsocketSuccess = false
|
||||
this.emit("error", err)
|
||||
this.onClose("transport error", err)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport close.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onClose(reason, desc?) {
|
||||
if (
|
||||
"opening" === this.readyState ||
|
||||
"open" === this.readyState ||
|
||||
"closing" === this.readyState
|
||||
) {
|
||||
debug('socket close with reason: "%s"', reason)
|
||||
|
||||
// clear timers
|
||||
this.clearTimeoutFn(this.pingIntervalTimer)
|
||||
this.clearTimeoutFn(this.pingTimeoutTimer)
|
||||
|
||||
// stop event from firing again for transport
|
||||
this.transport.removeAllListeners("close")
|
||||
|
||||
// ensure transport won't stay open
|
||||
this.transport.close()
|
||||
|
||||
// ignore further transport communication
|
||||
this.transport.removeAllListeners()
|
||||
|
||||
if (typeof removeEventListener === "function") {
|
||||
removeEventListener("offline", this.offlineEventListener, false)
|
||||
}
|
||||
|
||||
// set ready state
|
||||
this.readyState = "closed"
|
||||
|
||||
// clear session id
|
||||
this.id = null
|
||||
|
||||
// emit close event
|
||||
this.emit("close", reason, desc)
|
||||
|
||||
// clean buffers after, so users can still
|
||||
// grab the buffers on `close` event
|
||||
this.writeBuffer = []
|
||||
this.prevBufferLen = 0
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters upgrades, returning only those matching client transports.
|
||||
*
|
||||
* @param {Array} server upgrades
|
||||
* @api private
|
||||
*
|
||||
*/
|
||||
filterUpgrades(upgrades) {
|
||||
const filteredUpgrades = []
|
||||
let i = 0
|
||||
const j = upgrades.length
|
||||
for (; i < j; i++) {
|
||||
if (~this.transports.indexOf(upgrades[i]))
|
||||
filteredUpgrades.push(upgrades[i])
|
||||
}
|
||||
return filteredUpgrades
|
||||
}
|
||||
}
|
||||
|
||||
Socket.priorWebsocketSuccess = false
|
||||
|
||||
/**
|
||||
* Protocol version.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Socket.protocol = parser.protocol // this is an int
|
||||
|
||||
function clone(obj) {
|
||||
const o = {}
|
||||
for (let i in obj) {
|
||||
if (obj.hasOwnProperty(i)) {
|
||||
o[i] = obj[i]
|
||||
}
|
||||
}
|
||||
return o
|
||||
}
|
||||
119
packages/websocket/src/engine.io-client/transport.ts
Normal file
119
packages/websocket/src/engine.io-client/transport.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import parser from "../engine.io-parser"
|
||||
const Emitter = require("component-emitter")
|
||||
import { installTimerFunctions } from "./util"
|
||||
const debug = (...args: any) => console.debug('engine.io-client:transport', ...args)//require("debug")("engine.io-client:transport")
|
||||
|
||||
export class Transport extends Emitter {
|
||||
/**
|
||||
* Transport abstract constructor.
|
||||
*
|
||||
* @param {Object} options.
|
||||
* @api private
|
||||
*/
|
||||
constructor(opts) {
|
||||
super()
|
||||
installTimerFunctions(this, opts)
|
||||
|
||||
this.opts = opts
|
||||
this.query = opts.query
|
||||
this.readyState = ""
|
||||
this.socket = opts.socket
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an error.
|
||||
*
|
||||
* @param {String} str
|
||||
* @return {Transport} for chaining
|
||||
* @api public
|
||||
*/
|
||||
onError(msg, desc) {
|
||||
const err: any = new Error(msg)
|
||||
err.type = "TransportError"
|
||||
err.description = desc
|
||||
this.emit("error", err)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the transport.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
open() {
|
||||
if ("closed" === this.readyState || "" === this.readyState) {
|
||||
this.readyState = "opening"
|
||||
this.doOpen()
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the transport.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
close() {
|
||||
if ("opening" === this.readyState || "open" === this.readyState) {
|
||||
this.doClose()
|
||||
this.onClose()
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends multiple packets.
|
||||
*
|
||||
* @param {Array} packets
|
||||
* @api private
|
||||
*/
|
||||
send(packets) {
|
||||
if ("open" === this.readyState) {
|
||||
this.write(packets)
|
||||
} else {
|
||||
// this might happen if the transport was silently closed in the beforeunload event handler
|
||||
debug("transport is not open, discarding packets")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon open
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onOpen() {
|
||||
this.readyState = "open"
|
||||
this.writable = true
|
||||
this.emit("open")
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with data.
|
||||
*
|
||||
* @param {String} data
|
||||
* @api private
|
||||
*/
|
||||
onData(data) {
|
||||
const packet = parser.decodePacket(data, this.socket.binaryType)
|
||||
this.onPacket(packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with a decoded packet.
|
||||
*/
|
||||
onPacket(packet) {
|
||||
this.emit("packet", packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon close.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onClose() {
|
||||
this.readyState = "closed"
|
||||
this.emit("close")
|
||||
}
|
||||
}
|
||||
4
packages/websocket/src/engine.io-client/transports/index.ts
Executable file
4
packages/websocket/src/engine.io-client/transports/index.ts
Executable file
@@ -0,0 +1,4 @@
|
||||
import { WS } from "./websocket"
|
||||
export default {
|
||||
'websocket': WS
|
||||
}
|
||||
259
packages/websocket/src/engine.io-client/transports/websocket.ts
Normal file
259
packages/websocket/src/engine.io-client/transports/websocket.ts
Normal file
@@ -0,0 +1,259 @@
|
||||
import { Transport } from '../transport'
|
||||
// const Transport = require("../transport")
|
||||
import parser from '../../engine.io-parser'
|
||||
// const parser = require("../engine.io-parser")
|
||||
const parseqs = require("parseqs")
|
||||
const yeast = require("yeast")
|
||||
import { pick } from '../util'
|
||||
// const { pick } = require("../util")
|
||||
import { WebSocket } from '../../client'
|
||||
const usingBrowserWebSocket = true
|
||||
// const {
|
||||
// WebSocket,
|
||||
// usingBrowserWebSocket,
|
||||
// defaultBinaryType,
|
||||
// nextTick
|
||||
// } = require("./websocket-constructor")
|
||||
|
||||
const debug = (...args: any) => console.debug('engine.io-client:websocket', ...args)//require("debug")("engine.io-client:websocket")
|
||||
|
||||
// detect ReactNative environment
|
||||
const isReactNative =
|
||||
typeof navigator !== "undefined" &&
|
||||
typeof navigator.product === "string" &&
|
||||
navigator.product.toLowerCase() === "reactnative"
|
||||
|
||||
export class WS extends Transport {
|
||||
/**
|
||||
* WebSocket transport constructor.
|
||||
*
|
||||
* @api {Object} connection options
|
||||
* @api public
|
||||
*/
|
||||
constructor(opts) {
|
||||
super(opts)
|
||||
|
||||
this.supportsBinary = !opts.forceBase64
|
||||
}
|
||||
|
||||
/**
|
||||
* Transport name.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
get name() {
|
||||
return "websocket"
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens socket.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
doOpen() {
|
||||
if (!this.check()) {
|
||||
// let probe timeout
|
||||
return
|
||||
}
|
||||
|
||||
const uri = this.uri()
|
||||
const protocols = this.opts.protocols
|
||||
|
||||
// React Native only supports the 'headers' option, and will print a warning if anything else is passed
|
||||
const opts = isReactNative
|
||||
? {}
|
||||
: pick(
|
||||
this.opts,
|
||||
"agent",
|
||||
"perMessageDeflate",
|
||||
"pfx",
|
||||
"key",
|
||||
"passphrase",
|
||||
"cert",
|
||||
"ca",
|
||||
"ciphers",
|
||||
"rejectUnauthorized",
|
||||
"localAddress",
|
||||
"protocolVersion",
|
||||
"origin",
|
||||
"maxPayload",
|
||||
"family",
|
||||
"checkServerIdentity"
|
||||
)
|
||||
|
||||
if (this.opts.extraHeaders) {
|
||||
opts.headers = this.opts.extraHeaders
|
||||
}
|
||||
|
||||
try {
|
||||
this.ws = new WebSocket(uri, protocols)
|
||||
// usingBrowserWebSocket && !isReactNative
|
||||
// ? protocols
|
||||
// ? new WebSocket(uri, protocols)
|
||||
// : new WebSocket(uri)
|
||||
// : new WebSocket(uri, protocols, opts)
|
||||
} catch (err) {
|
||||
return this.emit("error", err)
|
||||
}
|
||||
|
||||
this.ws.binaryType = this.socket.binaryType || 'arraybuffer'
|
||||
|
||||
this.addEventListeners()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds event listeners to the socket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
addEventListeners() {
|
||||
this.ws.onopen = () => {
|
||||
if (this.opts.autoUnref) {
|
||||
this.ws._socket.unref()
|
||||
}
|
||||
this.onOpen()
|
||||
}
|
||||
this.ws.onclose = this.onClose.bind(this)
|
||||
this.ws.onmessage = ev => this.onData(ev.data)
|
||||
this.ws.onerror = e => this.onError("websocket error", e)
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to socket.
|
||||
*
|
||||
* @param {Array} array of packets.
|
||||
* @api private
|
||||
*/
|
||||
write(packets) {
|
||||
this.writable = false
|
||||
|
||||
// encodePacket efficient as it uses WS framing
|
||||
// no need for encodePayload
|
||||
for (let i = 0; i < packets.length; i++) {
|
||||
const packet = packets[i]
|
||||
const lastPacket = i === packets.length - 1
|
||||
|
||||
parser.encodePacket(packet, this.supportsBinary, data => {
|
||||
// always create a new object (GH-437)
|
||||
const opts: any = {}
|
||||
if (!usingBrowserWebSocket) {
|
||||
if (packet.options) {
|
||||
opts.compress = packet.options.compress
|
||||
}
|
||||
|
||||
if (this.opts.perMessageDeflate) {
|
||||
const len =
|
||||
"string" === typeof data ? Buffer.byteLength(data) : data.length
|
||||
if (len < this.opts.perMessageDeflate.threshold) {
|
||||
opts.compress = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sometimes the websocket has already been closed but the browser didn't
|
||||
// have a chance of informing us about it yet, in that case send will
|
||||
// throw an error
|
||||
try {
|
||||
if (usingBrowserWebSocket) {
|
||||
// TypeError is thrown when passing the second argument on Safari
|
||||
this.ws.send(data)
|
||||
} else {
|
||||
this.ws.send(data, opts)
|
||||
}
|
||||
} catch (e) {
|
||||
debug("websocket closed before onclose event")
|
||||
}
|
||||
|
||||
if (lastPacket) {
|
||||
// fake drain
|
||||
// defer to next tick to allow Socket to clear writeBuffer
|
||||
process.nextTick(() => {
|
||||
this.writable = true
|
||||
this.emit("drain")
|
||||
}, this.setTimeoutFn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon close
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onClose() {
|
||||
Transport.prototype.onClose.call(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes socket.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
doClose() {
|
||||
if (typeof this.ws !== "undefined") {
|
||||
this.ws.close()
|
||||
this.ws = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates uri for connection.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
uri() {
|
||||
let query = this.query || {}
|
||||
const schema = this.opts.secure ? "wss" : "ws"
|
||||
let port = ""
|
||||
|
||||
// avoid port if default for schema
|
||||
if (
|
||||
this.opts.port &&
|
||||
(("wss" === schema && Number(this.opts.port) !== 443) ||
|
||||
("ws" === schema && Number(this.opts.port) !== 80))
|
||||
) {
|
||||
port = ":" + this.opts.port
|
||||
}
|
||||
|
||||
// append timestamp to URI
|
||||
if (this.opts.timestampRequests) {
|
||||
query[this.opts.timestampParam] = yeast()
|
||||
}
|
||||
|
||||
// communicate binary support capabilities
|
||||
if (!this.supportsBinary) {
|
||||
query.b64 = 1
|
||||
}
|
||||
|
||||
query = parseqs.encode(query)
|
||||
|
||||
// prepend ? to query
|
||||
if (query.length) {
|
||||
query = "?" + query
|
||||
}
|
||||
|
||||
const ipv6 = this.opts.hostname.indexOf(":") !== -1
|
||||
return (
|
||||
schema +
|
||||
"://" +
|
||||
(ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) +
|
||||
port +
|
||||
this.opts.path +
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Feature detection for WebSocket.
|
||||
*
|
||||
* @return {Boolean} whether this transport is available.
|
||||
* @api public
|
||||
*/
|
||||
check() {
|
||||
return (
|
||||
!!WebSocket &&
|
||||
!("__initialize" in WebSocket && this.name === WS.prototype.name)
|
||||
)
|
||||
}
|
||||
}
|
||||
23
packages/websocket/src/engine.io-client/util.ts
Normal file
23
packages/websocket/src/engine.io-client/util.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
const pick = (obj, ...attr) => {
|
||||
return attr.reduce((acc, k) => {
|
||||
if (obj.hasOwnProperty(k)) {
|
||||
acc[k] = obj[k]
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
// Keep a reference to the real timeout functions so they can be used when overridden
|
||||
const NATIVE_SET_TIMEOUT = setTimeout
|
||||
const NATIVE_CLEAR_TIMEOUT = clearTimeout
|
||||
|
||||
const installTimerFunctions = (obj, opts) => {
|
||||
if (opts.useNativeTimers) {
|
||||
obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globalThis)
|
||||
obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globalThis)
|
||||
} else {
|
||||
obj.setTimeoutFn = setTimeout.bind(globalThis)
|
||||
obj.clearTimeoutFn = clearTimeout.bind(globalThis)
|
||||
}
|
||||
}
|
||||
export { pick, installTimerFunctions }
|
||||
21
packages/websocket/src/engine.io-parser/commons.ts
Normal file
21
packages/websocket/src/engine.io-parser/commons.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
const PACKET_TYPES = Object.create(null) // no Map = no polyfill
|
||||
PACKET_TYPES["open"] = "0"
|
||||
PACKET_TYPES["close"] = "1"
|
||||
PACKET_TYPES["ping"] = "2"
|
||||
PACKET_TYPES["pong"] = "3"
|
||||
PACKET_TYPES["message"] = "4"
|
||||
PACKET_TYPES["upgrade"] = "5"
|
||||
PACKET_TYPES["noop"] = "6"
|
||||
|
||||
const PACKET_TYPES_REVERSE = Object.create(null)
|
||||
Object.keys(PACKET_TYPES).forEach(key => {
|
||||
PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key
|
||||
})
|
||||
|
||||
const ERROR_PACKET = { type: "error", data: "parser error" }
|
||||
|
||||
export = {
|
||||
PACKET_TYPES,
|
||||
PACKET_TYPES_REVERSE,
|
||||
ERROR_PACKET
|
||||
}
|
||||
48
packages/websocket/src/engine.io-parser/decodePacket.ts
Normal file
48
packages/websocket/src/engine.io-parser/decodePacket.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
const { PACKET_TYPES_REVERSE, ERROR_PACKET } = require("./commons")
|
||||
|
||||
export const decodePacket = (encodedPacket, binaryType) => {
|
||||
if (typeof encodedPacket !== "string") {
|
||||
return {
|
||||
type: "message",
|
||||
data: mapBinary(encodedPacket, binaryType)
|
||||
}
|
||||
}
|
||||
const type = encodedPacket.charAt(0)
|
||||
if (type === "b") {
|
||||
const buffer = Buffer.from(encodedPacket.substring(1), "base64")
|
||||
return {
|
||||
type: "message",
|
||||
data: mapBinary(buffer, binaryType)
|
||||
}
|
||||
}
|
||||
if (!PACKET_TYPES_REVERSE[type]) {
|
||||
return ERROR_PACKET
|
||||
}
|
||||
return encodedPacket.length > 1
|
||||
? {
|
||||
type: PACKET_TYPES_REVERSE[type],
|
||||
data: encodedPacket.substring(1)
|
||||
}
|
||||
: {
|
||||
type: PACKET_TYPES_REVERSE[type]
|
||||
}
|
||||
}
|
||||
|
||||
const mapBinary = (data, binaryType) => {
|
||||
switch (binaryType) {
|
||||
case "arraybuffer":
|
||||
return Buffer.isBuffer(data) ? toArrayBuffer(data) : data
|
||||
case "nodebuffer":
|
||||
default:
|
||||
return data // assuming the data is already a Buffer
|
||||
}
|
||||
}
|
||||
|
||||
const toArrayBuffer = buffer => {
|
||||
const arrayBuffer = new ArrayBuffer(buffer.length)
|
||||
const view = new Uint8Array(arrayBuffer)
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
view[i] = buffer[i]
|
||||
}
|
||||
return arrayBuffer
|
||||
}
|
||||
26
packages/websocket/src/engine.io-parser/encodePacket.ts
Normal file
26
packages/websocket/src/engine.io-parser/encodePacket.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
const { PACKET_TYPES } = require("./commons")
|
||||
|
||||
export const encodePacket = ({ type, data }, supportsBinary, callback) => {
|
||||
console.trace('encodePacket', type, JSON.stringify(data))
|
||||
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
||||
const buffer = toBuffer(data)
|
||||
return callback(encodeBuffer(buffer, supportsBinary))
|
||||
}
|
||||
// plain string
|
||||
return callback(PACKET_TYPES[type] + (data || ""))
|
||||
}
|
||||
|
||||
const toBuffer = data => {
|
||||
if (Buffer.isBuffer(data)) {
|
||||
return data
|
||||
} else if (data instanceof ArrayBuffer) {
|
||||
return Buffer.from(data)
|
||||
} else {
|
||||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength)
|
||||
}
|
||||
}
|
||||
|
||||
// only 'message' packets can contain binary, so the type prefix is not needed
|
||||
const encodeBuffer = (data, supportsBinary) => {
|
||||
return supportsBinary ? data : "b" + data.toString("base64")
|
||||
}
|
||||
42
packages/websocket/src/engine.io-parser/index.ts
Normal file
42
packages/websocket/src/engine.io-parser/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { encodePacket } from "./encodePacket"
|
||||
import { decodePacket } from "./decodePacket"
|
||||
|
||||
const SEPARATOR = String.fromCharCode(30) // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
|
||||
|
||||
const encodePayload = (packets, callback) => {
|
||||
// some packets may be added to the array while encoding, so the initial length must be saved
|
||||
const length = packets.length
|
||||
const encodedPackets = new Array(length)
|
||||
let count = 0
|
||||
|
||||
packets.forEach((packet, i) => {
|
||||
// force base64 encoding for binary packets
|
||||
encodePacket(packet, false, encodedPacket => {
|
||||
encodedPackets[i] = encodedPacket
|
||||
if (++count === length) {
|
||||
callback(encodedPackets.join(SEPARATOR))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const decodePayload = (encodedPayload, binaryType) => {
|
||||
const encodedPackets = encodedPayload.split(SEPARATOR)
|
||||
const packets = []
|
||||
for (let i = 0; i < encodedPackets.length; i++) {
|
||||
const decodedPacket = decodePacket(encodedPackets[i], binaryType)
|
||||
packets.push(decodedPacket)
|
||||
if (decodedPacket.type === "error") {
|
||||
break
|
||||
}
|
||||
}
|
||||
return packets
|
||||
}
|
||||
|
||||
export default {
|
||||
protocol: 4,
|
||||
encodePacket,
|
||||
encodePayload,
|
||||
decodePacket,
|
||||
decodePayload
|
||||
}
|
||||
27
packages/websocket/src/engine.io/index.ts
Normal file
27
packages/websocket/src/engine.io/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
import * as server from "../server"
|
||||
// const http = require("http")
|
||||
// const Server = require("./server")
|
||||
import { Server } from './server'
|
||||
|
||||
/**
|
||||
* Captures upgrade requests for a http.Server.
|
||||
*
|
||||
* @param {http.Server} server
|
||||
* @param {Object} options
|
||||
* @return {Server} engine server
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function attach(srv, options) {
|
||||
const engine = new Server(options)
|
||||
engine.attach(server.attach(srv, options), options)
|
||||
return engine
|
||||
}
|
||||
|
||||
export = {
|
||||
attach
|
||||
}
|
||||
690
packages/websocket/src/engine.io/server.ts
Normal file
690
packages/websocket/src/engine.io/server.ts
Normal file
@@ -0,0 +1,690 @@
|
||||
const qs = require("querystring")
|
||||
const parse = require("url").parse
|
||||
// const base64id = require("base64id")
|
||||
import transports from './transports'
|
||||
import { EventEmitter } from 'events'
|
||||
// const EventEmitter = require("events").EventEmitter
|
||||
import { Socket } from './socket'
|
||||
// const debug = require("debug")("engine")
|
||||
const debug = function (...args) { }
|
||||
// const cookieMod = require("cookie")
|
||||
|
||||
// const DEFAULT_WS_ENGINE = require("ws").Server;
|
||||
import { WebSocketServer } from '../server'
|
||||
import { Transport } from './transport'
|
||||
const DEFAULT_WS_ENGINE = WebSocketServer
|
||||
|
||||
import { Request } from '../server/request'
|
||||
import { WebSocketClient } from '../server/client'
|
||||
|
||||
export class Server extends EventEmitter {
|
||||
public static errors = {
|
||||
UNKNOWN_TRANSPORT: 0,
|
||||
UNKNOWN_SID: 1,
|
||||
BAD_HANDSHAKE_METHOD: 2,
|
||||
BAD_REQUEST: 3,
|
||||
FORBIDDEN: 4,
|
||||
UNSUPPORTED_PROTOCOL_VERSION: 5
|
||||
}
|
||||
|
||||
public static errorMessages = {
|
||||
0: "Transport unknown",
|
||||
1: "Session ID unknown",
|
||||
2: "Bad handshake method",
|
||||
3: "Bad request",
|
||||
4: "Forbidden",
|
||||
5: "Unsupported protocol version"
|
||||
}
|
||||
|
||||
private clients = {}
|
||||
private clientsCount = 0
|
||||
public opts: any
|
||||
|
||||
private corsMiddleware: any
|
||||
|
||||
private ws: any
|
||||
private perMessageDeflate: any
|
||||
|
||||
constructor(opts: any = {}) {
|
||||
super()
|
||||
this.opts = Object.assign(
|
||||
{
|
||||
wsEngine: DEFAULT_WS_ENGINE,
|
||||
pingTimeout: 20000,
|
||||
pingInterval: 25000,
|
||||
upgradeTimeout: 10000,
|
||||
maxHttpBufferSize: 1e6,
|
||||
transports: Object.keys(transports),
|
||||
allowUpgrades: true,
|
||||
httpCompression: {
|
||||
threshold: 1024
|
||||
},
|
||||
cors: false,
|
||||
allowEIO3: false
|
||||
},
|
||||
opts
|
||||
)
|
||||
|
||||
// if (opts.cookie) {
|
||||
// this.opts.cookie = Object.assign(
|
||||
// {
|
||||
// name: "io",
|
||||
// path: "/",
|
||||
// httpOnly: opts.cookie.path !== false,
|
||||
// sameSite: "lax"
|
||||
// },
|
||||
// opts.cookie
|
||||
// )
|
||||
// }
|
||||
|
||||
// if (this.opts.cors) {
|
||||
// this.corsMiddleware = require("cors")(this.opts.cors)
|
||||
// }
|
||||
|
||||
// if (opts.perMessageDeflate) {
|
||||
// this.opts.perMessageDeflate = Object.assign(
|
||||
// {
|
||||
// threshold: 1024
|
||||
// },
|
||||
// opts.perMessageDeflate
|
||||
// )
|
||||
// }
|
||||
|
||||
// this.init()
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Initialize websocket server
|
||||
// *
|
||||
// * @api private
|
||||
// */
|
||||
// init() {
|
||||
// if (!~this.opts.transports.indexOf("websocket")) return
|
||||
|
||||
// if (this.ws) this.ws.close()
|
||||
|
||||
// this.ws = new this.opts.wsEngine({
|
||||
// noServer: true,
|
||||
// clientTracking: false,
|
||||
// perMessageDeflate: this.opts.perMessageDeflate,
|
||||
// maxPayload: this.opts.maxHttpBufferSize
|
||||
// })
|
||||
|
||||
// if (typeof this.ws.on === "function") {
|
||||
// this.ws.on("headers", (headersArray, req) => {
|
||||
// // note: 'ws' uses an array of headers, while Engine.IO uses an object (response.writeHead() accepts both formats)
|
||||
// // we could also try to parse the array and then sync the values, but that will be error-prone
|
||||
// const additionalHeaders = {}
|
||||
|
||||
// const isInitialRequest = !req._query.sid
|
||||
// if (isInitialRequest) {
|
||||
// this.emit("initial_headers", additionalHeaders, req)
|
||||
// }
|
||||
|
||||
// this.emit("headers", additionalHeaders, req)
|
||||
|
||||
// Object.keys(additionalHeaders).forEach(key => {
|
||||
// headersArray.push(`${key}: ${additionalHeaders[key]}`)
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Returns a list of available transports for upgrade given a certain transport.
|
||||
*
|
||||
* @return {Array}
|
||||
* @api public
|
||||
*/
|
||||
upgrades(transport): Array<any> {
|
||||
if (!this.opts.allowUpgrades) return []
|
||||
return transports[transport].upgradesTo || []
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Verifies a request.
|
||||
// *
|
||||
// * @param {http.IncomingMessage}
|
||||
// * @return {Boolean} whether the request is valid
|
||||
// * @api private
|
||||
// */
|
||||
// verify(req, upgrade, fn) {
|
||||
// // transport check
|
||||
// const transport = req._query.transport
|
||||
// if (!~this.opts.transports.indexOf(transport)) {
|
||||
// debug('unknown transport "%s"', transport)
|
||||
// return fn(Server.errors.UNKNOWN_TRANSPORT, { transport })
|
||||
// }
|
||||
|
||||
// // 'Origin' header check
|
||||
// const isOriginInvalid = checkInvalidHeaderChar(req.headers.origin)
|
||||
// if (isOriginInvalid) {
|
||||
// const origin = req.headers.origin
|
||||
// req.headers.origin = null
|
||||
// debug("origin header invalid")
|
||||
// return fn(Server.errors.BAD_REQUEST, {
|
||||
// name: "INVALID_ORIGIN",
|
||||
// origin
|
||||
// })
|
||||
// }
|
||||
|
||||
// // sid check
|
||||
// const sid = req._query.sid
|
||||
// if (sid) {
|
||||
// if (!this.clients.hasOwnProperty(sid)) {
|
||||
// debug('unknown sid "%s"', sid)
|
||||
// return fn(Server.errors.UNKNOWN_SID, {
|
||||
// sid
|
||||
// })
|
||||
// }
|
||||
// const previousTransport = this.clients[sid].transport.name
|
||||
// if (!upgrade && previousTransport !== transport) {
|
||||
// debug("bad request: unexpected transport without upgrade")
|
||||
// return fn(Server.errors.BAD_REQUEST, {
|
||||
// name: "TRANSPORT_MISMATCH",
|
||||
// transport,
|
||||
// previousTransport
|
||||
// })
|
||||
// }
|
||||
// } else {
|
||||
// // handshake is GET only
|
||||
// if ("GET" !== req.method) {
|
||||
// return fn(Server.errors.BAD_HANDSHAKE_METHOD, {
|
||||
// method: req.method
|
||||
// })
|
||||
// }
|
||||
|
||||
// if (!this.opts.allowRequest) return fn()
|
||||
|
||||
// return this.opts.allowRequest(req, (message, success) => {
|
||||
// if (!success) {
|
||||
// return fn(Server.errors.FORBIDDEN, {
|
||||
// message
|
||||
// })
|
||||
// }
|
||||
// fn()
|
||||
// })
|
||||
// }
|
||||
|
||||
// fn()
|
||||
// }
|
||||
|
||||
/**
|
||||
* Prepares a request by processing the query string.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
prepare(req) {
|
||||
// try to leverage pre-existing `req._query` (e.g: from connect)
|
||||
if (!req._query) {
|
||||
req._query = ~req.url.indexOf("?") ? qs.parse(parse(req.url).query) : {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes all clients.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
close() {
|
||||
debug("closing all open clients")
|
||||
for (let i in this.clients) {
|
||||
if (this.clients.hasOwnProperty(i)) {
|
||||
this.clients[i].close(true)
|
||||
}
|
||||
}
|
||||
if (this.ws) {
|
||||
debug("closing webSocketServer")
|
||||
this.ws.close()
|
||||
// don't delete this.ws because it can be used again if the http server starts listening again
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Handles an Engine.IO HTTP request.
|
||||
// *
|
||||
// * @param {http.IncomingMessage} request
|
||||
// * @param {http.ServerResponse|http.OutgoingMessage} response
|
||||
// * @api public
|
||||
// */
|
||||
// handleRequest(req, res) {
|
||||
// debug('handling "%s" http request "%s"', req.method, req.url)
|
||||
// this.prepare(req)
|
||||
// req.res = res
|
||||
|
||||
// const callback = (errorCode, errorContext) => {
|
||||
// if (errorCode !== undefined) {
|
||||
// this.emit("connection_error", {
|
||||
// req,
|
||||
// code: errorCode,
|
||||
// message: Server.errorMessages[errorCode],
|
||||
// context: errorContext
|
||||
// })
|
||||
// abortRequest(res, errorCode, errorContext)
|
||||
// return
|
||||
// }
|
||||
|
||||
// if (req._query.sid) {
|
||||
// debug("setting new request for existing client")
|
||||
// this.clients[req._query.sid].transport.onRequest(req)
|
||||
// } else {
|
||||
// const closeConnection = (errorCode, errorContext) =>
|
||||
// abortRequest(res, errorCode, errorContext)
|
||||
// this.handshake(req._query.transport, req, closeConnection)
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (this.corsMiddleware) {
|
||||
// this.corsMiddleware.call(null, req, res, () => {
|
||||
// this.verify(req, false, callback)
|
||||
// })
|
||||
// } else {
|
||||
// this.verify(req, false, callback)
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* generate a socket id.
|
||||
* Overwrite this method to generate your custom socket id
|
||||
*
|
||||
* @param {Object} request object
|
||||
* @api public
|
||||
*/
|
||||
generateId(req) {
|
||||
return req.id
|
||||
}
|
||||
|
||||
/**
|
||||
* Handshakes a new client.
|
||||
*
|
||||
* @param {String} transport name
|
||||
* @param {Object} request object
|
||||
* @param {Function} closeConnection
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
// @java-patch sync handshake
|
||||
handshake(transportName, req, closeConnection: (code: number) => void) {
|
||||
console.debug('engine.io server handshake transport', transportName, 'from', req.url)
|
||||
const protocol = req._query.EIO === "4" ? 4 : 3 // 3rd revision by default
|
||||
if (protocol === 3 && !this.opts.allowEIO3) {
|
||||
debug("unsupported protocol version")
|
||||
this.emit("connection_error", {
|
||||
req,
|
||||
code: Server.errors.UNSUPPORTED_PROTOCOL_VERSION,
|
||||
message:
|
||||
Server.errorMessages[Server.errors.UNSUPPORTED_PROTOCOL_VERSION],
|
||||
context: {
|
||||
protocol
|
||||
}
|
||||
})
|
||||
closeConnection(Server.errors.UNSUPPORTED_PROTOCOL_VERSION)
|
||||
return
|
||||
}
|
||||
|
||||
let id
|
||||
try {
|
||||
id = this.generateId(req)
|
||||
} catch (e) {
|
||||
console.debug("error while generating an id")
|
||||
this.emit("connection_error", {
|
||||
req,
|
||||
code: Server.errors.BAD_REQUEST,
|
||||
message: Server.errorMessages[Server.errors.BAD_REQUEST],
|
||||
context: {
|
||||
name: "ID_GENERATION_ERROR",
|
||||
error: e
|
||||
}
|
||||
})
|
||||
closeConnection(Server.errors.BAD_REQUEST)
|
||||
return
|
||||
}
|
||||
|
||||
console.debug('engine.io server handshaking client "' + id + '"')
|
||||
|
||||
try {
|
||||
var transport: Transport = new transports[transportName](req)
|
||||
if ("websocket" !== transportName) {
|
||||
throw new Error('Unsupport polling at MiaoScript!')
|
||||
}
|
||||
// if ("polling" === transportName) {
|
||||
// transport.maxHttpBufferSize = this.opts.maxHttpBufferSize
|
||||
// transport.httpCompression = this.opts.httpCompression
|
||||
// } else if ("websocket" === transportName) {
|
||||
transport.perMessageDeflate = this.opts.perMessageDeflate
|
||||
// }
|
||||
|
||||
if (req._query && req._query.b64) {
|
||||
transport.supportsBinary = false
|
||||
} else {
|
||||
transport.supportsBinary = true
|
||||
}
|
||||
} catch (e) {
|
||||
console.ex(e)
|
||||
this.emit("connection_error", {
|
||||
req,
|
||||
code: Server.errors.BAD_REQUEST,
|
||||
message: Server.errorMessages[Server.errors.BAD_REQUEST],
|
||||
context: {
|
||||
name: "TRANSPORT_HANDSHAKE_ERROR",
|
||||
error: e
|
||||
}
|
||||
})
|
||||
closeConnection(Server.errors.BAD_REQUEST)
|
||||
return
|
||||
}
|
||||
console.debug(`engine.io server create socket ${id} from transport ${transport.name} protocol ${protocol}`)
|
||||
const socket = new Socket(id, this, transport, req, protocol)
|
||||
|
||||
transport.on("headers", (headers, req) => {
|
||||
const isInitialRequest = !req._query.sid
|
||||
|
||||
if (isInitialRequest) {
|
||||
if (this.opts.cookie) {
|
||||
headers["Set-Cookie"] = [
|
||||
// cookieMod.serialize(this.opts.cookie.name, id, this.opts.cookie)
|
||||
]
|
||||
}
|
||||
this.emit("initial_headers", headers, req)
|
||||
}
|
||||
this.emit("headers", headers, req)
|
||||
})
|
||||
|
||||
transport.onRequest(req)
|
||||
|
||||
this.clients[id] = socket
|
||||
this.clientsCount++
|
||||
|
||||
socket.once("close", () => {
|
||||
delete this.clients[id]
|
||||
this.clientsCount--
|
||||
})
|
||||
this.emit("connection", socket)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Handles an Engine.IO HTTP Upgrade.
|
||||
// *
|
||||
// * @api public
|
||||
// */
|
||||
// handleUpgrade(req, socket, upgradeHead) {
|
||||
// this.prepare(req)
|
||||
|
||||
// this.verify(req, true, (errorCode, errorContext) => {
|
||||
// if (errorCode) {
|
||||
// this.emit("connection_error", {
|
||||
// req,
|
||||
// code: errorCode,
|
||||
// message: Server.errorMessages[errorCode],
|
||||
// context: errorContext
|
||||
// })
|
||||
// abortUpgrade(socket, errorCode, errorContext)
|
||||
// return
|
||||
// }
|
||||
|
||||
// const head = Buffer.from(upgradeHead) // eslint-disable-line node/no-deprecated-api
|
||||
// upgradeHead = null
|
||||
|
||||
// // delegate to ws
|
||||
// this.ws.handleUpgrade(req, socket, head, websocket => {
|
||||
// this.onWebSocket(req, socket, websocket)
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
|
||||
/**
|
||||
* Called upon a ws.io connection.
|
||||
*
|
||||
* @param {ws.Socket} websocket
|
||||
* @api private
|
||||
*/
|
||||
onWebSocket(req: Request, socket, websocket: WebSocketClient) {
|
||||
websocket.on("error", onUpgradeError)
|
||||
|
||||
if (
|
||||
transports[req._query.transport] !== undefined &&
|
||||
!transports[req._query.transport].prototype.handlesUpgrades
|
||||
) {
|
||||
console.debug("transport doesnt handle upgraded requests")
|
||||
websocket.close()
|
||||
return
|
||||
}
|
||||
|
||||
// get client id
|
||||
const id = req._query.sid
|
||||
|
||||
// keep a reference to the ws.Socket
|
||||
req.websocket = websocket
|
||||
|
||||
if (id) {
|
||||
const client = this.clients[id]
|
||||
if (!client) {
|
||||
console.debug("upgrade attempt for closed client")
|
||||
websocket.close()
|
||||
} else if (client.upgrading) {
|
||||
console.debug("transport has already been trying to upgrade")
|
||||
websocket.close()
|
||||
} else if (client.upgraded) {
|
||||
console.debug("transport had already been upgraded")
|
||||
websocket.close()
|
||||
} else {
|
||||
console.debug("upgrading existing transport")
|
||||
|
||||
// transport error handling takes over
|
||||
websocket.removeListener("error", onUpgradeError)
|
||||
|
||||
const transport = new transports[req._query.transport](req)
|
||||
if (req._query && req._query.b64) {
|
||||
transport.supportsBinary = false
|
||||
} else {
|
||||
transport.supportsBinary = true
|
||||
}
|
||||
transport.perMessageDeflate = this.perMessageDeflate
|
||||
client.maybeUpgrade(transport)
|
||||
}
|
||||
} else {
|
||||
// transport error handling takes over
|
||||
websocket.removeListener("error", onUpgradeError)
|
||||
|
||||
// const closeConnection = (errorCode, errorContext) =>
|
||||
// abortUpgrade(socket, errorCode, errorContext)
|
||||
this.handshake(req._query.transport, req, () => { })
|
||||
}
|
||||
|
||||
function onUpgradeError() {
|
||||
console.debug("websocket error before upgrade")
|
||||
// websocket.close() not needed
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures upgrade requests for a http.Server.
|
||||
*
|
||||
* @param {http.Server} server
|
||||
* @param {Object} options
|
||||
* @api public
|
||||
*/
|
||||
attach(server, options: any = {}) {
|
||||
// let path = (options.path || "/engine.io").replace(/\/$/, "")
|
||||
|
||||
// const destroyUpgradeTimeout = options.destroyUpgradeTimeout || 1000
|
||||
|
||||
// // normalize path
|
||||
// path += "/"
|
||||
|
||||
// function check(req) {
|
||||
// return path === req.url.substr(0, path.length)
|
||||
// }
|
||||
|
||||
// cache and clean up listeners
|
||||
// const listeners = server.listeners("request").slice(0)
|
||||
// server.removeAllListeners("request")
|
||||
server.on("close", this.close.bind(this))
|
||||
// server.on("listening", this.init.bind(this))
|
||||
// @java-patch transfer to Netty Server
|
||||
server.on("connect", (request: Request, websocket: WebSocketClient) => {
|
||||
console.debug('Engine.IO connect client from', request.url)
|
||||
this.prepare(request)
|
||||
this.onWebSocket(request, undefined, websocket)
|
||||
})
|
||||
// set server as ws server
|
||||
this.ws = server
|
||||
|
||||
// // add request handler
|
||||
// server.on("request", (req, res) => {
|
||||
// if (check(req)) {
|
||||
// debug('intercepting request for path "%s"', path)
|
||||
// this.handleRequest(req, res)
|
||||
// } else {
|
||||
// let i = 0
|
||||
// const l = listeners.length
|
||||
// for (; i < l; i++) {
|
||||
// listeners[i].call(server, req, res)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
|
||||
// if (~this.opts.transports.indexOf("websocket")) {
|
||||
// server.on("upgrade", (req, socket, head) => {
|
||||
// if (check(req)) {
|
||||
// this.handleUpgrade(req, socket, head)
|
||||
// } else if (false !== options.destroyUpgrade) {
|
||||
// // default node behavior is to disconnect when no handlers
|
||||
// // but by adding a handler, we prevent that
|
||||
// // and if no eio thing handles the upgrade
|
||||
// // then the socket needs to die!
|
||||
// setTimeout(function () {
|
||||
// if (socket.writable && socket.bytesWritten <= 0) {
|
||||
// return socket.end()
|
||||
// }
|
||||
// }, destroyUpgradeTimeout)
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Close the HTTP long-polling request
|
||||
// *
|
||||
// * @param res - the response object
|
||||
// * @param errorCode - the error code
|
||||
// * @param errorContext - additional error context
|
||||
// *
|
||||
// * @api private
|
||||
// */
|
||||
|
||||
// function abortRequest(res, errorCode, errorContext) {
|
||||
// const statusCode = errorCode === Server.errors.FORBIDDEN ? 403 : 400
|
||||
// const message =
|
||||
// errorContext && errorContext.message
|
||||
// ? errorContext.message
|
||||
// : Server.errorMessages[errorCode]
|
||||
|
||||
// res.writeHead(statusCode, { "Content-Type": "application/json" })
|
||||
// res.end(
|
||||
// JSON.stringify({
|
||||
// code: errorCode,
|
||||
// message
|
||||
// })
|
||||
// )
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Close the WebSocket connection
|
||||
// *
|
||||
// * @param {net.Socket} socket
|
||||
// * @param {string} errorCode - the error code
|
||||
// * @param {object} errorContext - additional error context
|
||||
// *
|
||||
// * @api private
|
||||
// */
|
||||
|
||||
// function abortUpgrade(socket, errorCode, errorContext: any = {}) {
|
||||
// socket.on("error", () => {
|
||||
// debug("ignoring error from closed connection")
|
||||
// })
|
||||
// if (socket.writable) {
|
||||
// const message = errorContext.message || Server.errorMessages[errorCode]
|
||||
// const length = Buffer.byteLength(message)
|
||||
// socket.write(
|
||||
// "HTTP/1.1 400 Bad Request\r\n" +
|
||||
// "Connection: close\r\n" +
|
||||
// "Content-type: text/html\r\n" +
|
||||
// "Content-Length: " +
|
||||
// length +
|
||||
// "\r\n" +
|
||||
// "\r\n" +
|
||||
// message
|
||||
// )
|
||||
// }
|
||||
// socket.destroy()
|
||||
// }
|
||||
|
||||
// module.exports = Server
|
||||
|
||||
/* eslint-disable */
|
||||
|
||||
// /**
|
||||
// * From https://github.com/nodejs/node/blob/v8.4.0/lib/_http_common.js#L303-L354
|
||||
// *
|
||||
// * True if val contains an invalid field-vchar
|
||||
// * field-value = *( field-content / obs-fold )
|
||||
// * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
||||
// * field-vchar = VCHAR / obs-text
|
||||
// *
|
||||
// * checkInvalidHeaderChar() is currently designed to be inlinable by v8,
|
||||
// * so take care when making changes to the implementation so that the source
|
||||
// * code size does not exceed v8's default max_inlined_source_size setting.
|
||||
// **/
|
||||
// // prettier-ignore
|
||||
// const validHdrChars = [
|
||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, // 0 - 15
|
||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80 - 95
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112 - 127
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128 ...
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // ... 255
|
||||
// ]
|
||||
|
||||
// function checkInvalidHeaderChar(val) {
|
||||
// val += ""
|
||||
// if (val.length < 1) return false
|
||||
// if (!validHdrChars[val.charCodeAt(0)]) {
|
||||
// debug('invalid header, index 0, char "%s"', val.charCodeAt(0))
|
||||
// return true
|
||||
// }
|
||||
// if (val.length < 2) return false
|
||||
// if (!validHdrChars[val.charCodeAt(1)]) {
|
||||
// debug('invalid header, index 1, char "%s"', val.charCodeAt(1))
|
||||
// return true
|
||||
// }
|
||||
// if (val.length < 3) return false
|
||||
// if (!validHdrChars[val.charCodeAt(2)]) {
|
||||
// debug('invalid header, index 2, char "%s"', val.charCodeAt(2))
|
||||
// return true
|
||||
// }
|
||||
// if (val.length < 4) return false
|
||||
// if (!validHdrChars[val.charCodeAt(3)]) {
|
||||
// debug('invalid header, index 3, char "%s"', val.charCodeAt(3))
|
||||
// return true
|
||||
// }
|
||||
// for (let i = 4; i < val.length; ++i) {
|
||||
// if (!validHdrChars[val.charCodeAt(i)]) {
|
||||
// debug('invalid header, index "%i", char "%s"', i, val.charCodeAt(i))
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
530
packages/websocket/src/engine.io/socket.ts
Normal file
530
packages/websocket/src/engine.io/socket.ts
Normal file
@@ -0,0 +1,530 @@
|
||||
import { EventEmitter } from "events"
|
||||
import { Server } from "./server"
|
||||
import { Transport } from "./transport"
|
||||
import type { Request } from "../server/request"
|
||||
// const debug = require("debug")("engine:socket")
|
||||
|
||||
export class Socket extends EventEmitter {
|
||||
public id: string
|
||||
private server: Server
|
||||
private upgrading = false
|
||||
private upgraded = false
|
||||
public readyState = "opening"
|
||||
private writeBuffer = []
|
||||
private packetsFn = []
|
||||
private sentCallbackFn = []
|
||||
private cleanupFn = []
|
||||
public request: Request
|
||||
public protocol: number
|
||||
public remoteAddress: any
|
||||
public transport: Transport
|
||||
|
||||
private checkIntervalTimer: NodeJS.Timeout
|
||||
private upgradeTimeoutTimer: NodeJS.Timeout
|
||||
private pingTimeoutTimer: NodeJS.Timeout
|
||||
private pingIntervalTimer: NodeJS.Timeout
|
||||
|
||||
/**
|
||||
* Client class (abstract).
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
constructor(id: string, server: Server, transport: Transport, req: Request, protocol: number) {
|
||||
super()
|
||||
this.id = id
|
||||
this.server = server
|
||||
this.request = req
|
||||
this.protocol = protocol
|
||||
|
||||
// Cache IP since it might not be in the req later
|
||||
if (req.websocket && req.websocket._socket) {
|
||||
this.remoteAddress = req.websocket._socket.remoteAddress
|
||||
} else {
|
||||
this.remoteAddress = req.connection.remoteAddress
|
||||
}
|
||||
|
||||
this.checkIntervalTimer = null
|
||||
this.upgradeTimeoutTimer = null
|
||||
this.pingTimeoutTimer = null
|
||||
this.pingIntervalTimer = null
|
||||
|
||||
this.setTransport(transport)
|
||||
this.onOpen()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport considered open.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onOpen() {
|
||||
this.readyState = "open"
|
||||
|
||||
// sends an `open` packet
|
||||
this.transport.sid = this.id
|
||||
this.sendPacket(
|
||||
"open",
|
||||
JSON.stringify({
|
||||
sid: this.id,
|
||||
upgrades: this.getAvailableUpgrades(),
|
||||
pingInterval: this.server.opts.pingInterval,
|
||||
pingTimeout: this.server.opts.pingTimeout
|
||||
})
|
||||
)
|
||||
|
||||
if (this.server.opts.initialPacket) {
|
||||
this.sendPacket("message", this.server.opts.initialPacket)
|
||||
}
|
||||
|
||||
this.emit("open")
|
||||
|
||||
if (this.protocol === 3) {
|
||||
// in protocol v3, the client sends a ping, and the server answers with a pong
|
||||
this.resetPingTimeout(
|
||||
this.server.opts.pingInterval + this.server.opts.pingTimeout
|
||||
)
|
||||
} else {
|
||||
// in protocol v4, the server sends a ping, and the client answers with a pong
|
||||
this.schedulePing()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport packet.
|
||||
*
|
||||
* @param {Object} packet
|
||||
* @api private
|
||||
*/
|
||||
onPacket(packet: { type: any; data: any }) {
|
||||
if ("open" !== this.readyState) {
|
||||
console.debug("packet received with closed socket")
|
||||
return
|
||||
}
|
||||
// export packet event
|
||||
// debug(`received packet ${packet.type}`)
|
||||
this.emit("packet", packet)
|
||||
|
||||
// Reset ping timeout on any packet, incoming data is a good sign of
|
||||
// other side's liveness
|
||||
this.resetPingTimeout(
|
||||
this.server.opts.pingInterval + this.server.opts.pingTimeout
|
||||
)
|
||||
|
||||
switch (packet.type) {
|
||||
case "ping":
|
||||
if (this.transport.protocol !== 3) {
|
||||
this.onError("invalid heartbeat direction")
|
||||
return
|
||||
}
|
||||
// debug("got ping")
|
||||
this.sendPacket("pong")
|
||||
this.emit("heartbeat")
|
||||
break
|
||||
|
||||
case "pong":
|
||||
if (this.transport.protocol === 3) {
|
||||
this.onError("invalid heartbeat direction")
|
||||
return
|
||||
}
|
||||
// debug("got pong")
|
||||
this.schedulePing()
|
||||
this.emit("heartbeat")
|
||||
break
|
||||
|
||||
case "error":
|
||||
this.onClose("parse error")
|
||||
break
|
||||
|
||||
case "message":
|
||||
this.emit("data", packet.data)
|
||||
this.emit("message", packet.data)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport error.
|
||||
*
|
||||
* @param {Error} error object
|
||||
* @api private
|
||||
*/
|
||||
onError(err: string) {
|
||||
// debug("transport error")
|
||||
this.onClose("transport error", err)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings client every `this.pingInterval` and expects response
|
||||
* within `this.pingTimeout` or closes connection.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
schedulePing() {
|
||||
clearTimeout(this.pingIntervalTimer)
|
||||
this.pingIntervalTimer = setTimeout(() => {
|
||||
// debug(
|
||||
// "writing ping packet - expecting pong within %sms",
|
||||
// this.server.opts.pingTimeout
|
||||
// )
|
||||
this.sendPacket("ping")
|
||||
this.resetPingTimeout(this.server.opts.pingTimeout)
|
||||
}, this.server.opts.pingInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets ping timeout.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
resetPingTimeout(timeout: number) {
|
||||
clearTimeout(this.pingTimeoutTimer)
|
||||
this.pingTimeoutTimer = setTimeout(() => {
|
||||
if (this.readyState === "closed") return
|
||||
this.onClose("ping timeout")
|
||||
}, timeout)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches handlers for the given transport.
|
||||
*
|
||||
* @param {Transport} transport
|
||||
* @api private
|
||||
*/
|
||||
setTransport(transport: Transport) {
|
||||
console.debug(`engine.io socket ${this.id} set transport ${transport.name}`)
|
||||
const onError = this.onError.bind(this)
|
||||
const onPacket = this.onPacket.bind(this)
|
||||
const flush = this.flush.bind(this)
|
||||
const onClose = this.onClose.bind(this, "transport close")
|
||||
|
||||
this.transport = transport
|
||||
this.transport.once("error", onError)
|
||||
this.transport.on("packet", onPacket)
|
||||
this.transport.on("drain", flush)
|
||||
this.transport.once("close", onClose)
|
||||
// this function will manage packet events (also message callbacks)
|
||||
this.setupSendCallback()
|
||||
|
||||
this.cleanupFn.push(function () {
|
||||
transport.removeListener("error", onError)
|
||||
transport.removeListener("packet", onPacket)
|
||||
transport.removeListener("drain", flush)
|
||||
transport.removeListener("close", onClose)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrades socket to the given transport
|
||||
*
|
||||
* @param {Transport} transport
|
||||
* @api private
|
||||
*/
|
||||
maybeUpgrade(transport: Transport) {
|
||||
console.debug(
|
||||
'might upgrade socket transport from "', this.transport.name, '" to "', transport.name, '"'
|
||||
)
|
||||
|
||||
this.upgrading = true
|
||||
|
||||
// set transport upgrade timer
|
||||
this.upgradeTimeoutTimer = setTimeout(() => {
|
||||
console.debug("client did not complete upgrade - closing transport")
|
||||
cleanup()
|
||||
if ("open" === transport.readyState) {
|
||||
transport.close()
|
||||
}
|
||||
}, this.server.opts.upgradeTimeout)
|
||||
|
||||
const onPacket = (packet: { type: string; data: string }) => {
|
||||
if ("ping" === packet.type && "probe" === packet.data) {
|
||||
transport.send([{ type: "pong", data: "probe" }])
|
||||
this.emit("upgrading", transport)
|
||||
clearInterval(this.checkIntervalTimer)
|
||||
this.checkIntervalTimer = setInterval(check, 100)
|
||||
} else if ("upgrade" === packet.type && this.readyState !== "closed") {
|
||||
// debug("got upgrade packet - upgrading")
|
||||
cleanup()
|
||||
this.transport.discard()
|
||||
this.upgraded = true
|
||||
this.clearTransport()
|
||||
this.setTransport(transport)
|
||||
this.emit("upgrade", transport)
|
||||
this.flush()
|
||||
if (this.readyState === "closing") {
|
||||
transport.close(() => {
|
||||
this.onClose("forced close")
|
||||
})
|
||||
}
|
||||
} else {
|
||||
cleanup()
|
||||
transport.close()
|
||||
}
|
||||
}
|
||||
|
||||
// we force a polling cycle to ensure a fast upgrade
|
||||
const check = () => {
|
||||
if ("polling" === this.transport.name && this.transport.writable) {
|
||||
// debug("writing a noop packet to polling for fast upgrade")
|
||||
this.transport.send([{ type: "noop" }])
|
||||
}
|
||||
}
|
||||
|
||||
const cleanup = () => {
|
||||
this.upgrading = false
|
||||
|
||||
clearInterval(this.checkIntervalTimer)
|
||||
this.checkIntervalTimer = null
|
||||
|
||||
clearTimeout(this.upgradeTimeoutTimer)
|
||||
this.upgradeTimeoutTimer = null
|
||||
|
||||
transport.removeListener("packet", onPacket)
|
||||
transport.removeListener("close", onTransportClose)
|
||||
transport.removeListener("error", onError)
|
||||
this.removeListener("close", onClose)
|
||||
}
|
||||
|
||||
const onError = (err: string) => {
|
||||
// debug("client did not complete upgrade - %s", err)
|
||||
cleanup()
|
||||
transport.close()
|
||||
transport = null
|
||||
}
|
||||
|
||||
const onTransportClose = () => {
|
||||
onError("transport closed")
|
||||
}
|
||||
|
||||
const onClose = () => {
|
||||
onError("socket closed")
|
||||
}
|
||||
|
||||
transport.on("packet", onPacket)
|
||||
transport.once("close", onTransportClose)
|
||||
transport.once("error", onError)
|
||||
|
||||
this.once("close", onClose)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears listeners and timers associated with current transport.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
clearTransport() {
|
||||
let cleanup: () => void
|
||||
|
||||
const toCleanUp = this.cleanupFn.length
|
||||
|
||||
for (let i = 0; i < toCleanUp; i++) {
|
||||
cleanup = this.cleanupFn.shift()
|
||||
cleanup()
|
||||
}
|
||||
|
||||
// silence further transport errors and prevent uncaught exceptions
|
||||
this.transport.on("error", function () {
|
||||
// debug("error triggered by discarded transport")
|
||||
})
|
||||
|
||||
// ensure transport won't stay open
|
||||
this.transport.close()
|
||||
|
||||
clearTimeout(this.pingTimeoutTimer)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport considered closed.
|
||||
* Possible reasons: `ping timeout`, `client error`, `parse error`,
|
||||
* `transport error`, `server close`, `transport close`
|
||||
*/
|
||||
onClose(reason: string, description?: string) {
|
||||
if ("closed" !== this.readyState) {
|
||||
this.readyState = "closed"
|
||||
|
||||
// clear timers
|
||||
clearTimeout(this.pingIntervalTimer)
|
||||
clearTimeout(this.pingTimeoutTimer)
|
||||
|
||||
clearInterval(this.checkIntervalTimer)
|
||||
this.checkIntervalTimer = null
|
||||
clearTimeout(this.upgradeTimeoutTimer)
|
||||
// clean writeBuffer in next tick, so developers can still
|
||||
// grab the writeBuffer on 'close' event
|
||||
process.nextTick(() => {
|
||||
this.writeBuffer = []
|
||||
})
|
||||
this.packetsFn = []
|
||||
this.sentCallbackFn = []
|
||||
this.clearTransport()
|
||||
this.emit("close", reason, description)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup and manage send callback
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
setupSendCallback() {
|
||||
// the message was sent successfully, execute the callback
|
||||
const onDrain = () => {
|
||||
if (this.sentCallbackFn.length > 0) {
|
||||
const seqFn = this.sentCallbackFn.splice(0, 1)[0]
|
||||
if ("function" === typeof seqFn) {
|
||||
// debug("executing send callback")
|
||||
seqFn(this.transport)
|
||||
} else if (Array.isArray(seqFn)) {
|
||||
// debug("executing batch send callback")
|
||||
const l = seqFn.length
|
||||
let i = 0
|
||||
for (; i < l; i++) {
|
||||
if ("function" === typeof seqFn[i]) {
|
||||
seqFn[i](this.transport)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.transport.on("drain", onDrain)
|
||||
|
||||
this.cleanupFn.push(() => {
|
||||
this.transport.removeListener("drain", onDrain)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message packet.
|
||||
*
|
||||
* @param {String} message
|
||||
* @param {Object} options
|
||||
* @param {Function} callback
|
||||
* @return {Socket} for chaining
|
||||
* @api public
|
||||
*/
|
||||
send(data: any, options: any, callback: any) {
|
||||
this.sendPacket("message", data, options, callback)
|
||||
return this
|
||||
}
|
||||
|
||||
write(data: any, options: any, callback?: any) {
|
||||
this.sendPacket("message", data, options, callback)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet.
|
||||
*
|
||||
* @param {String} packet type
|
||||
* @param {String} optional, data
|
||||
* @param {Object} options
|
||||
* @api private
|
||||
*/
|
||||
sendPacket(type: string, data?: string, options?: { compress?: any }, callback?: undefined) {
|
||||
if ("function" === typeof options) {
|
||||
callback = options
|
||||
options = null
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
options.compress = false !== options.compress
|
||||
|
||||
if ("closing" !== this.readyState && "closed" !== this.readyState) {
|
||||
// console.debug('sending packet "%s" (%s)', type, data)
|
||||
|
||||
const packet: any = {
|
||||
type: type,
|
||||
options: options
|
||||
}
|
||||
if (data) packet.data = data
|
||||
|
||||
// exports packetCreate event
|
||||
this.emit("packetCreate", packet)
|
||||
|
||||
this.writeBuffer.push(packet)
|
||||
|
||||
// add send callback to object, if defined
|
||||
if (callback) this.packetsFn.push(callback)
|
||||
|
||||
this.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to flush the packets buffer.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
flush() {
|
||||
if (
|
||||
"closed" !== this.readyState &&
|
||||
this.transport.writable &&
|
||||
this.writeBuffer.length
|
||||
) {
|
||||
console.trace("flushing buffer to transport")
|
||||
this.emit("flush", this.writeBuffer)
|
||||
this.server.emit("flush", this, this.writeBuffer)
|
||||
const wbuf = this.writeBuffer
|
||||
this.writeBuffer = []
|
||||
if (!this.transport.supportsFraming) {
|
||||
this.sentCallbackFn.push(this.packetsFn)
|
||||
} else {
|
||||
this.sentCallbackFn.push.apply(this.sentCallbackFn, this.packetsFn)
|
||||
}
|
||||
this.packetsFn = []
|
||||
this.transport.send(wbuf)
|
||||
this.emit("drain")
|
||||
this.server.emit("drain", this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available upgrades for this socket.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
getAvailableUpgrades() {
|
||||
const availableUpgrades = []
|
||||
const allUpgrades = this.server.upgrades(this.transport.name)
|
||||
let i = 0
|
||||
const l = allUpgrades.length
|
||||
for (; i < l; ++i) {
|
||||
const upg = allUpgrades[i]
|
||||
if (this.server.opts.transports.indexOf(upg) !== -1) {
|
||||
availableUpgrades.push(upg)
|
||||
}
|
||||
}
|
||||
return availableUpgrades
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the socket and underlying transport.
|
||||
*
|
||||
* @param {Boolean} optional, discard
|
||||
* @return {Socket} for chaining
|
||||
* @api public
|
||||
*/
|
||||
close(discard?: any) {
|
||||
if ("open" !== this.readyState) return
|
||||
|
||||
this.readyState = "closing"
|
||||
|
||||
if (this.writeBuffer.length) {
|
||||
this.once("drain", this.closeTransport.bind(this, discard))
|
||||
return
|
||||
}
|
||||
|
||||
this.closeTransport(discard)
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying transport.
|
||||
*
|
||||
* @param {Boolean} discard
|
||||
* @api private
|
||||
*/
|
||||
closeTransport(discard: any) {
|
||||
if (discard) this.transport.discard()
|
||||
this.transport.close(this.onClose.bind(this, "forced close"))
|
||||
}
|
||||
}
|
||||
121
packages/websocket/src/engine.io/transport.ts
Normal file
121
packages/websocket/src/engine.io/transport.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import parser_v4 from "../engine.io-parser"
|
||||
import type { WebSocketClient } from '../server/client'
|
||||
/**
|
||||
* Noop function.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function noop() { }
|
||||
|
||||
export abstract class Transport extends EventEmitter {
|
||||
public sid: string
|
||||
public req /**http.IncomingMessage */
|
||||
public socket: WebSocketClient
|
||||
public writable: boolean
|
||||
public readyState: string
|
||||
public discarded: boolean
|
||||
public protocol: Number
|
||||
public parser: any
|
||||
public perMessageDeflate: any
|
||||
public supportsBinary: boolean = false
|
||||
|
||||
/**
|
||||
* Transport constructor.
|
||||
*
|
||||
* @param {http.IncomingMessage} request
|
||||
* @api public
|
||||
*/
|
||||
constructor(req) {
|
||||
super()
|
||||
this.readyState = "open"
|
||||
this.discarded = false
|
||||
this.protocol = req._query.EIO === "4" ? 4 : 3 // 3rd revision by default
|
||||
this.parser = parser_v4//= this.protocol === 4 ? parser_v4 : parser_v3
|
||||
}
|
||||
|
||||
/**
|
||||
* Flags the transport as discarded.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
discard() {
|
||||
this.discarded = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with an incoming HTTP request.
|
||||
*
|
||||
* @param {http.IncomingMessage} request
|
||||
* @api private
|
||||
*/
|
||||
onRequest(req) {
|
||||
console.debug(`engine.io transport ${this.socket.id} setting request`, JSON.stringify(req))
|
||||
this.req = req
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the transport.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
close(fn?) {
|
||||
if ("closed" === this.readyState || "closing" === this.readyState) return
|
||||
|
||||
this.readyState = "closing"
|
||||
this.doClose(fn || noop)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with a transport error.
|
||||
*
|
||||
* @param {String} message error
|
||||
* @param {Object} error description
|
||||
* @api private
|
||||
*/
|
||||
onError(msg: string, desc?: string) {
|
||||
if (this.listeners("error").length) {
|
||||
const err: any = new Error(msg)
|
||||
err.type = "TransportError"
|
||||
err.description = desc
|
||||
this.emit("error", err)
|
||||
} else {
|
||||
console.debug(`ignored transport error ${msg} (${desc})`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with parsed out a packets from the data stream.
|
||||
*
|
||||
* @param {Object} packet
|
||||
* @api private
|
||||
*/
|
||||
onPacket(packet) {
|
||||
this.emit("packet", packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called with the encoded packet data.
|
||||
*
|
||||
* @param {String} data
|
||||
* @api private
|
||||
*/
|
||||
onData(data) {
|
||||
this.onPacket(this.parser.decodePacket(data))
|
||||
}
|
||||
|
||||
/**
|
||||
* Called upon transport close.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
onClose() {
|
||||
this.readyState = "closed"
|
||||
this.emit("close")
|
||||
}
|
||||
abstract get supportsFraming()
|
||||
abstract get name()
|
||||
abstract send(...args: any[])
|
||||
abstract doClose(d: Function)
|
||||
}
|
||||
3
packages/websocket/src/engine.io/transports/index.ts
Normal file
3
packages/websocket/src/engine.io/transports/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default {
|
||||
websocket: require("./websocket").WebSocket
|
||||
}
|
||||
116
packages/websocket/src/engine.io/transports/websocket.ts
Normal file
116
packages/websocket/src/engine.io/transports/websocket.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { Transport } from '../transport'
|
||||
// const debug = require("debug")("engine:ws")
|
||||
|
||||
export class WebSocket extends Transport {
|
||||
public perMessageDeflate: any
|
||||
|
||||
/**
|
||||
* WebSocket transport
|
||||
*
|
||||
* @param {http.IncomingMessage}
|
||||
* @api public
|
||||
*/
|
||||
constructor(req) {
|
||||
super(req)
|
||||
this.socket = req.websocket
|
||||
this.socket.on("message", this.onData.bind(this))
|
||||
this.socket.once("close", this.onClose.bind(this))
|
||||
this.socket.on("error", this.onError.bind(this))
|
||||
this.writable = true
|
||||
this.perMessageDeflate = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Transport name
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
get name() {
|
||||
return "websocket"
|
||||
}
|
||||
|
||||
/**
|
||||
* Advertise upgrade support.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
get handlesUpgrades() {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Advertise framing support.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
get supportsFraming() {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the incoming data.
|
||||
*
|
||||
* @param {String} encoded packet
|
||||
* @api private
|
||||
*/
|
||||
onData(data) {
|
||||
// debug('received "%s"', data)
|
||||
super.onData(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a packet payload.
|
||||
*
|
||||
* @param {Array} packets
|
||||
* @api private
|
||||
*/
|
||||
send(packets) {
|
||||
// console.log('WebSocket send packets', JSON.stringify(packets))
|
||||
const packet = packets.shift()
|
||||
if (typeof packet === "undefined") {
|
||||
this.writable = true
|
||||
this.emit("drain")
|
||||
return
|
||||
}
|
||||
|
||||
// always creates a new object since ws modifies it
|
||||
const opts: any = {}
|
||||
if (packet.options) {
|
||||
opts.compress = packet.options.compress
|
||||
}
|
||||
|
||||
const send = data => {
|
||||
if (this.perMessageDeflate) {
|
||||
const len =
|
||||
"string" === typeof data ? Buffer.byteLength(data) : data.length
|
||||
if (len < this.perMessageDeflate.threshold) {
|
||||
opts.compress = false
|
||||
}
|
||||
}
|
||||
console.trace('writing', data)
|
||||
this.writable = false
|
||||
|
||||
this.socket.send(data, opts, err => {
|
||||
if (err) return this.onError("write error", err.stack)
|
||||
this.send(packets)
|
||||
})
|
||||
}
|
||||
|
||||
if (packet.options && typeof packet.options.wsPreEncoded === "string") {
|
||||
send(packet.options.wsPreEncoded)
|
||||
} else {
|
||||
this.parser.encodePacket(packet, this.supportsBinary, send)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the transport.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
doClose(fn) {
|
||||
// debug("closing")
|
||||
this.socket.close()
|
||||
fn && fn()
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@javatypes/tomcat-websocket-api" />
|
||||
|
||||
import { Server, ServerOptions } from './socket-io'
|
||||
import { Server, ServerOptions } from './socket.io'
|
||||
|
||||
interface SocketIOStatic {
|
||||
/**
|
||||
@@ -44,7 +44,8 @@ let io: SocketStatic = function (pipeline: any, options: Partial<ServerOptions>)
|
||||
}
|
||||
io.Instance = Symbol("@ccms/websocket")
|
||||
export default io
|
||||
export * from './socket-io'
|
||||
export * from './socket.io'
|
||||
export * from './client'
|
||||
export * from './server'
|
||||
export * from './transport'
|
||||
export * from './engine.io/transport'
|
||||
export * as client from './socket.io-client'
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import { Transport } from '../transport'
|
||||
import { AttributeKeys } from './constants'
|
||||
|
||||
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
||||
|
||||
export class NettyClient extends Transport {
|
||||
private channel: any
|
||||
|
||||
constructor(server: any, channel: any) {
|
||||
super(server)
|
||||
this.remoteAddress = channel.remoteAddress() + ''
|
||||
this.request = channel.attr(AttributeKeys.Request).get()
|
||||
|
||||
this._id = channel.id() + ''
|
||||
this.channel = channel
|
||||
}
|
||||
|
||||
doSend(text: string) {
|
||||
this.channel.writeAndFlush(new TextWebSocketFrame(text))
|
||||
}
|
||||
doClose() {
|
||||
this.channel.close()
|
||||
}
|
||||
}
|
||||
@@ -1,69 +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 if (global.debug) {
|
||||
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 if (global.debug) {
|
||||
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 if (global.debug) {
|
||||
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,
|
||||
NettyClient
|
||||
}
|
||||
7
packages/websocket/src/server/client.ts
Normal file
7
packages/websocket/src/server/client.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { EventEmitter } from 'events'
|
||||
export abstract class WebSocketClient extends EventEmitter {
|
||||
public id: string
|
||||
public _socket: any
|
||||
abstract send(text: string, opts?: any, callback?: (err?: Error) => void)
|
||||
abstract close()
|
||||
}
|
||||
@@ -1,52 +1,87 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { ServerOptions } from '../socket.io'
|
||||
import { WebSocketClient } from './client'
|
||||
|
||||
import { Transport } from '../transport'
|
||||
import type { Request } from './request'
|
||||
|
||||
interface ServerOptions {
|
||||
export enum ServerEvent {
|
||||
detect = 'detect',
|
||||
request = 'request',
|
||||
upgrade = 'upgrade',
|
||||
connect = 'connect',
|
||||
connection = 'connection',
|
||||
message = 'message',
|
||||
error = 'error',
|
||||
disconnecting = 'disconnecting',
|
||||
disconnect = 'disconnect',
|
||||
}
|
||||
|
||||
export interface JavaServerOptions extends ServerOptions {
|
||||
event?: EventEmitter
|
||||
root?: string
|
||||
/**
|
||||
* name of the path to capture
|
||||
* @default "/socket.io"
|
||||
*/
|
||||
path: string
|
||||
}
|
||||
|
||||
interface WebSocketServerImpl extends EventEmitter {
|
||||
close(): void
|
||||
}
|
||||
|
||||
export class WebSocketServer extends EventEmitter {
|
||||
options: Partial<ServerOptions>
|
||||
private websocketServer: WebSocketServerImpl
|
||||
|
||||
constructor(instance: any, options: Partial<ServerOptions>) {
|
||||
export abstract class WebSocketServer extends EventEmitter {
|
||||
protected instance: any
|
||||
protected options: JavaServerOptions
|
||||
private clients: Map<string, WebSocketClient>
|
||||
constructor(instance: any, options: JavaServerOptions) {
|
||||
super()
|
||||
if (!instance) { throw new Error('instance can\'t be undefiend!') }
|
||||
this.options = Object.assign({
|
||||
event: new EventEmitter(),
|
||||
path: '/ws',
|
||||
root: root + '/wwwroot',
|
||||
}, options)
|
||||
this.selectServerImpl(instance)
|
||||
this.instance = instance
|
||||
this.options = options
|
||||
this.clients = new Map()
|
||||
console.debug('create websocket server from ' + this.constructor.name)
|
||||
this.initialize()
|
||||
}
|
||||
|
||||
on(event: "connect", cb: (transport: Transport) => void): this
|
||||
on(event: "message", cb: (transport: Transport, text: string) => void): this
|
||||
on(event: "disconnect", cb: (transport: Transport, reason: string) => void): this
|
||||
on(event: "error", cb: (transport: Transport, cause: Error) => void): this
|
||||
on(event: string, cb: (transport: Transport, extra?: any) => void): this {
|
||||
this.websocketServer.on(event, cb)
|
||||
return this
|
||||
protected onconnect(handler: any) {
|
||||
let id = this.getId(handler)
|
||||
console.log('client', id, 'connect')
|
||||
let request = this.getRequest(handler)
|
||||
request.id = id
|
||||
let websocket = this.getSocket(handler)
|
||||
this.clients.set(this.getId(handler), websocket)
|
||||
this.emit(ServerEvent.connect, request, websocket)
|
||||
}
|
||||
|
||||
private selectServerImpl(instance: any) {
|
||||
let WebSocketServerImpl = undefined
|
||||
if (instance.class.name.startsWith('io.netty.channel')) {
|
||||
WebSocketServerImpl = require("../netty").NettyWebSocketServer
|
||||
protected onmessage(handler: any, message: string) {
|
||||
this.execute(handler, (websocket) => websocket.emit(ServerEvent.message, message))
|
||||
}
|
||||
protected ondisconnect(handler: any, cause: string) {
|
||||
this.execute(handler, (websocket) => websocket.emit(ServerEvent.disconnect, cause))
|
||||
}
|
||||
protected onerror(handler: any, error: Error) {
|
||||
this.execute(handler, (websocket) => websocket.emit(ServerEvent.error, error))
|
||||
}
|
||||
protected execute(handler: any, callback: (websocket: WebSocketClient) => void) {
|
||||
let id = this.getId(handler)
|
||||
if (this.clients.has(id)) {
|
||||
this.clients.has(id) && callback(this.clients.get(id))
|
||||
} else {
|
||||
WebSocketServerImpl = require("../tomcat").TomcatWebSocketServer
|
||||
console.debug('ignore execute', handler, 'callback', callback)
|
||||
}
|
||||
this.websocketServer = new WebSocketServerImpl(instance, this.options)
|
||||
}
|
||||
public close() {
|
||||
this.clients.forEach(websocket => websocket.close())
|
||||
this.doClose()
|
||||
}
|
||||
protected abstract initialize(): void
|
||||
protected abstract getId(handler: any): string
|
||||
protected abstract getRequest(handler: any): Request
|
||||
protected abstract getSocket(handler: any): WebSocketClient
|
||||
protected abstract doClose(): void
|
||||
}
|
||||
|
||||
export const attach = (instance, options) => {
|
||||
if (!instance) { throw new Error('instance can\'t be undefiend!') }
|
||||
options = Object.assign({
|
||||
event: new EventEmitter(),
|
||||
path: '/ws',
|
||||
root: root + '/wwwroot',
|
||||
}, options)
|
||||
let WebSocketServerImpl = undefined
|
||||
if (instance.class.name.startsWith('io.netty.channel')) {
|
||||
WebSocketServerImpl = require("./netty").NettyWebSocketServer
|
||||
} else {
|
||||
WebSocketServerImpl = require("./tomcat").TomcatWebSocketServer
|
||||
}
|
||||
return new WebSocketServerImpl(instance, options)
|
||||
}
|
||||
|
||||
26
packages/websocket/src/server/netty/client.ts
Normal file
26
packages/websocket/src/server/netty/client.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { WebSocketClient } from '../client'
|
||||
|
||||
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
||||
|
||||
export class NettyClient extends WebSocketClient {
|
||||
private channel: any
|
||||
|
||||
constructor(channel: any) {
|
||||
super()
|
||||
this.id = channel.id() + ''
|
||||
this.channel = channel
|
||||
}
|
||||
|
||||
send(text: string, opts?: any, callback?: (err?: Error) => void) {
|
||||
try {
|
||||
this.channel.writeAndFlush(new TextWebSocketFrame(text))
|
||||
callback?.()
|
||||
} catch (error) {
|
||||
callback?.(error)
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this.channel.close()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { HttpRequestHandlerAdapter } from './adapter'
|
||||
import { AttributeKeys } from './constants'
|
||||
import { ServerOptions } from 'socket-io'
|
||||
|
||||
import type { JavaServerOptions } from '../'
|
||||
|
||||
const DefaultHttpResponse = Java.type('io.netty.handler.codec.http.DefaultHttpResponse')
|
||||
const DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse')
|
||||
@@ -18,7 +19,7 @@ const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener'
|
||||
export class HttpRequestHandler extends HttpRequestHandlerAdapter {
|
||||
private ws: string
|
||||
private root: string
|
||||
constructor(options: ServerOptions) {
|
||||
constructor(options: JavaServerOptions) {
|
||||
super()
|
||||
this.root = options.root
|
||||
this.ws = options.path
|
||||
76
packages/websocket/src/server/netty/index.ts
Normal file
76
packages/websocket/src/server/netty/index.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { ServerEvent, WebSocketServer } from '../'
|
||||
import { Request } from '../request'
|
||||
|
||||
import { NettyClient } from './client'
|
||||
import { AttributeKeys, Keys } from './constants'
|
||||
import { WebSocketDetect } from './websocket_detect'
|
||||
import { WebSocketHandler } from './websocket_handler'
|
||||
|
||||
import type { JavaServerOptions } from '../'
|
||||
|
||||
class NettyWebSocketServer extends WebSocketServer {
|
||||
constructor(pipeline: any, options: JavaServerOptions) {
|
||||
super(pipeline, options)
|
||||
}
|
||||
|
||||
protected initialize() {
|
||||
let connectEvent = this.options.event
|
||||
try { this.instance.remove(Keys.Detect) } catch (error) { }
|
||||
this.instance.addFirst(Keys.Detect, new WebSocketDetect(connectEvent).getHandler())
|
||||
connectEvent.on(ServerEvent.detect, (ctx, channel) => {
|
||||
channel.pipeline().addFirst(Keys.Handler, new WebSocketHandler(this.options).getHandler())
|
||||
ctx.fireChannelRead(channel)
|
||||
})
|
||||
connectEvent.on(ServerEvent.connect, (ctx) => {
|
||||
this.onconnect(ctx)
|
||||
})
|
||||
connectEvent.on(ServerEvent.message, (ctx, msg) => {
|
||||
this.onmessage(ctx, msg.text())
|
||||
})
|
||||
connectEvent.on(ServerEvent.disconnect, (ctx, cause) => {
|
||||
this.ondisconnect(ctx, cause)
|
||||
})
|
||||
connectEvent.on(ServerEvent.error, (ctx, error) => {
|
||||
this.onerror(ctx, error)
|
||||
})
|
||||
}
|
||||
|
||||
protected getId(ctx: any) {
|
||||
try {
|
||||
return ctx.channel().id() + ''
|
||||
} catch (error) {
|
||||
console.log(Object.toString.apply(ctx))
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
|
||||
protected getRequest(ctx) {
|
||||
let channel = ctx.channel()
|
||||
let req = channel.attr(AttributeKeys.Request).get()
|
||||
let headers = {}
|
||||
let nativeHeaders = req.headers()
|
||||
nativeHeaders.forEach(function (header) {
|
||||
headers[header.getKey()] = header.getValue()
|
||||
})
|
||||
let request = new Request(req.uri(), req.method().name(), headers)
|
||||
request.connection = {
|
||||
remoteAddress: channel.remoteAddress() + ''
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
protected getSocket(ctx) {
|
||||
return new NettyClient(ctx.channel())
|
||||
}
|
||||
|
||||
protected doClose() {
|
||||
if (this.instance.names().contains(Keys.Detect)) {
|
||||
this.instance.remove(Keys.Detect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
NettyWebSocketServer,
|
||||
NettyClient
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { ServerOptions } from '../socket-io'
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
import { ServerEvent } from '../'
|
||||
|
||||
import { TextWebSocketFrameHandlerAdapter } from './adapter'
|
||||
|
||||
import type { JavaServerOptions } from '../'
|
||||
|
||||
export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter {
|
||||
private event: EventEmitter
|
||||
constructor(options: ServerOptions) {
|
||||
constructor(options: JavaServerOptions) {
|
||||
super()
|
||||
this.event = options.event
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { WebSocketHandlerAdapter } from "./adapter"
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
|
||||
import { ServerEvent } from '../'
|
||||
|
||||
export class WebSocketDetect extends WebSocketHandlerAdapter {
|
||||
private event: EventEmitter
|
||||
@@ -1,11 +1,12 @@
|
||||
import { ServerOptions } from '../socket-io'
|
||||
import { ServerEvent } from '../socket-io/constants'
|
||||
import { ServerEvent } from '../'
|
||||
|
||||
import { Keys } from './constants'
|
||||
import { HttpRequestHandler } from './httprequest'
|
||||
import { WebSocketHandlerAdapter } from "./adapter"
|
||||
import { TextWebSocketFrameHandler } from './text_websocket_frame'
|
||||
|
||||
import type { JavaServerOptions } from '../'
|
||||
|
||||
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')
|
||||
@@ -13,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: ServerOptions
|
||||
constructor(options: ServerOptions) {
|
||||
private options: JavaServerOptions
|
||||
constructor(options: JavaServerOptions) {
|
||||
super()
|
||||
this.options = options
|
||||
}
|
||||
23
packages/websocket/src/server/request.ts
Normal file
23
packages/websocket/src/server/request.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { WebSocketClient } from "./client"
|
||||
interface HttpHeaders {
|
||||
[name: string]: string
|
||||
}
|
||||
interface Connection {
|
||||
remoteAddress: string
|
||||
}
|
||||
export class Request {
|
||||
public id: string
|
||||
public url: string
|
||||
public method: string
|
||||
public headers: HttpHeaders
|
||||
public connection: Connection
|
||||
public websocket: WebSocketClient
|
||||
|
||||
public _query: any
|
||||
|
||||
constructor(url: string, method = "GET", headers = {}) {
|
||||
this.url = url
|
||||
this.method = method
|
||||
this.headers = headers
|
||||
}
|
||||
}
|
||||
24
packages/websocket/src/server/tomcat/client.ts
Normal file
24
packages/websocket/src/server/tomcat/client.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { WebSocketClient } from '../client'
|
||||
|
||||
export class TomcatClient extends WebSocketClient {
|
||||
private session: javax.websocket.Session
|
||||
|
||||
constructor(session: javax.websocket.Session) {
|
||||
super()
|
||||
this.id = session.getId() + ''
|
||||
this.session = session
|
||||
}
|
||||
send(text: string, opts?: any, callback?: (err?: Error) => void) {
|
||||
Java.synchronized(() => {
|
||||
try {
|
||||
this.session.getBasicRemote().sendText(text)
|
||||
callback?.()
|
||||
} catch (error) {
|
||||
callback?.(error)
|
||||
}
|
||||
}, this.session)()
|
||||
}
|
||||
close() {
|
||||
this.session.close()
|
||||
}
|
||||
}
|
||||
75
packages/websocket/src/server/tomcat/index.ts
Normal file
75
packages/websocket/src/server/tomcat/index.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { JavaServerOptions, WebSocketServer } from '../'
|
||||
import { Request } from '../request'
|
||||
|
||||
import { TomcatClient } from './client'
|
||||
import { ProxyBeanName } from './constants'
|
||||
|
||||
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
|
||||
|
||||
type TomcatWebSocketSession = javax.websocket.Session
|
||||
|
||||
class TomcatWebSocketServer extends WebSocketServer {
|
||||
private executor: any
|
||||
|
||||
constructor(beanFactory: any, options: JavaServerOptions) {
|
||||
super(beanFactory, options)
|
||||
}
|
||||
|
||||
protected initialize(): void {
|
||||
this.initThreadPool()
|
||||
try { this.instance.destroySingleton(ProxyBeanName) } catch (error) { }
|
||||
let NashornWebSocketServerProxy = Java.extend(Java.type("pw.yumc.MiaoScript.websocket.WebSocketProxy"), {
|
||||
onOpen: (session: TomcatWebSocketSession) => {
|
||||
this.onconnect(session)
|
||||
},
|
||||
onMessage: (session: TomcatWebSocketSession, message: string) => {
|
||||
this.onmessage(session, message)
|
||||
},
|
||||
onClose: (session: TomcatWebSocketSession, reason: any) => {
|
||||
this.ondisconnect(session, reason)
|
||||
},
|
||||
onError: (session: TomcatWebSocketSession, error: Error) => {
|
||||
this.onerror(session, error)
|
||||
},
|
||||
})
|
||||
this.instance.registerSingleton(ProxyBeanName, new NashornWebSocketServerProxy())
|
||||
}
|
||||
|
||||
protected getId(session) {
|
||||
return session?.getId() + ''
|
||||
}
|
||||
|
||||
protected getRequest(session) {
|
||||
let request = new Request(session.getRequestURI(), "GET")
|
||||
request.connection = {
|
||||
remoteAddress: ''
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
protected getSocket(session) {
|
||||
return new TomcatClient(session)
|
||||
}
|
||||
|
||||
protected doClose() {
|
||||
this.instance.destroySingleton(ProxyBeanName)
|
||||
this.executor.shutdown()
|
||||
}
|
||||
|
||||
private initThreadPool() {
|
||||
const ThreadPoolTaskExecutor = Java.type('org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor')
|
||||
this.executor = new ThreadPoolTaskExecutor()
|
||||
this.executor.setCorePoolSize(10)
|
||||
this.executor.setMaxPoolSize(100)
|
||||
this.executor.setQueueCapacity(500)
|
||||
this.executor.setKeepAliveSeconds(60)
|
||||
this.executor.setThreadNamePrefix("@ccms/websocket-")
|
||||
this.executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
||||
this.executor.initialize()
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
TomcatWebSocketServer,
|
||||
TomcatClient
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { Namespace } from './namespace'
|
||||
import { Parser } from './parser'
|
||||
import { Socket } from './socket'
|
||||
|
||||
export type SocketId = string
|
||||
export type Room = string
|
||||
|
||||
export interface BroadcastFlags {
|
||||
volatile?: boolean
|
||||
compress?: boolean
|
||||
local?: boolean
|
||||
broadcast?: boolean
|
||||
binary?: boolean
|
||||
}
|
||||
|
||||
export interface BroadcastOptions {
|
||||
rooms: Set<Room>
|
||||
except?: Set<SocketId>
|
||||
flags?: BroadcastFlags
|
||||
}
|
||||
|
||||
export class Adapter extends EventEmitter implements Adapter {
|
||||
rooms: Map<Room, Set<SocketId>>
|
||||
sids: Map<SocketId, Set<Room>>
|
||||
private readonly encoder: Parser
|
||||
parser: Parser
|
||||
|
||||
constructor(readonly nsp: Namespace) {
|
||||
super()
|
||||
this.rooms = new Map()
|
||||
this.sids = new Map()
|
||||
this.parser = nsp.server._parser
|
||||
this.encoder = this.parser
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a socket to a list of room.
|
||||
*
|
||||
* @param {String} socket id
|
||||
* @param {String} rooms
|
||||
* @param {Function} callback
|
||||
* @api public
|
||||
*/
|
||||
addAll(id: SocketId, rooms: Set<Room>): Promise<void> | void {
|
||||
for (const room of rooms) {
|
||||
if (!this.sids.has(id)) {
|
||||
this.sids.set(id, new Set())
|
||||
}
|
||||
this.sids.get(id).add(room)
|
||||
|
||||
if (!this.rooms.has(room)) {
|
||||
this.rooms.set(room, new Set())
|
||||
}
|
||||
this.rooms.get(room).add(id)
|
||||
}
|
||||
}
|
||||
del(id: string, room: string, callback?: (err?: any) => void): void {
|
||||
if (this.sids.has(id)) {
|
||||
this.sids.get(id).delete(room)
|
||||
}
|
||||
|
||||
if (this.rooms.has(room)) {
|
||||
this.rooms.get(room).delete(id)
|
||||
if (this.rooms.get(room).size === 0) this.rooms.delete(room)
|
||||
}
|
||||
callback && callback.bind(null, null)
|
||||
}
|
||||
delAll(id: string): void {
|
||||
if (!this.sids.has(id)) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const room of this.sids.get(id)) {
|
||||
if (this.rooms.has(room)) {
|
||||
this.rooms.get(room).delete(id)
|
||||
if (this.rooms.get(room).size === 0) this.rooms.delete(room)
|
||||
}
|
||||
}
|
||||
|
||||
this.sids.delete(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcasts a packet.
|
||||
*
|
||||
* Options:
|
||||
* - `flags` {Object} flags for this packet
|
||||
* - `except` {Array} sids that should be excluded
|
||||
* - `rooms` {Array} list of rooms to broadcast to
|
||||
*
|
||||
* @param {Object} packet the packet object
|
||||
* @param {Object} opts the options
|
||||
* @public
|
||||
*/
|
||||
public broadcast(packet: any, opts: BroadcastOptions): void {
|
||||
const rooms = opts.rooms
|
||||
const except = opts.except || new Set()
|
||||
const flags = opts.flags || {}
|
||||
const packetOpts = {
|
||||
preEncoded: true,
|
||||
volatile: flags.volatile,
|
||||
compress: flags.compress
|
||||
}
|
||||
const ids = new Set()
|
||||
|
||||
packet.nsp = this.nsp.name
|
||||
const encodedPackets = this.encoder.encode(packet)
|
||||
|
||||
if (rooms.size) {
|
||||
for (const room of rooms) {
|
||||
if (!this.rooms.has(room)) continue
|
||||
|
||||
for (const id of this.rooms.get(room)) {
|
||||
if (ids.has(id) || except.has(id)) continue
|
||||
const socket = this.nsp.sockets.get(id)
|
||||
if (socket) {
|
||||
socket.packet(encodedPackets as any, packetOpts)
|
||||
ids.add(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const [id] of this.sids) {
|
||||
if (except.has(id)) continue
|
||||
const socket = this.nsp.sockets.get(id)
|
||||
if (socket) socket.packet(encodedPackets as any, packetOpts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of sockets by sid.
|
||||
*
|
||||
* @param {Set<Room>} rooms the explicit set of rooms to check.
|
||||
*/
|
||||
public sockets(rooms: Set<Room>): Promise<Set<SocketId>> {
|
||||
const sids = new Set<SocketId>()
|
||||
if (rooms.size) {
|
||||
for (const room of rooms) {
|
||||
if (!this.rooms.has(room)) continue
|
||||
for (const id of this.rooms.get(room)) {
|
||||
if (this.nsp.sockets.has(id)) {
|
||||
sids.add(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const [id] of this.sids) {
|
||||
if (this.nsp.sockets.has(id)) sids.add(id)
|
||||
}
|
||||
}
|
||||
return Promise.resolve(sids)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of rooms a given socket has joined.
|
||||
*
|
||||
* @param {SocketId} id the socket id
|
||||
*/
|
||||
public socketRooms(id: SocketId): Set<Room> | undefined {
|
||||
return this.sids.get(id)
|
||||
}
|
||||
}
|
||||
@@ -1,360 +0,0 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { Parser } from './parser'
|
||||
import { Packet } from './packet'
|
||||
import { Namespace, Server, Socket } from './index'
|
||||
import { PacketTypes, SubPacketTypes } from './types'
|
||||
import { ServerEvent } from './constants'
|
||||
import { SocketId } from './adapter'
|
||||
import { Transport } from '../transport'
|
||||
|
||||
const parser = new Parser()
|
||||
|
||||
export class Client extends EventEmitter {
|
||||
public readonly conn: Transport
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
readonly id: string
|
||||
private readonly server: Server
|
||||
// private readonly encoder: Encoder
|
||||
private readonly decoder: any
|
||||
private sockets: Map<SocketId, Socket>
|
||||
private nsps: Map<string, Socket>
|
||||
private connectTimeout: NodeJS.Timeout
|
||||
|
||||
private checkIntervalTimer: NodeJS.Timeout
|
||||
private upgradeTimeoutTimer: NodeJS.Timeout
|
||||
private pingTimeoutTimer: NodeJS.Timeout
|
||||
private pingIntervalTimer: NodeJS.Timeout
|
||||
|
||||
constructor(server: Server, conn) {
|
||||
super()
|
||||
this.server = server
|
||||
this.conn = conn
|
||||
// this.encoder = server.encoder
|
||||
this.decoder = server._parser
|
||||
this.id = this.conn.id + ''
|
||||
this.setup()
|
||||
// =============================
|
||||
this.sockets = new Map()
|
||||
this.nsps = new Map()
|
||||
// ================== engine.io
|
||||
this.onOpen()
|
||||
// ================== Transport
|
||||
this.conn.on(ServerEvent.disconnect, (reason) => {
|
||||
this.onclose(reason)
|
||||
})
|
||||
}
|
||||
/**
|
||||
* @return the reference to the request that originated the Engine.IO connection
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
public get request(): any /**IncomingMessage */ {
|
||||
return this.conn.request
|
||||
}
|
||||
/**
|
||||
* Sets up event listeners.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private setup() {
|
||||
// @ts-ignore
|
||||
// this.decoder.on("decoded", this.ondecoded)
|
||||
this.conn.on("data", this.ondata.bind(this))
|
||||
this.conn.on("error", this.onerror.bind(this))
|
||||
this.conn.on("close", this.onclose.bind(this))
|
||||
console.debug(`setup client ${this.id}`)
|
||||
this.connectTimeout = setTimeout(() => {
|
||||
if (this.nsps.size === 0) {
|
||||
console.debug("no namespace joined yet, close the client")
|
||||
this.close()
|
||||
} else {
|
||||
console.debug("the client has already joined a namespace, nothing to do")
|
||||
}
|
||||
}, this.server._connectTimeout)
|
||||
}
|
||||
/**
|
||||
* Connects a client to a namespace.
|
||||
*
|
||||
* @param {String} name - the namespace
|
||||
* @param {Object} auth - the auth parameters
|
||||
* @private
|
||||
*/
|
||||
private connect(name: string, auth: object = {}) {
|
||||
console.debug(`client ${this.id} connecting to namespace ${name} has: ${this.server._nsps.has(name)}`)
|
||||
if (this.server._nsps.has(name)) {
|
||||
return this.doConnect(name, auth)
|
||||
}
|
||||
|
||||
this.server._checkNamespace(name, auth, (dynamicNsp: Namespace) => {
|
||||
if (dynamicNsp) {
|
||||
console.debug(`dynamic namespace ${dynamicNsp.name} was created`)
|
||||
this.doConnect(name, auth)
|
||||
} else {
|
||||
console.debug(`creation of namespace ${name} was denied`)
|
||||
this._packet({
|
||||
type: PacketTypes.MESSAGE,
|
||||
sub_type: SubPacketTypes.ERROR,
|
||||
nsp: name,
|
||||
data: {
|
||||
message: "Invalid namespace"
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
doConnect(name, auth: object) {
|
||||
if (this.connectTimeout) {
|
||||
clearTimeout(this.connectTimeout)
|
||||
this.connectTimeout = null
|
||||
}
|
||||
const nsp = this.server.of(name)
|
||||
|
||||
nsp._add(this, auth, (socket: Socket) => {
|
||||
this.sockets.set(socket.id, socket)
|
||||
this.nsps.set(nsp.name, socket)
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Disconnects from all namespaces and closes transport.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_disconnect() {
|
||||
for (const socket of this.sockets.values()) {
|
||||
socket.disconnect()
|
||||
}
|
||||
this.sockets.clear()
|
||||
this.close()
|
||||
}
|
||||
/**
|
||||
* Removes a socket. Called by each `Socket`.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_remove(socket: Socket) {
|
||||
if (this.sockets.has(socket.id)) {
|
||||
this.sockets.delete(socket.id)
|
||||
this.nsps.delete(socket.nsp.name)
|
||||
} else {
|
||||
console.debug(`ignoring remove for ${socket.id}`,)
|
||||
}
|
||||
process.nextTick(() => {
|
||||
if (this.sockets.size == 0) {
|
||||
this.onclose('no live socket')
|
||||
}
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Closes the underlying connection.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private close() {
|
||||
console.debug(`client ${this.id} close`)
|
||||
if ("open" == this.conn.readyState) {
|
||||
console.debug("forcing transport close")
|
||||
this.onclose("forced server close")
|
||||
this.conn.close()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Writes a packet to the transport.
|
||||
*
|
||||
* @param {Object} packet object
|
||||
* @param {Object} opts
|
||||
* @private
|
||||
*/
|
||||
_packet(packet: Packet, opts = { preEncoded: false }) {
|
||||
// opts = opts || {}
|
||||
// const self = this
|
||||
|
||||
// // this writes to the actual connection
|
||||
// function writeToEngine(encodedPackets) {
|
||||
// if (opts.volatile && !self.conn.transport.writable) return
|
||||
// for (let i = 0; i < encodedPackets.length; i++) {
|
||||
// self.conn.write(encodedPackets[i], { compress: opts.compress })
|
||||
// }
|
||||
// }
|
||||
|
||||
// if ("open" == this.conn.readyState) {
|
||||
// debug("writing packet %j", packet)
|
||||
// if (!opts.preEncoded) {
|
||||
// // not broadcasting, need to encode
|
||||
// writeToEngine(this.encoder.encode(packet)) // encode, then write results to engine
|
||||
// } else {
|
||||
// // a broadcast pre-encodes a packet
|
||||
// writeToEngine(packet)
|
||||
// }
|
||||
// } else {
|
||||
// debug("ignoring packet write %j", packet)
|
||||
// }
|
||||
if ("open" == this.conn.readyState) {
|
||||
this.conn.send(opts.preEncoded ? packet as unknown as string : parser.encode(packet))
|
||||
} else {
|
||||
console.debug(`ignoring write packet ${JSON.stringify(packet)} to client ${this.id} is already close!`)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called with incoming transport data.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private ondata(data) {
|
||||
// try/catch is needed for protocol violations (GH-1880)
|
||||
try {
|
||||
this.decoder.add(data)
|
||||
} catch (e) {
|
||||
this.onerror(e)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called when parser fully decodes a packet.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
ondecoded(packet: Packet) {
|
||||
if (SubPacketTypes.CONNECT == packet.sub_type) {
|
||||
this.connect(packet.nsp, packet.data)
|
||||
} else {
|
||||
process.nextTick(() => {
|
||||
const socket = this.nsps.get(packet.nsp)
|
||||
if (socket) {
|
||||
socket._onpacket(packet)
|
||||
} else {
|
||||
console.debug(`client ${this.id} no socket for namespace ${packet.nsp}.`)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Handles an error.
|
||||
*
|
||||
* @param {Object} err object
|
||||
* @private
|
||||
*/
|
||||
private onerror(err) {
|
||||
for (const socket of this.sockets.values()) {
|
||||
socket._onerror(err)
|
||||
}
|
||||
this.conn.close()
|
||||
}
|
||||
onclose(reason?: string) {
|
||||
this.conn.readyState = "closing"
|
||||
// ======= engine.io
|
||||
this.onClose(reason)
|
||||
// cleanup connectTimeout
|
||||
if (this.connectTimeout) {
|
||||
clearTimeout(this.connectTimeout)
|
||||
this.connectTimeout = null
|
||||
}
|
||||
console.debug(`client ${this.id} close with reason ${reason}`)
|
||||
// ignore a potential subsequent `close` event
|
||||
// `nsps` and `sockets` are cleaned up seamlessly
|
||||
for (const socket of this.sockets.values()) {
|
||||
socket._onclose(reason)
|
||||
}
|
||||
this.sockets.clear()
|
||||
// this.decoder.destroy(); // clean up decoder
|
||||
}
|
||||
|
||||
destroy() {
|
||||
// this.conn.removeListener('data', this.ondata);
|
||||
// this.conn.removeListener('error', this.onerror);
|
||||
// this.conn.removeListener('close', this.onclose);
|
||||
// this.decoder.removeListener('decoded', this.ondecoded);
|
||||
}
|
||||
|
||||
//================== engine.io
|
||||
onOpen() {
|
||||
this.conn.readyState = "open"
|
||||
this._packet({
|
||||
type: PacketTypes.OPEN,
|
||||
data: {
|
||||
sid: this.id,
|
||||
upgrades: [],
|
||||
pingInterval: this.server.options.pingInterval,
|
||||
pingTimeout: this.server.options.pingTimeout
|
||||
}
|
||||
})
|
||||
this.schedulePing()
|
||||
}
|
||||
|
||||
onPacket(packet: Packet) {
|
||||
if ("open" === this.conn.readyState) {
|
||||
// export packet event
|
||||
// debug("packet")
|
||||
// this.emit("packet", packet)
|
||||
|
||||
// Reset ping timeout on any packet, incoming data is a good sign of
|
||||
// other side's liveness
|
||||
this.resetPingTimeout(this.server.options.pingInterval + this.server.options.pingTimeout * 2)
|
||||
switch (packet.type) {
|
||||
case PacketTypes.PING:
|
||||
this._packet({
|
||||
type: PacketTypes.PONG,
|
||||
data: packet.data
|
||||
})
|
||||
break
|
||||
case PacketTypes.PONG:
|
||||
this.schedulePing()
|
||||
break
|
||||
case PacketTypes.UPGRADE:
|
||||
break
|
||||
case PacketTypes.MESSAGE:
|
||||
this.ondecoded(packet)
|
||||
break
|
||||
case PacketTypes.CLOSE:
|
||||
this.onclose()
|
||||
break
|
||||
default:
|
||||
console.log(`client ${this.id} reciver unknow packet type: ${packet.type}`)
|
||||
}
|
||||
} else {
|
||||
console.debug(`packet received with closed client ${this.id}`)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called upon transport considered closed.
|
||||
* Possible reasons: `ping timeout`, `client error`, `parse error`,
|
||||
* `transport error`, `server close`, `transport close`
|
||||
*/
|
||||
onClose(reason, description?: string) {
|
||||
// if ("closed" !== this.conn.readyState) {
|
||||
clearTimeout(this.pingIntervalTimer)
|
||||
clearTimeout(this.pingTimeoutTimer)
|
||||
|
||||
clearInterval(this.checkIntervalTimer)
|
||||
this.checkIntervalTimer = null
|
||||
clearTimeout(this.upgradeTimeoutTimer)
|
||||
// this.emit("close", reason, description)
|
||||
// }
|
||||
}
|
||||
/**
|
||||
* Pings client every `this.pingInterval` and expects response
|
||||
* within `this.pingTimeout` or closes connection.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
schedulePing() {
|
||||
clearTimeout(this.pingIntervalTimer)
|
||||
this.pingIntervalTimer = setTimeout(() => {
|
||||
this.resetPingTimeout(this.server.options.pingTimeout)
|
||||
process.nextTick(() => this._packet({ type: PacketTypes.PING }))
|
||||
}, this.server.options.pingInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets ping timeout.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
resetPingTimeout(timeout: number) {
|
||||
clearTimeout(this.pingTimeoutTimer)
|
||||
this.pingTimeoutTimer = setTimeout(() => {
|
||||
if (this.conn.readyState === "closed") return
|
||||
this.onclose("ping timeout")
|
||||
}, timeout)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
export enum ServerEvent {
|
||||
detect = 'detect',
|
||||
connect = 'connect',
|
||||
connection = 'connection',
|
||||
message = 'message',
|
||||
error = 'error',
|
||||
disconnecting = 'disconnecting',
|
||||
disconnect = 'disconnect',
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user