Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "koa-ws", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"keywords": [ | ||
@@ -24,6 +24,9 @@ "koa", | ||
"scripts": { | ||
"test": "mocha --harmony" | ||
"test": "mocha --harmony", | ||
"test-cov": "node --harmony node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | node --harmony ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
}, | ||
"peerDependencies": { | ||
"koa": "^0.8.2" | ||
"config": { | ||
"travis-cov": { | ||
"threshold": 70 | ||
} | ||
}, | ||
@@ -38,3 +41,14 @@ "dependencies": { | ||
"ws": "^0.4.32" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"co-mocha": "0.0.4", | ||
"cov": "^0.1.3", | ||
"coveralls": "^2.11.1", | ||
"istanbul": "git://github.com/gotwarlost/istanbul.git#harmony", | ||
"koa": "^0.8.2", | ||
"mocha": "^1.21.4", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"travis-cov": "^0.2.5" | ||
} | ||
} |
@@ -1,3 +0,5 @@ | ||
# kao-ws | ||
[![Build Status](https://secure.travis-ci.org/mekwall/koa-ws.png)](http://travis-ci.org/mekwall/koa-ws) [![Coverage Status](https://img.shields.io/coveralls/mekwall/koa-ws.svg)](https://coveralls.io/r/mekwall/koa-ws) [![Dependency Status](https://david-dm.org/mekwall/koa-ws.png)](https://david-dm.org/mekwall/koa-ws) | ||
# koa-ws | ||
> Empower your koa.js application with realtime. | ||
@@ -45,3 +47,3 @@ | ||
koaws.emit('hello', function (err, result) { | ||
koaws.method('hello', function (err, result) { | ||
if (err) console.error('Something went wrong', err); | ||
@@ -90,2 +92,2 @@ console.log(result) // should log 'world!' | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
@@ -16,4 +16,6 @@ (function(){ | ||
hostname = WS_HOSTNAME; | ||
} else if (typeof location !== 'undefined') { | ||
hostname = location.hostname; | ||
} else { | ||
hostname = location.hostname; | ||
hostname = 'localhost'; | ||
} | ||
@@ -27,3 +29,8 @@ | ||
if (typeof WebSocket === 'undefined') { | ||
var WebSocket = require('ws'); | ||
} | ||
// Initialize WebSocket client | ||
debug('Connecting to server: ws://%s:%s', hostname, port); | ||
var client = new WebSocket('ws://' + hostname + ':' + port); | ||
@@ -45,42 +52,50 @@ | ||
client.addListener = client.on = function (type, cb) { | ||
if (callbacks[type]) { | ||
callbacks[type].push(cb); | ||
} else { | ||
callbacks[type] = [cb]; | ||
} | ||
}; | ||
if (!client.on) | ||
client.addListener = client.on = function (type, cb) { | ||
if (callbacks[type]) { | ||
callbacks[type].push(cb); | ||
} else { | ||
callbacks[type] = [cb]; | ||
} | ||
}; | ||
client.once = function (type, cb) { | ||
client.on(type, function onceFn () { | ||
client.off(type, onceFn); | ||
cb.apply(cb, arguments); | ||
}); | ||
}; | ||
if (!client.once) | ||
client.once = function (type, cb) { | ||
client.on(type, function onceFn () { | ||
client.off(type, onceFn); | ||
cb.apply(cb, arguments); | ||
}); | ||
}; | ||
client.removeListener = client.off = function (type, cb) { | ||
if (Array.isArray(callbacks[type])) { | ||
var idx = callbacks[type].indexOf(cb); | ||
if (idx !== -1) { | ||
if (callbacks[type].length === 1) { | ||
delete callbacks[type]; | ||
} else { | ||
callbacks[type].splice(idx, 1); | ||
if (!client.off) | ||
client.removeListener = client.off = function (type, cb) { | ||
if (Array.isArray(callbacks[type])) { | ||
var idx = callbacks[type].indexOf(cb); | ||
if (idx !== -1) { | ||
if (callbacks[type].length === 1) { | ||
delete callbacks[type]; | ||
} else { | ||
callbacks[type].splice(idx, 1); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
// Add helper handlers for the folowing events | ||
['open', 'close', 'message'] | ||
.forEach(function (type, i) { | ||
client['on' + type] = function () { | ||
for (var i = 0, l = callbacks[type].length; i < l; i++) { | ||
callbacks[type][i].apply(client, arguments); | ||
if (client._socket) { | ||
['open', 'close', 'message'] | ||
.forEach(function (type, i) { | ||
return; | ||
if (!client['on' + type]) { | ||
client['on' + type] = function () { | ||
for (var i = 0, l = callbacks[type].length; i < l; i++) { | ||
callbacks[type][i].apply(client, arguments); | ||
} | ||
}; | ||
} | ||
}; | ||
}); | ||
}); | ||
} | ||
// Emit a message | ||
client.emit = function () { | ||
// Call a method | ||
client.method = function () { | ||
var cb = null; | ||
@@ -126,3 +141,3 @@ var payload = { | ||
client.on('open', function (e) { | ||
debug('Webclient open'); | ||
debug('WebSocket open'); | ||
@@ -139,3 +154,3 @@ if (messageQueue.length) { | ||
client.on('message', function (e) { | ||
var payload = JSON.parse(e.data); | ||
var payload = JSON.parse(e.data || e); | ||
debug('Incoming message: %o', payload); | ||
@@ -142,0 +157,0 @@ if (payload.result && payload.id && awaitingResults[payload.id]) { |
@@ -19,5 +19,2 @@ var co = require('co'); | ||
// Create WebSocketServer | ||
this.server = new WebSocketServer({ server: app.server }); | ||
// Container for methods | ||
@@ -31,3 +28,10 @@ this.methods = {}; | ||
this.sessions = {}; | ||
} | ||
KoaWebSocketServer.prototype.listen = function (server) { | ||
// Create WebSocketServer | ||
this.server = new WebSocketServer({ | ||
server: server | ||
}); | ||
// Listen to connection | ||
@@ -129,3 +133,3 @@ this.server.on('connection', this.onConnection.bind(this)); | ||
if (typeof payload.params !== 'object') { | ||
if (payload.params && (typeof payload.params !== 'object' || !Array.isArray(payload.params))) { | ||
debug('Invalid params: %o', payload.params); | ||
@@ -213,4 +217,2 @@ socket.error(-32602, 'Invalid params'); | ||
module.exports = function (app, passedOptions) { | ||
@@ -229,6 +231,8 @@ // Default options | ||
app.server = oldListen.apply(app, arguments); | ||
app.ws = app.io = new KoaWebSocketServer(app, options); | ||
app.ws.listen(app.server); | ||
return app; | ||
}; | ||
app.ws = app.io = new KoaWebSocketServer(app, options); | ||
return function* (next) { | ||
@@ -242,3 +246,2 @@ if (options.serveClientFile && this.method === 'GET' && this.path === options.clientFilePath) { | ||
if (this.session && this.session.id) { | ||
console.log(app.ws); | ||
if (typeof app.ws.sockets[this.session.id] === 'undefined') { | ||
@@ -245,0 +248,0 @@ ws.sockets[this.session.id] = []; |
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
19722
7
12
439
92
9
3