|
| 1 | +// Type definitions for ws |
| 2 | +// Project: https://github.com/einaros/ws |
| 3 | +// Definitions by: Paul Loyd <https://github.com/loyd> |
| 4 | +// Definitions: https://github.com/borisyankov/DefinitelyTyped |
| 5 | + |
| 6 | +/// <reference path="../common/definitions/node.d.ts" /> |
| 7 | + |
| 8 | +declare module "ws" { |
| 9 | + import events = require('events'); |
| 10 | + import http = require('http'); |
| 11 | + import net = require('net'); |
| 12 | + |
| 13 | + class WebSocket extends events.EventEmitter { |
| 14 | + static CONNECTING: number; |
| 15 | + static OPEN: number; |
| 16 | + static CLOSING: number; |
| 17 | + static CLOSED: number; |
| 18 | + |
| 19 | + bytesReceived: number; |
| 20 | + readyState: number; |
| 21 | + protocolVersion: string; |
| 22 | + url: string; |
| 23 | + supports: any; |
| 24 | + upgradeReq: http.ServerRequest; |
| 25 | + |
| 26 | + CONNECTING: number; |
| 27 | + OPEN: number; |
| 28 | + CLOSING: number; |
| 29 | + CLOSED: number; |
| 30 | + |
| 31 | + onopen: (event: {target: WebSocket}) => void; |
| 32 | + onerror: (err: Error) => void; |
| 33 | + onclose: (event: {wasClean: boolean; code: number; reason: string; target: WebSocket}) => void; |
| 34 | + onmessage: (event: {data: any; type: string; target: WebSocket}) => void; |
| 35 | + |
| 36 | + constructor(address: string, options?: { |
| 37 | + protocol?: string; |
| 38 | + agent?: http.Agent; |
| 39 | + headers?: {[key: string]: string}; |
| 40 | + protocolVersion?: any; |
| 41 | + host?: string; |
| 42 | + origin?: string; |
| 43 | + pfx?: any; |
| 44 | + key?: any; |
| 45 | + passphrase?: string; |
| 46 | + cert?: any; |
| 47 | + ca?: any[]; |
| 48 | + ciphers?: string; |
| 49 | + rejectUnauthorized?: boolean; |
| 50 | + }); |
| 51 | + |
| 52 | + close(code?: number, data?: any): void; |
| 53 | + pause(): void; |
| 54 | + resume(): void; |
| 55 | + ping(data?: any, options?: {mask?: boolean; binary?: boolean}, dontFail?: boolean): void; |
| 56 | + pong(data?: any, options?: {mask?: boolean; binary?: boolean}, dontFail?: boolean): void; |
| 57 | + send(data: any, cb?: (err: Error) => void): void; |
| 58 | + send(data: any, options: {mask?: boolean; binary?: boolean}, cb?: (err: Error) => void): void; |
| 59 | + stream(options: {mask?: boolean; binary?: boolean}, cb?: (err: Error, final: boolean) => void): void; |
| 60 | + stream(cb?: (err: Error, final: boolean) => void): void; |
| 61 | + terminate(): void; |
| 62 | + |
| 63 | + // HTML5 WebSocket events |
| 64 | + addEventListener(method: 'message', cb?: (event: {data: any; type: string; target: WebSocket}) => void): void; |
| 65 | + addEventListener(method: 'close', cb?: (event: {wasClean: boolean; code: number; |
| 66 | + reason: string; target: WebSocket}) => void): void; |
| 67 | + addEventListener(method: 'error', cb?: (err: Error) => void): void; |
| 68 | + addEventListener(method: 'open', cb?: (event: {target: WebSocket}) => void): void; |
| 69 | + addEventListener(method: string, listener?: () => void): void; |
| 70 | + |
| 71 | + // Events |
| 72 | + on(event: 'error', cb: (err: Error) => void): WebSocket; |
| 73 | + on(event: 'close', cb: (code: number, message: string) => void): WebSocket; |
| 74 | + on(event: 'message', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 75 | + on(event: 'ping', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 76 | + on(event: 'pong', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 77 | + on(event: 'open', cb: () => void): WebSocket; |
| 78 | + on(event: string, listener: () => void): WebSocket; |
| 79 | + |
| 80 | + addListener(event: 'error', cb: (err: Error) => void): WebSocket; |
| 81 | + addListener(event: 'close', cb: (code: number, message: string) => void): WebSocket; |
| 82 | + addListener(event: 'message', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 83 | + addListener(event: 'ping', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 84 | + addListener(event: 'pong', cb: (data: any, flags: {binary: boolean}) => void): WebSocket; |
| 85 | + addListener(event: 'open', cb: () => void): WebSocket; |
| 86 | + addListener(event: string, listener: () => void): WebSocket; |
| 87 | + } |
| 88 | + |
| 89 | + module WebSocket { |
| 90 | + export interface IServerOptions { |
| 91 | + host?: string; |
| 92 | + port?: number; |
| 93 | + server?: http.Server; |
| 94 | + verifyClient?: { |
| 95 | + (info: {origin: string; secure: boolean; req: http.ServerRequest}): boolean; |
| 96 | + (info: {origin: string; secure: boolean; req: http.ServerRequest}, |
| 97 | + callback: (res: boolean) => void): void; |
| 98 | + }; |
| 99 | + handleProtocols?: any; |
| 100 | + path?: string; |
| 101 | + noServer?: boolean; |
| 102 | + disableHixie?: boolean; |
| 103 | + clientTracking?: boolean; |
| 104 | + } |
| 105 | + |
| 106 | + export class Server extends events.EventEmitter { |
| 107 | + options: IServerOptions; |
| 108 | + path: string; |
| 109 | + clients: WebSocket[]; |
| 110 | + |
| 111 | + constructor(options?: IServerOptions, callback?: Function); |
| 112 | + |
| 113 | + close(): void; |
| 114 | + handleUpgrade(request: http.ServerRequest, socket: net.Socket, |
| 115 | + upgradeHead: Buffer, callback: (client: WebSocket) => void): void; |
| 116 | + |
| 117 | + // Events |
| 118 | + on(event: 'error', cb: (err: Error) => void): Server; |
| 119 | + on(event: 'headers', cb: (headers: string[]) => void): Server; |
| 120 | + on(event: 'connection', cb: (client: WebSocket) => void): Server; |
| 121 | + on(event: string, listener: () => void): Server; |
| 122 | + |
| 123 | + addListener(event: 'error', cb: (err: Error) => void): Server; |
| 124 | + addListener(event: 'headers', cb: (headers: string[]) => void): Server; |
| 125 | + addListener(event: 'connection', cb: (client: WebSocket) => void): Server; |
| 126 | + addListener(event: string, listener: () => void): Server; |
| 127 | + } |
| 128 | + |
| 129 | + export function createServer(options?: IServerOptions, |
| 130 | + connectionListener?: (client: WebSocket) => void): Server; |
| 131 | + export function connect(address: string, openListener?: Function): void; |
| 132 | + export function createConnection(address: string, openListener?: Function): void; |
| 133 | + } |
| 134 | + |
| 135 | + export = WebSocket; |
| 136 | +} |
0 commit comments