forked from thoov/mock-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
92 lines (74 loc) · 2.64 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
// Type definitions for Mock Socket 8.X+
// Project: Mock Socket
// Definitions by: Travis Hoover <https://github.com/thoov/mock-socket>
declare module 'mock-socket' {
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;
open: () => void;
}
//
// https://html.spec.whatwg.org/multipage/web-sockets.html#websocket
//
class WebSocket extends EventTarget {
constructor(url?: string, 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: EventHandlerNonNull;
onerror: EventHandlerNonNull;
onclose: EventHandlerNonNull;
readonly extensions: string;
readonly protocol: string;
close(code?: number, reason?: string): void;
onmessage: EventHandlerNonNull;
binaryType: BinaryType;
send(data: string | Blob | ArrayBuffer | ArrayBufferView): void;
on<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
}
class Server extends EventTarget {
constructor(url: string, options?: ServerOptions);
readonly options?: ServerOptions;
start(): void;
stop(callback?: () => void): void;
on(type: string, callback: (socket: WebSocket) => void): void;
close(options?: CloseOptions): void;
emit(event: string, data: any, options?: EmitOptions): void;
clients(): WebSocket[];
to(room: any, broadcaster: any, broadcastList?: object): ToReturnObject;
in(any: any): ToReturnObject;
simulate(event: string): void;
public of(url: string): Server;
}
interface CloseOptions {
code: number;
reason: string;
wasClean: boolean;
}
interface EmitOptions {
websockets: WebSocket[];
}
interface ToReturnObject {
to: (chainedRoom: any, chainedBroadcaster: any) => ToReturnObject;
emit(event: Event, data: any): void;
}
interface ServerOptions {
verifyClient?: () => boolean;
selectProtocol?: (protocols: string[]) => string | null;
}
}