koa-websocket
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -5,2 +5,8 @@ # Changelog | ||
## v4.0.0 | ||
* Switch Koa v2 support to master, and moved v1 to the legacy branch. | ||
* Added ability to set websocket options. | ||
* Upgraded to websocket 2.x. | ||
## v3.0.1 | ||
@@ -7,0 +13,0 @@ |
18
index.js
@@ -15,6 +15,4 @@ 'use strict'; | ||
KoaWebSocketServer.prototype.listen = function (server) { | ||
this.server = new WebSocketServer({ | ||
server: server | ||
}); | ||
KoaWebSocketServer.prototype.listen = function (options) { | ||
this.server = new WebSocketServer(options); | ||
this.server.on('connection', this.onConnection.bind(this)); | ||
@@ -44,3 +42,3 @@ }; | ||
module.exports = function (app) { | ||
module.exports = function (app, wsOptions) { | ||
const oldListen = app.listen; | ||
@@ -50,3 +48,11 @@ app.listen = function () { | ||
app.server = oldListen.apply(app, arguments); | ||
app.ws.listen(app.server); | ||
const options = { server: app.server}; | ||
if (wsOptions) { | ||
for (var key in wsOptions) { | ||
if (wsOptions.hasOwnProperty(key)) { | ||
options[key] = wsOptions[key]; | ||
} | ||
} | ||
} | ||
app.ws.listen(options); | ||
return app.server; | ||
@@ -53,0 +59,0 @@ }; |
{ | ||
"name": "koa-websocket", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.", | ||
@@ -29,10 +29,10 @@ "main": "index.js", | ||
"debug": "^2.1.2", | ||
"koa-compose": "^3.0.0", | ||
"ws": "^1.1.0" | ||
"koa-compose": "^4.0.0", | ||
"ws": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"koa": "^2.0.0-alpha.7", | ||
"koa": "^2.2.0", | ||
"koa-route": "^3.2.0", | ||
"mocha": "^2.5.3" | ||
"mocha": "^3.3.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # koa-websocket | ||
> This is a pre-release for compatibility with Koa 2. Doing `npm install koa-websocket` will continue to give you 2.x until Koa 2 is stable. | ||
> Koa v2 is now the default. For Koa v1 support install with koa-websocket@2 and see the `legacy` branch. | ||
@@ -35,2 +35,24 @@ ## Installation | ||
// do something with the message from client | ||
console.log(message); | ||
}); | ||
})); | ||
app.listen(3000); | ||
``` | ||
With custom websocket options. | ||
``` | ||
const Koa = require('koa'), | ||
route = require('koa-route'), | ||
websockify = require('koa-websocket'); | ||
const wsOptions = {}; | ||
const app = websockify(new Koa(), wsOptions); | ||
app.ws.use(route.all('/', function* (ctx) { | ||
// the websocket is added to the context as `this.websocket`. | ||
ctx.websocket.on('message', function(message) { | ||
// print message from the client | ||
console.log(message); | ||
@@ -37,0 +59,0 @@ }); |
@@ -11,3 +11,9 @@ 'use strict'; | ||
describe('should route ws messages seperately', function() { | ||
var app = koaws(new Koa()); | ||
const app = koaws(new Koa(), { | ||
handleProtocols: function (protocols) { | ||
if (protocols.indexOf("bad_protocol") !== -1) | ||
return false; | ||
return protocols.pop(); | ||
} | ||
}); | ||
@@ -86,3 +92,18 @@ app.ws.use(function(ctx, next){ | ||
}); | ||
it('reject bad protocol use wsOptions', function(done){ | ||
var ws = new WebSocket('ws://localhost:' + server.address().port + '/abc', ['bad_protocol']); | ||
ws.on('open', function() { | ||
ws.send('abc'); | ||
}); | ||
ws.on('message', function(message) { | ||
assert(false); | ||
done(); | ||
}); | ||
ws.on('unexpected-response', function() { | ||
assert(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
; |
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
8773
177
63
+ Addedkoa-compose@4.2.0(transitive)
+ Addedsafe-buffer@5.0.1(transitive)
+ Addedultron@1.1.1(transitive)
+ Addedws@2.3.1(transitive)
- Removedany-promise@1.3.0(transitive)
- Removedkoa-compose@3.2.1(transitive)
- Removedoptions@0.0.6(transitive)
- Removedultron@1.0.2(transitive)
- Removedws@1.1.5(transitive)
Updatedkoa-compose@^4.0.0
Updatedws@^2.3.1