forked from thoov/mock-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
140 lines (114 loc) · 4.1 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Type definitions for Mock Socket 8.X+
// Project: Mock Socket
// Definitions by: Travis Hoover <https://github.com/thoov/mock-socket>
declare module 'mock-socket' {
// support TS under 3.5
type _Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
class EventTarget {
listeners: any;
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
dispatchEvent(evt: Event): boolean;
removeEventListener(type: string, listener?: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
}
interface WebSocketCallbackMap {
close: () => void;
error: (err: Error) => void;
message: (message: string | Blob | ArrayBuffer | ArrayBufferView) => void;
}
//
// https://html.spec.whatwg.org/multipage/web-sockets.html#websocket
//
class WebSocket extends EventTarget {
constructor(url: string | URL, protocols?: string|string[]);
static readonly CONNECTING: 0;
static readonly OPEN: 1;
static readonly CLOSING: 2;
static readonly CLOSED: 3;
readonly url: string;
readonly CONNECTING: 0;
readonly OPEN: 1;
readonly CLOSING: 2;
readonly CLOSED: 3;
readonly readyState: number;
readonly bufferedAmount: number;
onopen: (event: Event) => void;
onerror: (event: Event) => void;
onclose: (event: CloseEvent) => void;
onmessage: (event: MessageEvent) => void;
readonly extensions: string;
readonly protocol: string;
close(code?: number, reason?: string): void;
binaryType: BinaryType;
send(data: string | Blob | ArrayBuffer | ArrayBufferView): void;
}
interface Client extends _Omit<WebSocket, 'close'> {
target: WebSocket;
close(options?: CloseOptions): void;
on<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
off<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
}
class Server extends EventTarget {
constructor(url: string, options?: ServerOptions);
readonly options?: ServerOptions;
stop(callback?: () => void): void;
mockWebsocket(): void;
restoreWebsocket(): void;
on(type: string, callback: (socket: Client) => void): void;
off(type: string, callback: (socket: Client) => void): void;
close(options?: CloseOptions): void;
emit(event: string, data: any, options?: EmitOptions): void;
clients(): Client[];
to(room: any, broadcaster: any, broadcastList?: object): ToReturnObject;
in(any: any): ToReturnObject;
simulate(event: string): void;
static of(url: string): Server;
}
interface SocketIOClient extends EventTarget {
binaryType: BinaryType;
readonly CONNECTING: 0;
readonly OPEN: 1;
readonly CLOSING: 2;
readonly CLOSED: 3;
readonly url: string;
readonly readyState: number;
readonly protocol: string;
readonly target: this;
close(): this;
disconnect(): this;
emit(event: string, data: any): this;
send(data: any): this;
on(type: string, callback: (socket: SocketIOClient) => void): this;
off(type: string, callback: (socket: SocketIOClient) => void): void;
hasListeners(type: string): boolean;
join(room: string): void;
leave(room: string): void;
to(room: string): ToReturnObject;
in(room: string): ToReturnObject;
readonly broadcast: {
emit(event: string, data: any): SocketIOClient;
to(room: string): ToReturnObject;
in(room: string): ToReturnObject;
};
}
const SocketIO: {
(url: string, protocol?: string | string[]): SocketIOClient;
connect(url: string, protocol?: string | string[]): SocketIOClient;
}
interface CloseOptions {
code: number;
reason: string;
wasClean: boolean;
}
interface EmitOptions {
websockets: Client[];
}
interface ToReturnObject {
to: (chainedRoom: any, chainedBroadcaster: any) => ToReturnObject;
emit(event: Event, data: any): void;
}
interface ServerOptions {
mock?: boolean;
verifyClient?: () => boolean;
selectProtocol?: (protocols: string[]) => string | null;
}
}