promise-ws
Advanced tools
Comparing version 0.0.0 to 0.1.0
@@ -29,3 +29,3 @@ 'use strict'; | ||
class Client { | ||
class Client extends _events2.default { | ||
static create(address) { | ||
@@ -83,2 +83,5 @@ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
super(); | ||
const onClose = options.onClose, | ||
@@ -148,2 +151,17 @@ onOpen = options.onOpen, | ||
} | ||
const forward = eventType => { | ||
ws.on(eventType, this.emit.bind(this, eventType)); | ||
}; | ||
this.on('error', _utils.noop); | ||
forward('open'); | ||
forward('close'); | ||
forward('headers'); | ||
forward('error'); | ||
forward('message'); | ||
forward('ping'); | ||
forward('pong'); | ||
forward('unexpected-response'); | ||
} | ||
@@ -150,0 +168,0 @@ |
@@ -13,2 +13,6 @@ 'use strict'; | ||
var _events = require('events'); | ||
var _events2 = _interopRequireDefault(_events); | ||
var _pify = require('pify'); | ||
@@ -22,2 +26,4 @@ | ||
var _utils = require('./utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -27,3 +33,3 @@ | ||
class Server { | ||
class Server extends _events2.default { | ||
static create(options) { | ||
@@ -44,2 +50,4 @@ return _asyncToGenerator(function* () { | ||
constructor(options, callback) { | ||
super(); | ||
const wss = new _ws2.default.Server(_extends({}, options, { | ||
@@ -90,2 +98,12 @@ clientTracking: true | ||
wss.on('error', callback); | ||
const forward = eventType => { | ||
wss.on(eventType, this.emit.bind(this, eventType)); | ||
}; | ||
this.on('error', _utils.noop); | ||
forward('connection'); | ||
forward('error'); | ||
forward('headers'); | ||
} | ||
@@ -92,0 +110,0 @@ |
@@ -7,4 +7,7 @@ 'use strict'; | ||
exports.isFunction = isFunction; | ||
exports.noop = noop; | ||
function isFunction(target) { | ||
return typeof target === 'function'; | ||
} | ||
} | ||
function noop() {} |
{ | ||
"name": "promise-ws", | ||
"description": "A Promise-Based WebSocket implementation for Node.js", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"main": "lib/index", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -18,2 +18,3 @@ # promise-ws | ||
- [Server.create\(options\)](#servercreateoptions) | ||
- [Server Events](#server-events) | ||
- [server#onReply\(name, response\)](#serveronreplyname-response) | ||
@@ -31,2 +32,3 @@ - [server#addReply\(name, response\)](#serveraddreplyname-response) | ||
- [Client.autoReconnect\(address, waitUntil\[, delay\]\)](#clientautoreconnectaddress-waituntil-delay) | ||
- [Client Events](#client-events) | ||
- [client#onReply\(name, response\)](#clientonreplyname-response) | ||
@@ -54,3 +56,2 @@ - [client#addReply\(name, response\)](#clientaddreplyname-response) | ||
const server = await Server.create({ port }); | ||
const client = await Client.create(`ws://127.0.0.1:${port}`); | ||
server.reply('say', async (data) => { | ||
@@ -60,4 +61,8 @@ console.log('data'); /* 'hello' */ | ||
}); | ||
const response = await client.request('say', 'hello'); | ||
console.log(response); /* 'world' */ | ||
const url = `ws://127.0.0.1:${port}`; | ||
await Client.autoReconnect(url, async (client) => { | ||
const response = await client.request('say', 'hello'); | ||
console.log(response); /* 'world' */ | ||
}); | ||
}()); | ||
@@ -92,2 +97,12 @@ ``` | ||
<a name="server-events"></a> | ||
### Server Events | ||
Server extends [EventEmitter](https://nodejs.org/api/events.html), which forwards these events from [WebSocket.Server](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocketserver): | ||
- [connection](https://github.com/websockets/ws/blob/master/doc/ws.md#event-connection) | ||
- [error](https://github.com/websockets/ws/blob/master/doc/ws.md#event-error) | ||
- [headers](https://github.com/websockets/ws/blob/master/doc/ws.md#event-headers) | ||
<a name="serveronreplyname-response"></a> | ||
@@ -232,5 +247,5 @@ ### server#onReply(name, response) | ||
/* do something... */ | ||
return 'response data'; | ||
return 'chris'; | ||
}); | ||
console.log('res', res); /* will be 'response data' */ | ||
console.log('res:', res); /* res: chris */ | ||
} | ||
@@ -269,5 +284,5 @@ catch (err) { | ||
/* do something... */ | ||
return 'response data'; | ||
return 'chris'; | ||
}); | ||
console.log('res', res); /* will be 'response data' */ | ||
console.log('res:', res); /* res: chris */ | ||
} | ||
@@ -280,3 +295,17 @@ catch (err) { | ||
<a name="client-events"></a> | ||
### Client Events | ||
Client extends [EventEmitter](https://nodejs.org/api/events.html), which forwards these events from [WebSocket.Client](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket): | ||
- [close](https://github.com/websockets/ws/blob/master/doc/ws.md#event-close) | ||
- [error](https://github.com/websockets/ws/blob/master/doc/ws.md#event-error-1) | ||
- [headers](https://github.com/websockets/ws/blob/master/doc/ws.md#event-headers-1) | ||
- [message](https://github.com/websockets/ws/blob/master/doc/ws.md#event-message) | ||
- [open](https://github.com/websockets/ws/blob/master/doc/ws.md#event-open) | ||
- [ping](https://github.com/websockets/ws/blob/master/doc/ws.md#event-ping) | ||
- [pong](https://github.com/websockets/ws/blob/master/doc/ws.md#event-pong) | ||
- [unexpected-response](https://github.com/websockets/ws/blob/master/doc/ws.md#event-unexpected-response) | ||
<a name="clientonreplyname-response"></a> | ||
@@ -283,0 +312,0 @@ ### client#onReply(name, response) |
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
25963
389
375