From f7163677f8c8436bac263beb455407351cf25686 Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Sun, 24 Jul 2016 20:19:34 +0800 Subject: [PATCH 1/3] add `protocol`, fix verifyClient callback defination --- ws/ws-tests.ts | 27 ++++++++++++++++++++++++++- ws/ws.d.ts | 12 +++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/ws/ws-tests.ts b/ws/ws-tests.ts index ec2bc431e0..0c1887527e 100644 --- a/ws/ws-tests.ts +++ b/ws/ws-tests.ts @@ -1,6 +1,9 @@ /// import WebSocket = require('ws'); +import http = require('http'); +// import * as http from 'http' + var WebSocketServer = WebSocket.Server; { @@ -29,7 +32,7 @@ var WebSocketServer = WebSocket.Server; { var wss = new WebSocketServer({port: 8080}); - function broadcast(data: any) { + const broadcast = function(data: any) { for(var i in wss.clients) wss.clients[i].send(data); }; @@ -51,3 +54,25 @@ var WebSocketServer = WebSocket.Server; }, 500); }); } + +{ + + const verifyClient = function( + info: { + origin: string + secure: boolean + req: http.ServerRequest + } + , callback: (res: boolean) => void + ): void { + callback(true) + } + + var wsv = new WebSocketServer({ + verifyClient + }) + + wsv.on('connection', function connection(ws) { + console.log(ws.protocol) + }) +} \ No newline at end of file diff --git a/ws/ws.d.ts b/ws/ws.d.ts index d9a1338d21..235b538030 100644 --- a/ws/ws.d.ts +++ b/ws/ws.d.ts @@ -22,6 +22,7 @@ declare module "ws" { url: string; supports: any; upgradeReq: http.ServerRequest; + protocol: string; CONNECTING: number; OPEN: number; @@ -91,11 +92,12 @@ declare module "ws" { host?: string; port?: number; server?: http.Server; - verifyClient?: { - (info: {origin: string; secure: boolean; req: http.ServerRequest}): boolean; - (info: {origin: string; secure: boolean; req: http.ServerRequest}, - callback: (res: boolean) => void): void; - }; + verifyClient?: ( + ((info: {origin: string; secure: boolean; req: http.ServerRequest}) => boolean) + | + ((info: {origin: string; secure: boolean; req: http.ServerRequest}, + callback: (res: boolean) => void) => void) + ); handleProtocols?: any; path?: string; noServer?: boolean; From c32177d0cdfd4e04102a845925bd012c7f33e3fd Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Sun, 24 Jul 2016 20:21:53 +0800 Subject: [PATCH 2/3] type alias for 2 callback, fix #10250 --- ws/ws.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ws/ws.d.ts b/ws/ws.d.ts index 235b538030..6b94bee19d 100644 --- a/ws/ws.d.ts +++ b/ws/ws.d.ts @@ -88,16 +88,16 @@ declare module "ws" { } namespace WebSocket { + + type VerifyClientCallbackSync = (info: {origin: string; secure: boolean; req: http.ServerRequest}) => boolean; + type VerifyClientCallbackAsync = (info: {origin: string; secure: boolean; req: http.ServerRequest} + , callback: (res: boolean) => void) => void; + export interface IServerOptions { host?: string; port?: number; server?: http.Server; - verifyClient?: ( - ((info: {origin: string; secure: boolean; req: http.ServerRequest}) => boolean) - | - ((info: {origin: string; secure: boolean; req: http.ServerRequest}, - callback: (res: boolean) => void) => void) - ); + verifyClient?: VerifyClientCallbackAsync | VerifyClientCallbackSync; handleProtocols?: any; path?: string; noServer?: boolean; From 6a6f78076bcc98d7c4150d1b294ae06a1f04d69c Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Sun, 24 Jul 2016 20:25:36 +0800 Subject: [PATCH 3/3] work with --target es6 --- ws/ws-tests.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ws/ws-tests.ts b/ws/ws-tests.ts index 0c1887527e..a8afa8cb99 100644 --- a/ws/ws-tests.ts +++ b/ws/ws-tests.ts @@ -1,8 +1,7 @@ /// -import WebSocket = require('ws'); -import http = require('http'); -// import * as http from 'http' +import * as WebSocket from 'ws'; +import * as http from'http'; var WebSocketServer = WebSocket.Server; @@ -56,7 +55,6 @@ var WebSocketServer = WebSocket.Server; } { - const verifyClient = function( info: { origin: string