oox 0.3.2 → 0.3.4

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.
@@ -1,11 +1,8 @@
1
- import { Socket as _ServerSocket } from 'socket.io';
2
- import { Socket as _ClientSocket } from 'socket.io-client';
3
- import { RPCKeepAliveConnectionData } from '../../index.js';
4
- export interface ServerSocket extends _ServerSocket {
5
- data: RPCKeepAliveConnectionData;
6
- }
7
- export interface ClientSocket extends _ClientSocket {
8
- data: RPCKeepAliveConnectionData;
9
- }
10
- export type Socket = ServerSocket | ClientSocket;
11
- export declare const sockets: Map<string, Socket>;
1
+ import { Socket as ServerSocket } from 'socket.io';
2
+ import { Socket as ClientSocket } from 'socket.io-client';
3
+ export type SocketMap = {
4
+ [key: string]: ServerSocket | ClientSocket;
5
+ server: ServerSocket;
6
+ client: ClientSocket;
7
+ };
8
+ export type Socket<T extends keyof SocketMap = keyof SocketMap> = SocketMap[T];
@@ -0,0 +1,8 @@
1
+ export declare enum OOXEvent {
2
+ READY = "oox:ready",
3
+ ENABLED = "oox:enabled",
4
+ DISABLED = "oox:disabled",
5
+ SYNC_CONNECTIONS = "oox:sync_connections"
6
+ }
7
+ export declare function isWebSocketURL(url: string | URL): boolean;
8
+ export declare function genWebSocketURL(url: string): URL;
@@ -0,0 +1,16 @@
1
+ import { KeepAliveConnection, KeepAliveConnectionData } from './keepalive-connection.js';
2
+ export interface SyncConnectionsQuery {
3
+ name?: string | string[];
4
+ }
5
+ /**
6
+ * 服务器发送连接列表
7
+ * @param connection
8
+ * @param query
9
+ * @param callback
10
+ */
11
+ export declare function onSyncConnections(connection: KeepAliveConnection<any>, query: SyncConnectionsQuery, callback: (datas: KeepAliveConnectionData[]) => void): void;
12
+ /**
13
+ * 同步连接列表
14
+ * @param datas 连接参数列表
15
+ */
16
+ export declare function syncConnections(datas: KeepAliveConnectionData[]): void;
@@ -0,0 +1 @@
1
+ export { SampleKeepAliveConnectionAdapter, SampleKeepAliveNativeConnection } from './keepalive-connection-sample.js';
@@ -0,0 +1,46 @@
1
+ import * as oox from '../index.js';
2
+ export interface SampleKeepAliveNativeConnection {
3
+ connected: boolean;
4
+ connect?(): void;
5
+ disconnect(): void;
6
+ on(event: string | symbol, listener: (...args: any[]) => void): any;
7
+ once(event: string | symbol, listener: (...args: any[]) => void): any;
8
+ off(event: string | symbol, listener?: (...args: any[]) => void): any;
9
+ emit(event: string | symbol, ...args: any[]): any;
10
+ removeAllListeners(event?: string | symbol): any;
11
+ }
12
+ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements oox.KeepAliveConnectionAdapter<NativeConnection> {
13
+ name: string;
14
+ OOXEvent: {
15
+ READY: string;
16
+ ENABLED: string;
17
+ DISABLED: string;
18
+ SYNC_CONNECTIONS: string;
19
+ };
20
+ nativeEvent: {
21
+ CONNECT: string;
22
+ DISCONNECT: string;
23
+ ERROR: string;
24
+ };
25
+ abstract newConnection(url: string | URL): oox.KeepAliveConnection<NativeConnection>;
26
+ bindConnectionEvents(connection: oox.KeepAliveConnection<NativeConnection>): void;
27
+ open(url: string | URL): Promise<oox.KeepAliveConnection<NativeConnection>>;
28
+ close(connection: oox.KeepAliveConnection<NativeConnection>): Promise<void>;
29
+ /**
30
+ * 发送事件
31
+ * @param socket
32
+ * @param event
33
+ * @param params
34
+ */
35
+ emit(socket: NativeConnection, event: string, params: any[]): Promise<unknown>;
36
+ /**
37
+ * RPC
38
+ */
39
+ rpc(connection: oox.KeepAliveConnection<NativeConnection>, action: string, params: [], context?: oox.Context): Promise<any>;
40
+ /**
41
+ * 绑定 call 事件
42
+ * @param connection
43
+ */
44
+ bindCall(connection: oox.KeepAliveConnection<NativeConnection>): void;
45
+ call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody>;
46
+ }
@@ -1,97 +0,0 @@
1
- import * as SocketIOClient from 'socket.io-client';
2
- import { sockets } from './socket.js';
3
- import SocketIOServer from './server.js';
4
- import * as oox from '../../index.js';
5
- export default class SocketIOCore extends SocketIOServer {
6
- /**
7
- * connect to <SocketIO RPC> service
8
- */
9
- async connect(url) {
10
- let socket = sockets.get(url);
11
- // 已经连接的直接返回
12
- if (socket) {
13
- try {
14
- await this.clientWaitConnection(socket);
15
- }
16
- catch (error) {
17
- this.clientOnSocketDisconnect(socket, error.message);
18
- throw error;
19
- }
20
- return socket;
21
- }
22
- const headers = {
23
- 'x-caller': oox.config.name
24
- };
25
- const { host } = oox.config;
26
- headers['x-ip'] = host;
27
- headers['x-caller-id'] = this.getUrl();
28
- // create socket handler
29
- const mURL = new URL(url);
30
- socket = SocketIOClient.io(mURL.origin, {
31
- extraHeaders: headers,
32
- path: mURL.pathname
33
- });
34
- socket.data = { name: 'anonymous', connected: false, id: url, host: mURL.host };
35
- sockets.set(url, socket);
36
- try {
37
- await this.clientWaitConnection(socket);
38
- }
39
- catch (error) {
40
- this.clientOnSocketDisconnect(socket, error);
41
- throw error;
42
- }
43
- return socket;
44
- }
45
- /**
46
- * 客户端Socket连接事件
47
- */
48
- clientOnSocketConnection(socket) {
49
- socket.data.connected = true;
50
- socket.once('disconnect', reason => this.clientOnSocketDisconnect(socket, reason));
51
- this.clientOnConnection(socket);
52
- }
53
- clientOnDisconnect(socket, reason) { }
54
- clientOnConnection(socket) { }
55
- /**
56
- * 客户端Socket断开事件
57
- * @param {Socket} socket
58
- */
59
- clientOnSocketDisconnect(socket, reason) {
60
- socket.data.connected = false;
61
- socket.disconnect();
62
- sockets.delete(socket.data.id);
63
- this.clientOnDisconnect(socket, reason);
64
- }
65
- /**
66
- * 等待socket连接
67
- */
68
- async clientWaitConnection(socket) {
69
- if (socket.data.connected)
70
- return;
71
- if (socket.connect)
72
- socket.connect();
73
- try {
74
- await new Promise((resolve, reject) => {
75
- const onError = (reason) => {
76
- socket.offAny(onError);
77
- const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
78
- reject(new Error(message));
79
- };
80
- socket.once('disconnect', onError);
81
- socket.once('connect_error', onError);
82
- socket.once('connect_timeout', onError);
83
- socket.once('reconnect_error', onError);
84
- socket.once('reconnect_failed', onError);
85
- socket.once('oox_connected', ({ name }) => {
86
- socket.offAny(onError);
87
- socket.data.name = name;
88
- resolve();
89
- });
90
- });
91
- }
92
- catch (error) {
93
- throw new Error(error.message);
94
- }
95
- this.clientOnSocketConnection(socket);
96
- }
97
- }
@@ -1,23 +0,0 @@
1
- import { ClientSocket as Socket } from './socket.js';
2
- import SocketIOServer from './server.js';
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
- }