koa-websocket
Advanced tools
Comparing version 0.1.2 to 1.0.0
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -11,8 +11,11 @@ const koa = require('koa'), | ||
app.ws.use(route.all('/test/:id', function* (next) { | ||
// `this` is the socket passed from WebSocketServer to connection listeners | ||
this.send('Hello World'); | ||
this.on('message', function(message) { | ||
// `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) { | ||
// do something with the message from client | ||
console.log(message); | ||
}); | ||
// yielding `next` will pass the socket on to the next ws middleware | ||
// yielding `next` will pass the context (this) on to the next ws middleware | ||
yield next; | ||
})); | ||
@@ -19,0 +22,0 @@ |
19
index.js
@@ -1,7 +0,9 @@ | ||
"use strict"; | ||
'use strict'; | ||
const WebSocketServer = require('ws').Server, | ||
debug = require('debug')('koa:websockets'), | ||
const url = require('url'), | ||
compose = require('koa-compose'), | ||
co = require('co'); | ||
co = require('co'), | ||
ws = require('ws'); | ||
const WebSocketServer = ws.Server; | ||
const debug = require('debug')('koa:websockets'); | ||
@@ -21,2 +23,3 @@ function KoaWebSocketServer (app) { | ||
KoaWebSocketServer.prototype.onConnection = function(socket) { | ||
debug('Connection received'); | ||
socket.on('error', function (err) { | ||
@@ -26,4 +29,8 @@ debug('Error occurred:', err); | ||
const fn = co.wrap(compose(this.middleware)); | ||
socket.path = socket.upgradeReq.url; | ||
fn.bind(socket).call().catch(function(err) { | ||
const context = this.app.createContext(socket.upgradeReq); | ||
context.websocket = socket; | ||
context.path = url.parse(socket.upgradeReq.url).pathname; | ||
fn.bind(context).call().catch(function(err) { | ||
debug(err); | ||
@@ -30,0 +37,0 @@ }); |
{ | ||
"name": "koa-websocket", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.", | ||
@@ -33,6 +33,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"koa": "^0.18.1", | ||
"koa": "^0.21.0", | ||
"koa-route": "^2.4.0", | ||
"mocha": "~2.1.0" | ||
"mocha": "~2.2.5" | ||
} | ||
} |
# koa-websocket | ||
[![Build Status](https://travis-ci.org/kudos/koa-websocket.svg?branch=master)](https://travis-ci.org/kudos/koa-websocket) | ||
[![Circle CI](https://circleci.com/gh/kudos/koa-websocket.svg?style=svg)](https://circleci.com/gh/kudos/koa-websocket) | ||
@@ -20,8 +20,11 @@ ## Installation | ||
app.ws.use(route.all('/test/:id', function* (next) { | ||
this.send('Hello World'); | ||
// `this` is the socket passed from WebSocketServer to connection listeners | ||
this.on('message', function(message) { | ||
// `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) { | ||
// do something with the message from client | ||
console.log(message); | ||
}); | ||
// yielding `next` will pass the socket on to the next ws middleware | ||
// yielding `next` will pass the context (this) on to the next ws middleware | ||
yield next; | ||
})); | ||
@@ -31,2 +34,2 @@ | ||
``` | ||
``` |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'), | ||
@@ -10,11 +12,11 @@ WebSocket = require('ws'), | ||
var app = koaws(koa()); | ||
app.ws.use(route.all('/abc', function*(next) { | ||
this.on('message', function(message) { | ||
this.send(message); | ||
app.ws.use(route.all('/abc', function*() { | ||
this.websocket.on('message', function(message) { | ||
this.websocket.send(message); | ||
}.bind(this)); | ||
})); | ||
app.ws.use(route.all('/def', function*(next) { | ||
this.on('message', function(message) { | ||
this.send(message); | ||
app.ws.use(route.all('/def', function*() { | ||
this.websocket.on('message', function(message) { | ||
this.websocket.send(message); | ||
}.bind(this)); | ||
@@ -25,6 +27,6 @@ })); | ||
it("sends abc message to abc route", function(done){ | ||
it('sends abc message to abc route', function(done){ | ||
var ws = new WebSocket('ws://localhost:' + server.address().port + '/abc'); | ||
ws.on('open', function() { | ||
ws.send("abc"); | ||
ws.send('abc'); | ||
}); | ||
@@ -37,6 +39,6 @@ ws.on('message', function(message) { | ||
it("sends def message to def route", function(done){ | ||
it('sends def message to def route', function(done){ | ||
var ws = new WebSocket('ws://localhost:' + server.address().port + '/def'); | ||
ws.on('open', function() { | ||
ws.send("def"); | ||
ws.send('def'); | ||
}); | ||
@@ -48,2 +50,13 @@ ws.on('message', function(message) { | ||
}); | ||
it('handles urls with query parameters', function(done){ | ||
var ws = new WebSocket('ws://localhost:' + server.address().port + '/abc?foo=bar'); | ||
ws.on('open', function() { | ||
ws.send('abc'); | ||
}); | ||
ws.on('message', function(message) { | ||
assert(message === 'abc'); | ||
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5883
10
113
0
34