forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.d.ts
151 lines (128 loc) · 6.49 KB
/
ws.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
141
142
143
144
145
146
147
148
149
150
151
// Type definitions for ws
// Project: https://github.com/einaros/ws
// Definitions by: Paul Loyd <https://github.com/loyd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module "ws" {
import * as events from 'events';
import * as http from 'http';
import * as https from 'https';
import * as net from 'net';
class WebSocket extends events.EventEmitter {
static CONNECTING: number;
static OPEN: number;
static CLOSING: number;
static CLOSED: number;
bytesReceived: number;
readyState: number;
protocolVersion: string;
url: string;
supports: any;
upgradeReq: http.IncomingMessage;
protocol: string;
CONNECTING: number;
OPEN: number;
CLOSING: number;
CLOSED: number;
onopen: (event: {target: WebSocket}) => void;
onerror: (err: Error) => void;
onclose: (event: {wasClean: boolean; code: number; reason: string; target: WebSocket}) => void;
onmessage: (event: {data: any; type: string; target: WebSocket}) => void;
constructor(address: string, options?: WebSocket.IClientOptions);
constructor(address: string, protocols?: string | string[], options?: WebSocket.IClientOptions);
close(code?: number, data?: any): void;
pause(): void;
resume(): void;
ping(data?: any, options?: {mask?: boolean; binary?: boolean}, dontFail?: boolean): void;
pong(data?: any, options?: {mask?: boolean; binary?: boolean}, dontFail?: boolean): void;
send(data: any, cb?: (err: Error) => void): void;
send(data: any, options: {mask?: boolean; binary?: boolean}, cb?: (err: Error) => void): void;
stream(options: {mask?: boolean; binary?: boolean}, cb?: (err: Error, final: boolean) => void): void;
stream(cb?: (err: Error, final: boolean) => void): void;
terminate(): void;
// HTML5 WebSocket events
addEventListener(method: 'message', cb?: (event: {data: any; type: string; target: WebSocket}) => void): void;
addEventListener(method: 'close', cb?: (event: {wasClean: boolean; code: number;
reason: string; target: WebSocket}) => void): void;
addEventListener(method: 'error', cb?: (err: Error) => void): void;
addEventListener(method: 'open', cb?: (event: {target: WebSocket}) => void): void;
addEventListener(method: string, listener?: () => void): void;
// Events
on(event: 'error', cb: (err: Error) => void): this;
on(event: 'close', cb: (code: number, message: string) => void): this;
on(event: 'message', cb: (data: any, flags: {binary: boolean}) => void): this;
on(event: 'ping', cb: (data: any, flags: {binary: boolean}) => void): this;
on(event: 'pong', cb: (data: any, flags: {binary: boolean}) => void): this;
on(event: 'open', cb: () => void): this;
on(event: string, listener: () => void): this;
addListener(event: 'error', cb: (err: Error) => void): this;
addListener(event: 'close', cb: (code: number, message: string) => void): this;
addListener(event: 'message', cb: (data: any, flags: {binary: boolean}) => void): this;
addListener(event: 'ping', cb: (data: any, flags: {binary: boolean}) => void): this;
addListener(event: 'pong', cb: (data: any, flags: {binary: boolean}) => void): this;
addListener(event: 'open', cb: () => void): this;
addListener(event: string, listener: () => void): this;
}
namespace WebSocket {
type VerifyClientCallbackSync = (info: {origin: string; secure: boolean; req: http.IncomingMessage}) => boolean;
type VerifyClientCallbackAsync = (info: {origin: string; secure: boolean; req: http.IncomingMessage}
, callback: (res: boolean) => void) => void;
export interface IClientOptions {
protocol?: string;
agent?: http.Agent;
headers?: {[key: string]: string};
protocolVersion?: any;
host?: string;
origin?: string;
pfx?: any;
key?: any;
passphrase?: string;
cert?: any;
ca?: any[];
ciphers?: string;
rejectUnauthorized?: boolean;
}
export interface IPerMessageDeflateOptions {
serverNoContextTakeover?: boolean;
clientNoContextTakeover?: boolean;
serverMaxWindowBits?: number;
clientMaxWindowBits?: number;
memLevel?: number;
}
export interface IServerOptions {
host?: string;
port?: number;
server?: http.Server | https.Server;
verifyClient?: VerifyClientCallbackAsync | VerifyClientCallbackSync;
handleProtocols?: any;
path?: string;
noServer?: boolean;
disableHixie?: boolean;
clientTracking?: boolean;
perMessageDeflate?: boolean | IPerMessageDeflateOptions;
}
export class Server extends events.EventEmitter {
options: IServerOptions;
path: string;
clients: WebSocket[];
constructor(options?: IServerOptions, callback?: Function);
close(cb?: () => {}): void;
handleUpgrade(request: http.IncomingMessage, socket: net.Socket,
upgradeHead: Buffer, callback: (client: WebSocket) => void): void;
// Events
on(event: 'error', cb: (err: Error) => void): this;
on(event: 'headers', cb: (headers: string[]) => void): this;
on(event: 'connection', cb: (client: WebSocket) => void): this;
on(event: string, listener: () => void): this;
addListener(event: 'error', cb: (err: Error) => void): this;
addListener(event: 'headers', cb: (headers: string[]) => void): this;
addListener(event: 'connection', cb: (client: WebSocket) => void): this;
addListener(event: string, listener: () => void): this;
}
export function createServer(options?: IServerOptions,
connectionListener?: (client: WebSocket) => void): Server;
export function connect(address: string, openListener?: Function): void;
export function createConnection(address: string, openListener?: Function): void;
}
export = WebSocket;
}