oox 0.2.0 → 0.3.0-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +32 -29
  3. package/app.js +142 -0
  4. package/bin/argv.js +70 -95
  5. package/bin/cli.js +43 -58
  6. package/bin/configurer.js +49 -78
  7. package/bin/proxyer.js +61 -95
  8. package/bin/register.js +54 -105
  9. package/bin/starter.js +77 -144
  10. package/index.js +149 -6
  11. package/index.mjs +4 -0
  12. package/modules/http/index.js +180 -0
  13. package/modules/http/utils.js +73 -0
  14. package/modules/index.js +88 -0
  15. package/modules/module.js +16 -0
  16. package/modules/socketio/client.js +101 -0
  17. package/modules/socketio/index.js +148 -0
  18. package/modules/socketio/server.js +130 -0
  19. package/modules/socketio/socket.js +4 -0
  20. package/package.json +34 -16
  21. package/types/app.d.ts +37 -0
  22. package/types/bin/argv.d.ts +8 -0
  23. package/types/bin/cli.d.ts +2 -0
  24. package/types/bin/configurer.d.ts +1 -0
  25. package/types/bin/proxyer.d.ts +1 -0
  26. package/types/bin/register.d.ts +1 -0
  27. package/types/bin/starter.d.ts +1 -0
  28. package/types/index.d.ts +70 -0
  29. package/types/modules/http/index.d.ts +47 -0
  30. package/types/modules/http/utils.d.ts +17 -0
  31. package/types/modules/index.d.ts +23 -0
  32. package/types/modules/module.d.ts +11 -0
  33. package/types/modules/socketio/client.d.ts +23 -0
  34. package/types/modules/socketio/index.d.ts +35 -0
  35. package/types/modules/socketio/server.d.ts +35 -0
  36. package/types/modules/socketio/socket.d.ts +11 -0
  37. package/types/utils.d.ts +6 -0
  38. package/utils.js +63 -0
  39. package/.gitattributes +0 -2
  40. package/global.js +0 -118
  41. package/middleware.js +0 -167
  42. package/rpc/config.class.js +0 -48
  43. package/rpc/context.class.js +0 -15
  44. package/rpc/http.class.js +0 -312
  45. package/rpc/rpc.class.js +0 -231
  46. package/rpc/rpc.interface.class.js +0 -119
  47. package/rpc/socketio.class.js +0 -223
  48. package/service/service.class.js +0 -74
  49. package/service/socketio.class.js +0 -145
  50. package/setMap.class.js +0 -67
  51. package/socketio/client.class.js +0 -190
  52. package/socketio/server.class.js +0 -222
  53. package/socketio/socket.class.js +0 -23
  54. package/util.js +0 -224
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const client_1 = require("./client");
4
+ const oox = require("../../index");
5
+ const index_1 = require("../../index");
6
+ const socket_1 = require("./socket");
7
+ class SocketIOModule extends client_1.default {
8
+ sockets = socket_1.sockets;
9
+ onSyncConnection(socket) {
10
+ const mSockets = Array.from(socket_1.sockets.values())
11
+ .filter(s => s !== socket &&
12
+ s.data.name !== socket.data.name &&
13
+ s.data.id.startsWith('ws://'));
14
+ return mSockets.map(s => s.data);
15
+ }
16
+ serverOnDisconnect(socket, reason) {
17
+ super.serverOnDisconnect(socket, reason);
18
+ (0, index_1.removeKeepAliveConnection)(socket.data.name, socket.data.id);
19
+ }
20
+ clientOnDisconnect(socket, reason) {
21
+ super.clientOnDisconnect(socket, reason);
22
+ (0, index_1.removeKeepAliveConnection)(socket.data.name, socket.data.id);
23
+ }
24
+ /**
25
+ *
26
+ * @param socket 是由哪个通道发送过来的
27
+ * @param connectionDatas
28
+ */
29
+ clientOnSyncConnection(socket, connectionDatas) {
30
+ for (const data of connectionDatas)
31
+ if (!socket_1.sockets.has(data.id))
32
+ this.connect(data.id).catch((error) => console.error(error));
33
+ }
34
+ onFetchActions(socket, search) {
35
+ const data = [];
36
+ for (const key of oox.kvMethods.keys())
37
+ if (!key.endsWith('_proxy') && key.includes(search))
38
+ data.push(key);
39
+ return data;
40
+ }
41
+ fetchActions(id, search = '') {
42
+ let socket = socket_1.sockets.get(id);
43
+ if (!socket) {
44
+ const connections = (0, index_1.getKeepAliveConnections)(id);
45
+ if (!connections || !connections.size)
46
+ throw new Error(`Unknown service identify<${id}>`);
47
+ id = connections.keys().next().value;
48
+ socket = socket_1.sockets.get(id);
49
+ }
50
+ if (!socket)
51
+ throw new Error(`Unknown service identify<${id}>`);
52
+ return this.emit(socket.data.id, 'fetchActions', [search]);
53
+ }
54
+ onConnection(socket) {
55
+ const { id, name, host } = socket.data;
56
+ const connection = new index_1.RPCKeepAliveConnection(this, id, socket.data);
57
+ (0, index_1.addKeepAliveConnection)(connection);
58
+ const connectionContext = {
59
+ sourceIP: '',
60
+ ip: host,
61
+ caller: name,
62
+ callerId: id,
63
+ connection
64
+ };
65
+ socket.on('fetchActions', async (search, fn) => {
66
+ if ('function' !== typeof fn)
67
+ return;
68
+ const data = await this.onFetchActions(socket, search);
69
+ fn(data);
70
+ });
71
+ socket.on('call', async (action, params, context, callback) => {
72
+ if ('object' !== typeof context)
73
+ context = oox.genContext(connectionContext);
74
+ else
75
+ context = oox.genContext(Object.assign(context, connectionContext));
76
+ this.call(action, params, context, callback);
77
+ });
78
+ }
79
+ /**
80
+ *
81
+ * @param {Socket} socket
82
+ */
83
+ serverOnConnection(socket) {
84
+ super.serverOnConnection(socket);
85
+ socket.setMaxListeners(0);
86
+ socket.on('syncConnection', async (fn) => {
87
+ if ('function' !== typeof fn)
88
+ return;
89
+ const data = this.onSyncConnection(socket);
90
+ fn(data);
91
+ });
92
+ this.onConnection(socket);
93
+ }
94
+ async call(action, params, context, callback) {
95
+ const returns = await oox.call(action, params, context);
96
+ 'function' === typeof callback && callback(returns);
97
+ return returns;
98
+ }
99
+ clientOnConnection(socket) {
100
+ super.clientOnConnection(socket);
101
+ socket.emit('syncConnection', (socketDatas) => this.clientOnSyncConnection(socket, socketDatas));
102
+ this.onConnection(socket);
103
+ }
104
+ /**
105
+ * socketio emit
106
+ */
107
+ async emit(url, action, params) {
108
+ let socket = null;
109
+ try {
110
+ socket = await this.connect(url);
111
+ }
112
+ catch (error) {
113
+ // try again
114
+ socket = await this.connect(url);
115
+ }
116
+ try {
117
+ return await new Promise((resolve, reject) => {
118
+ const onError = (reason) => {
119
+ const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
120
+ reject(new Error(message));
121
+ };
122
+ // RPC 执行时中断连接
123
+ socket.once('disconnect', onError);
124
+ socket.emit(action, ...params, (returns) => {
125
+ socket.off('disconnect', onError);
126
+ resolve(returns);
127
+ });
128
+ });
129
+ }
130
+ catch (error) {
131
+ throw new Error(error.message);
132
+ }
133
+ }
134
+ /**
135
+ * RPC
136
+ */
137
+ async rpc(url, action, params, context) {
138
+ if (!context || !context.traceId) {
139
+ context = oox.getContext();
140
+ }
141
+ const { error, body } = await this.emit(url, 'call', [action, params, context]);
142
+ if (error)
143
+ throw new Error(error.message);
144
+ else
145
+ return body;
146
+ }
147
+ }
148
+ exports.default = SocketIOModule;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SocketIOConfig = void 0;
4
+ const http = require("http");
5
+ const socket_io_1 = require("socket.io");
6
+ const module_1 = require("../module");
7
+ const socket_1 = require("./socket");
8
+ const oox = require("../../index");
9
+ class SocketIOConfig extends module_1.ModuleConfig {
10
+ // listen port
11
+ port = 0;
12
+ // service path
13
+ path = '/socket.io';
14
+ // browser cross origin
15
+ origin = '';
16
+ }
17
+ exports.SocketIOConfig = SocketIOConfig;
18
+ class SocketIOServer extends module_1.default {
19
+ name = 'oox:socketio';
20
+ config = new SocketIOConfig;
21
+ /**
22
+ * means this.server created by myself<SocketIOServer>
23
+ */
24
+ #isSelfServer = false;
25
+ server = null;
26
+ socketServer = null;
27
+ setConfig(config) {
28
+ Object.assign(this.config, config);
29
+ }
30
+ getConfig() {
31
+ return this.config;
32
+ }
33
+ async serve() {
34
+ await this.stop();
35
+ const port = this.config.port;
36
+ const isSelfServer = this.#isSelfServer = this.server ? true : false;
37
+ const server = this.server = isSelfServer ? this.server :
38
+ http.createServer((request, response) => response.end('No HTTP Gateway'));
39
+ if (!server.listening)
40
+ server.listen(port);
41
+ const address = server.address();
42
+ if (!address || 'object' !== typeof address)
43
+ throw new Error('Cannot read socket.io server port');
44
+ this.config.port = address.port;
45
+ this.createSocketIOServer();
46
+ }
47
+ async stop() {
48
+ if (this.socketServer)
49
+ await new Promise((resolve, reject) => this.socketServer.close(error => error ? reject(error) : resolve()));
50
+ if (this.#isSelfServer)
51
+ await new Promise((resolve, reject) => this.server.close(error => error ? reject(error) : resolve()));
52
+ }
53
+ genSocketIOServerOptions() {
54
+ const options = {
55
+ /**
56
+ * name of the path to capture
57
+ * @default "/socket.io"
58
+ */
59
+ path: this.config.path,
60
+ /**
61
+ * how many ms before a client without namespace is closed
62
+ * @default 45000
63
+ */
64
+ connectTimeout: 5000,
65
+ /**
66
+ * how many ms without a pong packet to consider the connection closed
67
+ * @default 5000
68
+ */
69
+ pingTimeout: 2000,
70
+ /**
71
+ * how many ms before sending a new ping packet
72
+ * @default 25000
73
+ */
74
+ pingInterval: 10000,
75
+ /**
76
+ * how many bytes or characters a message can be, before closing the session (to avoid DoS).
77
+ * @default 1e5 (100 KB)
78
+ */
79
+ maxHttpBufferSize: 1e5
80
+ };
81
+ const { origin } = this.config;
82
+ if (origin)
83
+ options.cors = { origin };
84
+ return options;
85
+ }
86
+ createSocketIOServer() {
87
+ const socketServer = this.socketServer = new socket_io_1.Server(this.server, this.genSocketIOServerOptions());
88
+ socketServer.on('connection', async (socket) => {
89
+ try {
90
+ this.serverOnSocketConnection(socket);
91
+ }
92
+ catch (error) {
93
+ socket.send(error.message).disconnect(true);
94
+ }
95
+ });
96
+ }
97
+ /**
98
+ * 服务端Socket连接事件
99
+ */
100
+ serverOnSocketConnection(socket) {
101
+ const headers = socket.handshake.headers;
102
+ const callerId = String(headers['x-caller-id'] || '') || socket.id;
103
+ // 已经存在相同的连接
104
+ if (socket_1.sockets.has(callerId))
105
+ throw new Error('Connection Exists');
106
+ // client ip or caller service ip
107
+ const ip = String(headers['x-real-ip'] || headers['x-ip'] || socket.handshake.address);
108
+ // service name
109
+ const caller = String(headers['x-caller'] || 'anonymous');
110
+ socket.data = { connected: true, host: ip, name: caller, id: callerId };
111
+ // 保存 callerId 与 socket 对应关系
112
+ socket_1.sockets.set(callerId, socket);
113
+ socket.on('disconnect', reason => this.serverOnSocketDisconnect(socket, reason));
114
+ socket.emit('oox_connected', { name: oox.config.name });
115
+ this.serverOnConnection(socket);
116
+ }
117
+ serverOnConnection(socket) { }
118
+ /**
119
+ * 服务端Socket断开事件
120
+ * @param {Socket} socket
121
+ * @param {Error} reason
122
+ */
123
+ serverOnSocketDisconnect(socket, reason) {
124
+ socket.data.connected = false;
125
+ socket_1.sockets.delete(socket.data.id);
126
+ this.serverOnDisconnect(socket, reason);
127
+ }
128
+ serverOnDisconnect(socket, reason) { }
129
+ }
130
+ exports.default = SocketIOServer;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sockets = void 0;
4
+ exports.sockets = new Map();
package/package.json CHANGED
@@ -1,17 +1,35 @@
1
1
  {
2
- "name": "oox",
3
- "version": "0.2.0",
4
- "description": "distributed api service",
5
- "main": "index.js",
6
- "bin": {
7
- "oox": "bin/cli.js"
8
- },
9
- "author": "lipingruan",
10
- "homepage": "https://github.com/lipingruan/oox",
11
- "license": "MIT",
12
- "dependencies": {
13
- "chalk": "^4.1.0",
14
- "socket.io": "^4.4.0",
15
- "socket.io-client": "^4.4.0"
16
- }
17
- }
2
+ "name": "oox",
3
+ "version": "0.3.0-beta1",
4
+ "description": "Graceful NodeJS distributed application solution",
5
+ "keywords": [
6
+ "http",
7
+ "socket.io",
8
+ "rpc",
9
+ "microservice",
10
+ "distributed",
11
+ "tracing"
12
+ ],
13
+ "type": "commonjs",
14
+ "main": "index.js",
15
+ "types": "types/index.d.ts",
16
+ "exports": {
17
+ "require": "./index.js",
18
+ "import": "./index.mjs",
19
+ "types": "./types/index.d.ts"
20
+ },
21
+ "bin": {
22
+ "oox": "bin/cli.js"
23
+ },
24
+ "author": "lipingruan",
25
+ "homepage": "https://github.com/lipingruan/oox",
26
+ "license": "MIT",
27
+ "dependencies": {
28
+ "chalk": "^4.1.0",
29
+ "socket.io": "^4.4.0",
30
+ "socket.io-client": "^4.4.0"
31
+ },
32
+ "engines": {
33
+ "node": ">=12.0.0"
34
+ }
35
+ }
package/types/app.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { EventEmitter } from 'node:events';
4
+ import { AsyncLocalStorage } from 'node:async_hooks';
5
+ export interface ReturnsBody {
6
+ traceId: string;
7
+ success: boolean;
8
+ body?: any;
9
+ error?: {
10
+ message: string;
11
+ stack: string;
12
+ };
13
+ }
14
+ export declare class Context {
15
+ traceId?: string;
16
+ }
17
+ export declare const asyncStore: AsyncLocalStorage<Context>;
18
+ export declare const eventHub: EventEmitter;
19
+ /**
20
+ * the kvMethods is all actions refs [has bind this]
21
+ */
22
+ export declare const kvMethods: Map<string, Function>;
23
+ export declare const sourceKVMethods: Map<string, Function>;
24
+ export declare function setMethods(methods: any): void;
25
+ export declare function getMethods(): any;
26
+ export declare function on(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
27
+ export declare function on(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
28
+ export declare function on(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
29
+ /**
30
+ * Call an Function on RPC server
31
+ * @param action
32
+ * @param params
33
+ * @param context
34
+ * @returns
35
+ */
36
+ export declare function call(action: string, params: any[], context: Context): Promise<ReturnsBody>;
37
+ export declare function execute(action: string, params: Array<any>, context: Context): Promise<any>;
@@ -0,0 +1,8 @@
1
+ export declare function getAllEnvArgs(): {
2
+ [x: string]: any;
3
+ };
4
+ export declare function getEnvArgs(names: string[]): {
5
+ [x: string]: any;
6
+ };
7
+ export declare function getEnvArg(name: string): any;
8
+ export declare function parseEnvArg(value: string): any;
@@ -0,0 +1,2 @@
1
+ #! /usr/bin/env node
2
+ export {};
@@ -0,0 +1 @@
1
+ export declare function configure(): any;
@@ -0,0 +1 @@
1
+ export declare function proxyGroup(groupDirectory: string, excludes?: any[]): void;
@@ -0,0 +1 @@
1
+ export declare function registry(urls: string | string[]): Promise<void>;
@@ -0,0 +1 @@
1
+ export declare function startup(): Promise<void>;
@@ -0,0 +1,70 @@
1
+ /// <reference types="node" />
2
+ import * as app from './app';
3
+ import Module, { ModuleConfig } from './modules/module';
4
+ import Modules from './modules';
5
+ export { ReturnsBody } from './app';
6
+ export { Module, ModuleConfig };
7
+ export declare const modules: Modules;
8
+ export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, on: typeof app.on;
9
+ export declare class Context extends app.Context {
10
+ sourceIP: string;
11
+ ip: string;
12
+ caller: string;
13
+ callerId: string;
14
+ connection?: RPCKeepAliveConnection;
15
+ toJSON?(): string;
16
+ }
17
+ export declare class Config {
18
+ [x: string]: any;
19
+ name: string;
20
+ host: string;
21
+ }
22
+ export declare const config: Config;
23
+ export declare function getConfig(): void;
24
+ export declare function setGenTraceIdFunction(fn: () => string): void;
25
+ /**
26
+ * 生成随机不重复id
27
+ */
28
+ export declare function genTraceId(): string;
29
+ /**
30
+ * 获取链路跟踪上下文
31
+ */
32
+ export declare function genContext(context?: Context): Context;
33
+ export declare function getContext(): Context;
34
+ export declare function serve(): Promise<void>;
35
+ export declare function stop(): Promise<void>;
36
+ export interface RPCConnectionAdapter {
37
+ rpc(connection: any, action: string, params: any[], context: Context): Promise<any>;
38
+ }
39
+ export interface RPCKeepAliveConnectionData {
40
+ /**
41
+ * 是否连接成功
42
+ */
43
+ connected: boolean;
44
+ /**
45
+ * 连接主机地址
46
+ */
47
+ host: string;
48
+ /**
49
+ * 连接服务名称
50
+ */
51
+ name: string;
52
+ /**
53
+ * 目标服务网络唯一ID
54
+ */
55
+ id: string;
56
+ }
57
+ export declare class RPCKeepAliveConnection {
58
+ data: RPCKeepAliveConnectionData;
59
+ nativeConnection: any;
60
+ adapter: RPCConnectionAdapter;
61
+ constructor(adapter: RPCConnectionAdapter, nativeConnection: any, data?: RPCKeepAliveConnectionData);
62
+ }
63
+ export declare const keepAliveConnections: Map<string, Map<string, RPCKeepAliveConnection>>;
64
+ export declare function getKeepAliveConnections(name: string): Map<string, RPCKeepAliveConnection>;
65
+ export declare function getKeepAliveConnection(name: string, id: string): RPCKeepAliveConnection | null;
66
+ export declare function addKeepAliveConnection(connection: RPCKeepAliveConnection): void;
67
+ export declare function removeKeepAliveConnection(connection: RPCKeepAliveConnection): void;
68
+ export declare function removeKeepAliveConnection(name: string, id: string): void;
69
+ export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
70
+ export declare function rpc(connection: RPCKeepAliveConnection, action: string, params: any[], context?: Context): Promise<any>;
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'node:http';
3
+ import * as oox from '../../index';
4
+ import Module, { ModuleConfig } from '../module';
5
+ export declare class HTTPConfig extends ModuleConfig {
6
+ port: number;
7
+ path: string;
8
+ origin: string;
9
+ }
10
+ export default class HTTPModule extends Module {
11
+ name: string;
12
+ config: HTTPConfig;
13
+ server: http.Server;
14
+ setConfig(config: HTTPConfig): void;
15
+ getConfig(): HTTPConfig;
16
+ /**
17
+ * start http service
18
+ */
19
+ serve(): Promise<void>;
20
+ /**
21
+ * stop http service
22
+ */
23
+ stop(): Promise<void>;
24
+ /**
25
+ * browser cross origin
26
+ */
27
+ cors(request: http.IncomingMessage, response: http.ServerResponse): boolean;
28
+ /**
29
+ * HTTP-RPC服务器请求监听方法
30
+ */
31
+ call(request: http.IncomingMessage, response: http.ServerResponse): Promise<void>;
32
+ /**
33
+ * HTTP Response Catch
34
+ */
35
+ respond(request: http.IncomingMessage, response: http.ServerResponse, format: {
36
+ body?: any;
37
+ success: boolean;
38
+ error?: {
39
+ message: any;
40
+ stack: any;
41
+ };
42
+ }): void;
43
+ /**
44
+ * HTTP RPC
45
+ */
46
+ rpc(url: string | URL, action: string, params: Array<any>, context?: oox.Context): Promise<any>;
47
+ }
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import * as http from 'node:http';
5
+ import * as stream from 'node:stream';
6
+ /**
7
+ * Stream => Buffer
8
+ */
9
+ export declare function stream2buffer(stream: stream.Readable, totalLength?: number): Promise<Buffer>;
10
+ /**
11
+ * Request => JSONObject
12
+ */
13
+ export declare function parseHTTPBody(request: http.IncomingMessage): Promise<any>;
14
+ /**
15
+ * http request
16
+ */
17
+ export declare function httpRequest(url: URL | string, options: http.RequestOptions, body: string): Promise<any>;
@@ -0,0 +1,23 @@
1
+ import Module from './module';
2
+ import HTTP from './http';
3
+ import SocketIO from './socketio';
4
+ export default class Modules extends Module {
5
+ #private;
6
+ /**
7
+ * the module unique name
8
+ */
9
+ name: string;
10
+ /**
11
+ * all builtin modules
12
+ */
13
+ builtins: {
14
+ http: HTTP;
15
+ socketio: SocketIO;
16
+ };
17
+ constructor();
18
+ add(module: Module): this;
19
+ get(name: string): Module;
20
+ remove(name: string): Promise<void>;
21
+ serve(): Promise<void>;
22
+ stop(): Promise<void>;
23
+ }
@@ -0,0 +1,11 @@
1
+ export declare class ModuleConfig {
2
+ disabled: boolean;
3
+ }
4
+ export default class Module {
5
+ config: ModuleConfig;
6
+ name: string;
7
+ setConfig(config: any): void;
8
+ getConfig(): ModuleConfig;
9
+ serve(): Promise<void>;
10
+ stop(): Promise<void>;
11
+ }
@@ -0,0 +1,23 @@
1
+ import { ClientSocket as Socket } from './socket';
2
+ import SocketIOServer from './server';
3
+ export default class SocketIOCore extends SocketIOServer {
4
+ /**
5
+ * connect to <SocketIO RPC> service
6
+ */
7
+ connect(url: string): Promise<Socket>;
8
+ /**
9
+ * 客户端Socket连接事件
10
+ */
11
+ clientOnSocketConnection(socket: Socket): void;
12
+ clientOnDisconnect(socket: Socket, reason: any): void;
13
+ clientOnConnection(socket: Socket): void;
14
+ /**
15
+ * 客户端Socket断开事件
16
+ * @param {Socket} socket
17
+ */
18
+ clientOnSocketDisconnect(socket: Socket, reason: any): void;
19
+ /**
20
+ * 等待socket连接
21
+ */
22
+ clientWaitConnection(socket: Socket): Promise<void>;
23
+ }
@@ -0,0 +1,35 @@
1
+ import SocketIOClient from './client';
2
+ import * as oox from '../../index';
3
+ import { RPCKeepAliveConnectionData, RPCConnectionAdapter } from '../../index';
4
+ import { Socket, ServerSocket, ClientSocket } from './socket';
5
+ export default class SocketIOModule extends SocketIOClient implements RPCConnectionAdapter {
6
+ sockets: Map<string, Socket>;
7
+ onSyncConnection(socket: Socket): oox.RPCKeepAliveConnectionData[];
8
+ serverOnDisconnect(socket: ServerSocket, reason: string): void;
9
+ clientOnDisconnect(socket: ClientSocket, reason: any): void;
10
+ /**
11
+ *
12
+ * @param socket 是由哪个通道发送过来的
13
+ * @param connectionDatas
14
+ */
15
+ clientOnSyncConnection(socket: Socket, connectionDatas: RPCKeepAliveConnectionData[]): void;
16
+ onFetchActions(socket: Socket, search: string): string[];
17
+ fetchActions(url: string, search?: string): Promise<RPCKeepAliveConnectionData[]>;
18
+ fetchActions(name: string, search?: string): Promise<RPCKeepAliveConnectionData[]>;
19
+ onConnection(socket: Socket): void;
20
+ /**
21
+ *
22
+ * @param {Socket} socket
23
+ */
24
+ serverOnConnection(socket: ServerSocket): void;
25
+ call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody>;
26
+ clientOnConnection(socket: ClientSocket): void;
27
+ /**
28
+ * socketio emit
29
+ */
30
+ emit(url: string, action: string, params: any[]): Promise<unknown>;
31
+ /**
32
+ * RPC
33
+ */
34
+ rpc(url: string, action: string, params: [], context?: oox.Context): Promise<any>;
35
+ }
@@ -0,0 +1,35 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'http';
3
+ import { Server, ServerOptions } from 'socket.io';
4
+ import Module, { ModuleConfig } from '../module';
5
+ import { ServerSocket as Socket } from './socket';
6
+ export declare class SocketIOConfig extends ModuleConfig {
7
+ port: number;
8
+ path: string;
9
+ origin: string;
10
+ }
11
+ export default class SocketIOServer extends Module {
12
+ #private;
13
+ name: string;
14
+ config: SocketIOConfig;
15
+ server: http.Server;
16
+ socketServer: Server;
17
+ setConfig(config: SocketIOConfig): void;
18
+ getConfig(): SocketIOConfig;
19
+ serve(): Promise<void>;
20
+ stop(): Promise<void>;
21
+ genSocketIOServerOptions(): Partial<ServerOptions>;
22
+ createSocketIOServer(): void;
23
+ /**
24
+ * 服务端Socket连接事件
25
+ */
26
+ serverOnSocketConnection(socket: Socket): void;
27
+ serverOnConnection(socket: Socket): void;
28
+ /**
29
+ * 服务端Socket断开事件
30
+ * @param {Socket} socket
31
+ * @param {Error} reason
32
+ */
33
+ serverOnSocketDisconnect(socket: Socket, reason: string): void;
34
+ serverOnDisconnect(socket: Socket, reason: string): void;
35
+ }