add protocol, fix verifyClient callback defination

This commit is contained in:
Zhuohuan LI
2016-07-24 20:19:34 +08:00
parent 7d6547a8a0
commit f7163677f8
2 changed files with 33 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
/// <reference path="ws.d.ts" />
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)
})
}

12
ws/ws.d.ts vendored
View File

@@ -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;