From ae48e5684b31a05782cfa0dad44287cc83d896f0 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Thu, 16 Nov 2017 16:16:12 +0800 Subject: [PATCH] Add files via upload @types/websocket interfaces updated. --- types/websocket/index.d.ts | 11 ++--- types/websocket/websocket-tests.ts | 64 +++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/types/websocket/index.d.ts b/types/websocket/index.d.ts index 3b41a5f6da..ab4e453e80 100644 --- a/types/websocket/index.d.ts +++ b/types/websocket/index.d.ts @@ -1,7 +1,8 @@ // Type definitions for websocket // Project: https://github.com/theturtle32/WebSocket-Node // Definitions by: Paul Loyd , -// Kay Schecker +// Kay Schecker , +// Zhao Lei // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -563,10 +564,10 @@ declare class client extends events.EventEmitter { * any scripting content that caused the connection to be requested. * @param requestUrl should be a standard websocket url */ - connect(requestUrl: url.Url, protocols?: string[], origin?: string, headers?: any[]): void; - connect(requestUrl: string, protocols?: string[], origin?: string, headers?: any[]): void; - connect(requestUrl: url.Url, protocols?: string, origin?: string, headers?: any[]): void; - connect(requestUrl: string, protocols?: string, origin?: string, headers?: any[]): void; + connect(requestUrl: url.Url, protocols?: string[], origin?: string, headers?: any[], extraRequestOptions?: http.RequestOptions): void; + connect(requestUrl: string, protocols?: string[], origin?: string, headers?: any[], extraRequestOptions?: http.RequestOptions): void; + connect(requestUrl: url.Url, protocols?: string, origin?: string, headers?: any[], extraRequestOptions?: http.RequestOptions): void; + connect(requestUrl: string, protocols?: string, origin?: string, headers?: any[], extraRequestOptions?: http.RequestOptions): void; // Events on(event: string, listener: () => void): this; diff --git a/types/websocket/websocket-tests.ts b/types/websocket/websocket-tests.ts index 5ecee0080d..3044f9bf7d 100644 --- a/types/websocket/websocket-tests.ts +++ b/types/websocket/websocket-tests.ts @@ -1,8 +1,10 @@ import websocket = require('websocket'); import http = require('http'); +import os = require('os'); -{ +/* +function serverTest() { var server = http.createServer((req, res) => { console.log((new Date()) + ' Received request for ' + req.url); res.writeHead(404); @@ -88,7 +90,7 @@ import http = require('http'); wsRouter.unmount(/^route\/[a-zA-Z]+$/, 'protocol'); } -{ +function clientTest() { var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); @@ -124,4 +126,62 @@ import http = require('http'); }); client.connect('ws://localhost:8080/', 'echo-protocol'); +}*/ + +function getLocalIpArray(): Array { + var interfaces: any = os.networkInterfaces(); + var ipArray: Array = []; + for (var dev in interfaces) { + for (var i = 0; i < interfaces[dev].length; i++) { + if (interfaces[dev][i].family == "IPv4" && interfaces[dev][i].internal == false) { + ipArray.push(interfaces[dev][i].address); + } + } + } + return ipArray; +} + +function serverTest2() { + var server = http.createServer((req, rsp) => { + rsp.writeHead(200); + rsp.end("Hello, world!"); + }); + server.listen(8888); + + var wsServer = new websocket.server({ + httpServer: server, + autoAcceptConnections: true + }); + + wsServer.on("connect", (conn) => { + conn.sendUTF(`Your IP Address is - ${conn.remoteAddress}`) + }); +} + +function clientTest2() { + var ipArray = getLocalIpArray(); + + var client = new websocket.client(); + client.on("connect", (conn) => { + console.log(`on connect`); + conn.on("frame", (frame) => { + console.log(`on frame - ${frame.binaryPayload.toString()}`); + }); + conn.on("message", (data) => { + console.log(`on message - ${data.utf8Data}`); + }); + }); + client.on("connectFailed", (err) => { + console.log(`on failed: ${err}`); + }); + client.connect(`ws://${ipArray[0]}:8888`, undefined, undefined, undefined, { + localAddress: ipArray[0] + }); + +} + +{ + console.log(`websocket test start.`); + serverTest2(); + clientTest2(); }