@oox/ws-client 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/adapter.js +50 -0
- package/browser.js +2517 -0
- package/browser.js.map +1 -0
- package/index.js +3 -0
- package/package.json +35 -0
- package/socket.js +189 -0
- package/types/adapter.d.ts +19 -0
- package/types/index.d.ts +3 -0
- package/types/socket.d.ts +59 -0
- package/types/utils.d.ts +14 -0
- package/utils.js +44 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SampleKeepAliveNativeConnection } from '@oox/client';
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
export type WebSocketEventType = keyof WebSocketEventMap;
|
|
4
|
+
export declare const WebsocketEvents: WebSocketEventType[];
|
|
5
|
+
export declare const MessagePackEncodeOptions: {
|
|
6
|
+
ignoreUndefined: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare enum RPCType {
|
|
9
|
+
Request = 1,
|
|
10
|
+
Response = 2
|
|
11
|
+
}
|
|
12
|
+
export interface RPCMessage {
|
|
13
|
+
type: RPCType;
|
|
14
|
+
id?: number;
|
|
15
|
+
event?: string;
|
|
16
|
+
args: any[];
|
|
17
|
+
}
|
|
18
|
+
export declare class RPCEvent extends Event {
|
|
19
|
+
data: any[];
|
|
20
|
+
constructor(type: string, data: any[]);
|
|
21
|
+
}
|
|
22
|
+
export declare const MAX_REQUEST_ID = 4294967295;
|
|
23
|
+
export type Callback = (...args: any[]) => void;
|
|
24
|
+
export declare class Socket extends EventEmitter implements SampleKeepAliveNativeConnection {
|
|
25
|
+
private callbacks;
|
|
26
|
+
private requestId;
|
|
27
|
+
private eventHub;
|
|
28
|
+
native: WebSocket | null;
|
|
29
|
+
connected: boolean;
|
|
30
|
+
url: URL;
|
|
31
|
+
protocols?: string | string[];
|
|
32
|
+
constructor(address: string | URL, protocols?: string | string[]);
|
|
33
|
+
isNativeEvent(event: string): boolean;
|
|
34
|
+
off(event: WebSocketEventType | string, listener?: (...args: any[]) => void): this;
|
|
35
|
+
emit(event: WebSocketEventType | string, ...args: any[]): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 生成自增请求ID
|
|
38
|
+
*/
|
|
39
|
+
private generateRequestId;
|
|
40
|
+
disconnect(): void;
|
|
41
|
+
connect(): void;
|
|
42
|
+
send(data: string | Uint8Array): void;
|
|
43
|
+
/**
|
|
44
|
+
* 发送RPC消息
|
|
45
|
+
*/
|
|
46
|
+
sendRPCMessage(message: RPCMessage): void;
|
|
47
|
+
/**
|
|
48
|
+
* 处理消息
|
|
49
|
+
*/
|
|
50
|
+
private handleMessage;
|
|
51
|
+
/**
|
|
52
|
+
* 处理RPC消息
|
|
53
|
+
*/
|
|
54
|
+
private handleRPC;
|
|
55
|
+
/**
|
|
56
|
+
* RPC调用
|
|
57
|
+
*/
|
|
58
|
+
rpc(event: string, ...args: any[]): void;
|
|
59
|
+
}
|
package/types/utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const OOXEvent: {
|
|
2
|
+
CONNECT: string;
|
|
3
|
+
DISCONNECT: string;
|
|
4
|
+
ERROR: string;
|
|
5
|
+
CALL: string;
|
|
6
|
+
READY: string;
|
|
7
|
+
ENABLED: string;
|
|
8
|
+
DISABLED: string;
|
|
9
|
+
REGISTRY_SYNC_CONNECTIONS: string;
|
|
10
|
+
REGISTRY_SUBSCRIBE: string;
|
|
11
|
+
REGISTRY_NOTIFY: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function isWebSocketURL(url: string | URL): boolean;
|
|
14
|
+
export declare function genWebSocketURL(url: string): URL;
|
package/utils.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const OOXEvent = {
|
|
2
|
+
CONNECT: 'open',
|
|
3
|
+
DISCONNECT: 'close',
|
|
4
|
+
ERROR: 'socket:error',
|
|
5
|
+
CALL: 'call',
|
|
6
|
+
READY: 'oox:ready',
|
|
7
|
+
ENABLED: 'oox:enabled',
|
|
8
|
+
DISABLED: 'oox:disabled',
|
|
9
|
+
REGISTRY_SYNC_CONNECTIONS: 'oox:registry:sync_connections',
|
|
10
|
+
REGISTRY_SUBSCRIBE: 'oox:registry:subscribe',
|
|
11
|
+
REGISTRY_NOTIFY: 'oox:registry:notify',
|
|
12
|
+
};
|
|
13
|
+
export function isWebSocketURL(url) {
|
|
14
|
+
if ('string' === typeof url) {
|
|
15
|
+
return /^wss?:\/\/.+$/.test(url);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return url.protocol === 'ws:' || url.protocol === 'wss:';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function genWebSocketURL(url) {
|
|
22
|
+
// :6000
|
|
23
|
+
if (url.startsWith(':'))
|
|
24
|
+
url = 'ws://localhost' + url;
|
|
25
|
+
// 127.0.0.1:6000
|
|
26
|
+
if (!/^\w+?:\/.+$/.test(url))
|
|
27
|
+
url = 'ws://' + url;
|
|
28
|
+
const urlObject = new URL(url);
|
|
29
|
+
// :8000 => :8000/ws
|
|
30
|
+
const notSetPath = !urlObject.pathname || (urlObject.pathname === '/'
|
|
31
|
+
&& !url.endsWith('/')
|
|
32
|
+
&& !urlObject.search
|
|
33
|
+
&& !urlObject.hash);
|
|
34
|
+
if (notSetPath) {
|
|
35
|
+
urlObject.pathname = '/ws';
|
|
36
|
+
}
|
|
37
|
+
if (urlObject.protocol !== 'wss:') {
|
|
38
|
+
if (urlObject.port === '443')
|
|
39
|
+
urlObject.protocol = 'wss:';
|
|
40
|
+
else
|
|
41
|
+
urlObject.protocol = 'ws:';
|
|
42
|
+
}
|
|
43
|
+
return urlObject;
|
|
44
|
+
}
|