koa-websocket
Advanced tools
Comparing version 2.0.0 to 3.0.1
# Changelog | ||
Note: koa-websockets follows semver. | ||
Note: koa-websocket follows semver. | ||
## v3.0.1 | ||
Because NPM wouldn't let me retroactively tag 3.0.0 as `next` instead of latest. | ||
## v3.0.0 | ||
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. | ||
### Improvements | ||
* Koa 2 comptiblity. | ||
## v2.0.0 | ||
@@ -6,0 +18,0 @@ |
@@ -33,3 +33,3 @@ 'use strict'; | ||
fn.bind(context).call().catch(function(err) { | ||
fn(context).catch(function(err) { | ||
debug(err); | ||
@@ -36,0 +36,0 @@ }); |
{ | ||
"name": "koa-websocket", | ||
"version": "2.0.0", | ||
"version": "3.0.1", | ||
"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": "^2.3.0", | ||
"koa-compose": "^3.0.0", | ||
"ws": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"koa": "^1.0.0", | ||
"koa-route": "^2.4.0", | ||
"koa": "^2.0.0-alpha.7", | ||
"koa-route": "^3.2.0", | ||
"mocha": "^2.5.3" | ||
} | ||
} |
@@ -5,5 +5,7 @@ # 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. | ||
## Installation | ||
`npm install koa-websocket` | ||
`npm install koa-websocket@next` | ||
@@ -13,19 +15,24 @@ ## Usage | ||
```js | ||
const koa = require('koa'), | ||
const Koa = require('koa'), | ||
route = require('koa-route'), | ||
websockify = require('koa-websocket'); | ||
const app = websockify(koa()); | ||
const app = websockify(new Koa()); | ||
// Regular middleware | ||
// Note it's app.ws.use and not app.use | ||
app.ws.use(route.all('/test/:id', function* (next) { | ||
// `this` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object. | ||
// the websocket is added to the context on `this.websocket`. | ||
this.websocket.send('Hello World'); | ||
this.websocket.on('message', function(message) { | ||
app.ws.use(function(ctx, next) { | ||
// return `next` to pass the context (ctx) on to the next ws middleware | ||
return next(ctx); | ||
}); | ||
// Using routes | ||
app.ws.use(route.all('/test/:id', function (ctx) { | ||
// `ctx` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object. | ||
// the websocket is added to the context on `ctx.websocket`. | ||
ctx.websocket.send('Hello World'); | ||
ctx.websocket.on('message', function(message) { | ||
// do something with the message from client | ||
console.log(message); | ||
}); | ||
// yielding `next` will pass the context (this) on to the next ws middleware | ||
yield next; | ||
})); | ||
@@ -32,0 +39,0 @@ |
@@ -5,3 +5,3 @@ 'use strict'; | ||
WebSocket = require('ws'), | ||
koa = require('koa'), | ||
Koa = require('koa'), | ||
route = require('koa-route'); | ||
@@ -12,17 +12,44 @@ | ||
describe('should route ws messages seperately', function() { | ||
var app = koaws(koa()); | ||
app.ws.use(route.all('/abc', function*() { | ||
this.websocket.on('message', function(message) { | ||
this.websocket.send(message); | ||
}.bind(this)); | ||
var app = koaws(new Koa()); | ||
app.ws.use(function(ctx, next){ | ||
ctx.websocket.on('message', function(message) { | ||
if (message == '123') { | ||
ctx.websocket.send(message); | ||
} | ||
}); | ||
return next(); | ||
}); | ||
app.ws.use(route.all('/abc', function(ctx){ | ||
ctx.websocket.on('message', function(message) { | ||
ctx.websocket.send(message); | ||
}); | ||
})); | ||
app.ws.use(route.all('/def', function*() { | ||
this.websocket.on('message', function(message) { | ||
this.websocket.send(message); | ||
}.bind(this)); | ||
app.ws.use(route.all('/abc', function(ctx){ | ||
ctx.websocket.on('message', function(message) { | ||
ctx.websocket.send(message); | ||
}); | ||
})); | ||
app.ws.use(route.all('/def', function(ctx){ | ||
ctx.websocket.on('message', function(message) { | ||
ctx.websocket.send(message); | ||
}); | ||
})); | ||
var server = app.listen(); | ||
it('sends 123 message to any route', function(done){ | ||
var ws = new WebSocket('ws://localhost:' + server.address().port + '/not-a-route'); | ||
ws.on('open', function() { | ||
ws.send('123'); | ||
}); | ||
ws.on('message', function(message) { | ||
assert(message === '123'); | ||
done(); | ||
}); | ||
}); | ||
it('sends abc message to abc route', function(done){ | ||
@@ -61,1 +88,2 @@ var ws = new WebSocket('ws://localhost:' + server.address().port + '/abc'); | ||
}); | ||
; |
Sorry, the diff of this file is not supported yet
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
7436
151
41
+ Addedany-promise@1.3.0(transitive)
+ Addedkoa-compose@3.2.1(transitive)
- Removedkoa-compose@2.5.1(transitive)
Updatedkoa-compose@^3.0.0