Add new definition for "engine.io-client" (#18362)

This commit is contained in:
KentarouTakeda
2017-07-25 07:46:04 -07:00
committed by Andy
parent 0abcdf3c10
commit 53c47fa7d0
4 changed files with 287 additions and 0 deletions
@@ -0,0 +1,77 @@
import engine = require('engine.io');
import client = require('engine.io-client');
let server: engine.Server;
let socket: client.Socket;
let options: client.SocketOptions = {};
options.agent = false;
options.upgrade = true;
options.forceJSONP = false;
options.jsonp = false;
options.forceBase64 = false;
options.enablesXDR = false;
options.timestampRequests = true;
options.timestampParam = 't';
options.policyPort = 843;
options.path = '/engine.io';
options.transports = ['websocket'];
options.transportOptions = {};
options.rememberUpgrade = true;
options.pfx = '';
options.key = '';
options.passphrase = '';
options.cert = '';
options.ca = '';
options.ciphers = undefined;
options.rejectUnauthorized = true;
options.perMessageDeflate = false;
options.extraHeaders = {foo: 'bar'};
options.onlyBinaryUpgrades = true;
options.forceNode = false;
options.localAddress = 'localhost';
socket = client("ws://localhost:32767", options);
socket.on('close', (mes, detail) => {
console.log('CLOSE', mes, detail);
});
socket.on('error', (err) => {
console.log('ERROR', err);
});
server = engine.listen(8000, {pingTimeout: 100, pingInterval: 30});
server.on("connection", socket => {
socket.send('hello');
});
socket = client("ws://localhost:8000");
socket.on("message", (data) => {
console.log('MESSAGE', data);
});
socket.on('close', (mes, detail) => {
console.log('CLOSE', mes, detail);
});
socket.on('ping', () => {
console.log('client ping callback');
});
socket.on('pong', () => {
console.log('client pong callback');
});
socket.on('flush', () => {
console.log('client flush callback');
});
socket.on('drain', () => {
console.log('client drain callback');
});
socket.on("open", () => {
socket.send("hi", {compress: false}, () => {
console.log('client send callback');
setTimeout(() => {
socket.close();
setTimeout(() => {
server.close();
server.httpServer!.close();
}, 200);
}, 200);
});
});
+184
View File
@@ -0,0 +1,184 @@
// Type definitions for engine.io-client 3.1
// Project: https://github.com/socketio/engine.io-client
// Definitions by: KentarouTakeda <https://github.com/KentarouTakeda/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import http = require('http');
declare namespace client {
type Transport = 'polling'|'websocket';
type Message = string|ArrayBuffer|ArrayBufferView|Blob;
interface MessageOptions {
compress?: boolean;
}
interface SocketOptions {
/**
* http.Agent to use, defaults to false (NodeJS only)
*/
agent?: http.Agent|false;
/**
* defaults to true, whether the client should try to upgrade the transport from long-polling to something better.
*/
upgrade?: boolean;
/**
* forces JSONP for polling transport.
*/
forceJSONP?: boolean;
/**
* determines whether to use JSONP when necessary for polling.
* If disabled (by settings to false) an error will be emitted (saying "No transports available")
* if no other transports are available. If another transport is available
* for opening a connection (e.g. WebSocket) that transport will be used instead.
*/
jsonp?: boolean;
/**
* forces base 64 encoding for polling transport even when XHR2 responseType is available and WebSocket even if the used standard supports binary.
*/
forceBase64?: boolean;
/**
* enables XDomainRequest for IE8 to avoid loading bar flashing with click sound. default to false because XDomainRequest has a flaw of not sending cookie.
*/
enablesXDR?: boolean;
/**
* whether to add the timestamp with each transport request. Note: polling requests are always stamped unless this option is explicitly set to false (false)
*/
timestampRequests?: boolean;
/**
* timestamp parameter (t)
*/
timestampParam?: string;
/**
* port the policy server listens on (843)
*/
policyPort?: number;
/**
* path to connect to, default is /engine.io
*/
path?: string;
/**
* a list of transports to try (in order). Defaults to ['polling', 'websocket']. Engine always attempts to connect directly with the first one, provided the feature detection test for it passes.
*/
transports?: Transport[];
/**
* hash of options, indexed by transport name, overriding the common options for the given transport
*/
transportOptions?: {[key: string]: SocketOptions};
/**
* defaults to false. If true and if the previous websocket connection to the server succeeded,
* the connection attempt will bypass the normal upgrade process and will initially try websocket.
* A connection attempt following a transport error will use the normal upgrade process.
* It is recommended you turn this on only when using SSL/TLS connections,
* or if you know that your network does not block websockets.
*/
rememberUpgrade?: boolean;
/**
* Certificate, Private key and CA certificates to use for SSL. Can be used in Node.js client environment to manually specify certificate information.
*/
pfx?: string;
/**
* Private key to use for SSL. Can be used in Node.js client environment to manually specify certificate information.
*/
key?: string;
/**
* A string of passphrase for the private key or pfx. Can be used in Node.js client environment to manually specify certificate information.
*/
passphrase?: string;
/**
* Public x509 certificate to use. Can be used in Node.js client environment to manually specify certificate information.
*/
cert?: string;
/**
* An authority certificate or array of authority certificates to check the remote host against.
* Can be used in Node.js client environment to manually specify certificate information.
*/
ca?: string|string[];
/**
* A string describing the ciphers to use or exclude. Consult the cipher format list for details on the format.
* Can be used in Node.js client environment to manually specify certificate information.
*/
ciphers?: string;
/**
* If true, the server certificate is verified against the list of supplied CAs.
* An 'error' event is emitted if verification fails. Verification happens at the connection level,
* before the HTTP request is sent. Can be used in Node.js client environment to manually specify certificate information.
*/
rejectUnauthorized?: boolean;
/**
* parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable. (true)
*/
perMessageDeflate?: any;
/**
* Headers that will be passed for each request to the server (via xhr-polling and via websockets).
* These values then can be used during handshake or for special proxies. Can only be used in Node.js client environment.
*/
extraHeaders?: {[header: string]: string};
/**
* whether transport upgrades should be restricted to transports supporting binary data (false)
*/
onlyBinaryUpgrades?: boolean;
/**
* Uses NodeJS implementation for websockets - even if there is a native Browser-Websocket available,
* which is preferred by default over the NodeJS implementation. (This is useful when using hybrid platforms
* like nw.js or electron) (false, NodeJS only)
*/
forceNode?: boolean;
/**
* the local IP address to connect to
*/
localAddress?: string;
}
interface UpgradeError extends Error {
transport: string;
}
class Socket {
protocol?: number;
binaryType?: 'arraybuffer'|'blob';
/*
* open: Fired upon successful connection.
* flush: Fired upon completing a buffer flush
* drain: Fired after drain event of transport if writeBuffer is empty
* ping: Fired upon flushing a ping packet (ie: actual packet write out)
* pong: Fired upon receiving a pong packet.
*/
on(ev: 'open'|'flush'|'drain'|'ping'|'pong' , cb: () => void): this;
/*
* Fired when data is received from the server.
*/
on(ev: 'message' , cb: (data: string|ArrayBuffer) => void): this;
/*
* Fired upon disconnection. In compliance with the WebSocket API spec, this event may be fired even if the open event does not occur (i.e. due to connection error or close()).
*/
on(ev: 'close' , cb: (mes: string, detail?: Error) => void): this;
/*
* Fired when an error occurs.
*/
on(ev: 'error' , cb: (err: Error) => void): this;
/*
* Fired if an error occurs with a transport we're trying to upgrade to.
*/
on(ev: 'upgradeError' , cb: (err: UpgradeError) => void): this;
/*
* Fired upon upgrade success, after the new transport is set
*/
on(ev: 'upgrade' , cb: (transport: any) => void): this;
/**
* Sends a message to the server
*/
send(message: Message, opts?: MessageOptions, cb?: () => void): this;
/**
* Disconnects the client.
*/
close(): this;
}
}
declare function client(url?: string, opts?: client.SocketOptions): client.Socket;
export = client;
+25
View File
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"dom",
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"strict": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"engine.io-client-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }