Add files via upload

@types/websocket interfaces updated.
This commit is contained in:
Zhao Lei
2017-11-16 16:16:12 +08:00
committed by GitHub
parent 08da8b8a66
commit ae48e5684b
2 changed files with 68 additions and 7 deletions

View File

@@ -1,7 +1,8 @@
// Type definitions for websocket
// Project: https://github.com/theturtle32/WebSocket-Node
// Definitions by: Paul Loyd <https://github.com/loyd>,
// Kay Schecker <https://github.com/flynetworks>
// Kay Schecker <https://github.com/flynetworks>,
// Zhao Lei <https://github.com/zhaoleimxd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@@ -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;

View File

@@ -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<string> {
var interfaces: any = os.networkInterfaces();
var ipArray: Array<string> = [];
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();
}