Comparing version 5.1.1 to 5.2.0
@@ -207,4 +207,6 @@ 'use strict'; | ||
if (this.options.verifyClient.length === 2) { | ||
this.options.verifyClient(info, (verified, code, message) => { | ||
if (!verified) return abortHandshake(socket, code || 401, message); | ||
this.options.verifyClient(info, (verified, code, message, headers) => { | ||
if (!verified) { | ||
return abortHandshake(socket, code || 401, message, headers); | ||
} | ||
@@ -334,13 +336,18 @@ this.completeUpgrade(extensions, req, socket, head, cb); | ||
* @param {String} [message] The HTTP response body | ||
* @param {Object} [headers] Additional HTTP response headers | ||
* @private | ||
*/ | ||
function abortHandshake (socket, code, message) { | ||
function abortHandshake (socket, code, message, headers) { | ||
if (socket.writable) { | ||
message = message || http.STATUS_CODES[code]; | ||
headers = Object.assign({ | ||
'Connection': 'close', | ||
'Content-type': 'text/html', | ||
'Content-Length': Buffer.byteLength(message) | ||
}, headers); | ||
socket.write( | ||
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` + | ||
'Connection: close\r\n' + | ||
'Content-type: text/html\r\n' + | ||
`Content-Length: ${Buffer.byteLength(message)}\r\n` + | ||
'\r\n' + | ||
Object.keys(headers).map(h => `${h}: ${headers[h]}`).join('\r\n') + | ||
'\r\n\r\n' + | ||
message | ||
@@ -347,0 +354,0 @@ ); |
@@ -526,3 +526,3 @@ 'use strict'; | ||
options.handshakeTimeout, | ||
abortHandshake.bind(null, this, req, 'Opening handshake has timed out') | ||
() => abortHandshake(this, req, 'Opening handshake has timed out') | ||
); | ||
@@ -529,0 +529,0 @@ } |
{ | ||
"name": "ws", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", | ||
@@ -36,10 +36,10 @@ "keywords": [ | ||
"eslint-config-standard": "~11.0.0", | ||
"eslint-plugin-import": "~2.10.0", | ||
"eslint-plugin-import": "~2.12.0", | ||
"eslint-plugin-node": "~6.0.0", | ||
"eslint-plugin-promise": "~3.7.0", | ||
"eslint-plugin-standard": "~3.0.0", | ||
"mocha": "~5.0.0", | ||
"nyc": "~11.6.0", | ||
"mocha": "~5.2.0", | ||
"nyc": "~11.8.0", | ||
"utf-8-validate": "~4.0.0" | ||
} | ||
} |
108
README.md
@@ -32,5 +32,6 @@ # ws: a Node.js WebSocket library | ||
+ [Sending binary data](#sending-binary-data) | ||
+ [Server example](#server-example) | ||
+ [Broadcast example](#broadcast-example) | ||
+ [ExpressJS example](#expressjs-example) | ||
+ [Simple server](#simple-server) | ||
+ [External HTTP/S server](#external-https-server) | ||
+ [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server) | ||
+ [Server broadcast](#server-broadcast) | ||
+ [echo.websocket.org demo](#echowebsocketorg-demo) | ||
@@ -173,3 +174,3 @@ + [Other examples](#other-examples) | ||
### Server example | ||
### Simple server | ||
@@ -190,7 +191,68 @@ ```js | ||
### Broadcast example | ||
### External HTTP/S server | ||
```js | ||
const fs = require('fs'); | ||
const https = require('https'); | ||
const WebSocket = require('ws'); | ||
const server = new https.createServer({ | ||
cert: fs.readFileSync('/path/to/cert.pem'), | ||
key: fs.readFileSync('/path/to/key.pem') | ||
}); | ||
const wss = new WebSocket.Server({ server }); | ||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function incoming(message) { | ||
console.log('received: %s', message); | ||
}); | ||
ws.send('something'); | ||
}); | ||
server.listen(8080); | ||
``` | ||
### Multiple servers sharing a single HTTP/S server | ||
```js | ||
const http = require('http'); | ||
const WebSocket = require('ws'); | ||
const server = http.createServer(); | ||
const wss1 = new WebSocket.Server({ noServer: true }); | ||
const wss2 = new WebSocket.Server({ noServer: true }); | ||
wss1.on('connection', function connection(ws) { | ||
// ... | ||
}); | ||
wss2.on('connection', function connection(ws) { | ||
// ... | ||
}); | ||
server.on('upgrade', function upgrade(request, socket, head) { | ||
const pathname = url.parse(request.url).pathname; | ||
if (pathname === '/foo') { | ||
wss1.handleUpgrade(request, socket, head, function done(ws) { | ||
wss1.emit('connection', ws, request); | ||
}); | ||
} else if (pathname === '/bar') { | ||
wss2.handleUpgrade(request, socket, head, function done(ws) { | ||
wss2.emit('connection', ws, request); | ||
}); | ||
} else { | ||
socket.destroy(); | ||
} | ||
}); | ||
server.listen(8080); | ||
``` | ||
### Server broadcast | ||
```js | ||
const WebSocket = require('ws'); | ||
const wss = new WebSocket.Server({ port: 8080 }); | ||
@@ -219,36 +281,2 @@ | ||
### ExpressJS example | ||
```js | ||
const express = require('express'); | ||
const http = require('http'); | ||
const url = require('url'); | ||
const WebSocket = require('ws'); | ||
const app = express(); | ||
app.use(function (req, res) { | ||
res.send({ msg: "hello" }); | ||
}); | ||
const server = http.createServer(app); | ||
const wss = new WebSocket.Server({ server }); | ||
wss.on('connection', function connection(ws, req) { | ||
const location = url.parse(req.url, true); | ||
// You might use location.query.access_token to authenticate or share sessions | ||
// or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312) | ||
ws.on('message', function incoming(message) { | ||
console.log('received: %s', message); | ||
}); | ||
ws.send('something'); | ||
}); | ||
server.listen(8080, function listening() { | ||
console.log('Listening on %d', server.address().port); | ||
}); | ||
``` | ||
### echo.websocket.org demo | ||
@@ -330,3 +358,3 @@ | ||
wss.on('connection', function connection(ws, req) { | ||
const ip = req.headers['x-forwarded-for']; | ||
const ip = req.headers['x-forwarded-for'].split(/\s*,\s*/)[0]; | ||
}); | ||
@@ -333,0 +361,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2708
418
98827
14