Merge pull request #23791 from dex4er/simple-websocket

New typings: simple-websocket 7.0
This commit is contained in:
Benjamin Lichtman
2018-03-09 08:33:06 -08:00
committed by GitHub
5 changed files with 158 additions and 0 deletions

32
types/simple-websocket/index.d.ts vendored Normal file
View File

@@ -0,0 +1,32 @@
// Type definitions for simple-websocket 7.0
// Project: https://github.com/feross/simple-websocket
// Definitions by: Piotr Roszatycki <https://github.com/dex4er>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { Duplex, DuplexOptions } from "stream";
import WebSocket = require("ws");
declare namespace Socket {
interface Options extends DuplexOptions {
/** websocket server url */
url?: string;
/** raw websocket instance to wrap */
socket?: WebSocket;
}
}
declare class Socket extends Duplex {
static WEBSOCKET_SUPPORT: boolean;
constructor(options: Socket.Options | string);
/** Send text/binary data to the WebSocket server */
send(chunk: any): void;
/** Destroy and cleanup this websocket connection */
destroy(err?: Error): void;
}
export = Socket;

70
types/simple-websocket/server.d.ts vendored Normal file
View File

@@ -0,0 +1,70 @@
/// <reference types="node" />
import { EventEmitter } from "events";
import * as http from "http";
import * as net from "net";
import WebSocket = require("ws");
import Socket = require(".");
declare namespace SocketServer {
type Options = WebSocket.ServerOptions;
}
declare class SocketServer extends EventEmitter {
options: SocketServer.Options;
path: string;
clients: Set<WebSocket>;
constructor(options?: SocketServer.Options, callback?: () => void);
close(cb?: (err?: Error) => void): void;
handleUpgrade(request: http.IncomingMessage, socket: net.Socket,
upgradeHead: Buffer, callback: (client: WebSocket) => void): void;
shouldHandle(request: http.IncomingMessage): boolean;
// Events
addListener(event: "connection", cb: (client: Socket) => void): this;
addListener(event: "error", cb: (err: Error) => void): this;
addListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
addListener(event: "listening", cb: () => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: "connection", socket: Socket): boolean;
emit(event: "error", error: Error): boolean;
emit(event: "headers", headers: string[], request: http.IncomingMessage): boolean;
emit(event: "listening"): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: "connection", cb: (socket: Socket, request: http.IncomingMessage) => void): this;
on(event: "error", cb: (error: Error) => void): this;
on(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
on(event: "listening", cb: () => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: "connection", cb: (socket: Socket, request: http.IncomingMessage) => void): this;
once(event: "error", cb: (error: Error) => void): this;
once(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
once(event: "listening", cb: () => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: "connection", cb: (client: Socket) => void): this;
prependListener(event: "error", cb: (err: Error) => void): this;
prependListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
prependListener(event: "listening", cb: () => void): this;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: "connection", cb: (client: Socket) => void): this;
prependOnceListener(event: "error", cb: (err: Error) => void): this;
prependOnceListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
prependOnceListener(event: "listening", cb: () => void): this;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
removeListener(event: "connection", cb: (client: Socket) => void): this;
removeListener(event: "error", cb: (err: Error) => void): this;
removeListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this;
removeListener(event: "listening", cb: () => void): this;
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
}
export = SocketServer;

View File

@@ -0,0 +1,30 @@
import Socket = require("simple-websocket");
if (Socket.WEBSOCKET_SUPPORT) {
const socket = new Socket('ws://echo.websocket.org');
socket.on('connect', () => {
// socket is connected!
socket.send('sup!');
});
socket.on('data', (data: string | Buffer) => {
console.log('got message: ' + data);
});
socket.on('error', (err: Error) => {
socket.destroy(err);
});
}
import Server = require("simple-websocket/server");
const server = new Server({ port: 8080 });
server.on('connection', (socket: Socket) => {
socket.write('pong');
socket.on('data', (data: string | Buffer) => {});
socket.on('close', () => {});
socket.on('error', (err: Error) => {});
});
server.close();

View File

@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
"server.d.ts",
"simple-websocket-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }