rpc-websockets
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -44,4 +44,5 @@ { | ||
"no-duplicate-imports": [ "error" ], | ||
"prefer-const": [ "error" ] | ||
"prefer-const": [ "error" ], | ||
"no-cond-assign": 0 | ||
} | ||
} |
@@ -73,14 +73,14 @@ /** | ||
function Client() { | ||
var address = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "ws://localhost:8080/rpc/1.0"; | ||
var address = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "ws://localhost:8080"; | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$autoconnect = _ref.autoconnect, | ||
autoconnect = _ref$autoconnect === undefined ? true : _ref$autoconnect, | ||
_ref$reconnect = _ref.reconnect, | ||
reconnect = _ref$reconnect === undefined ? true : _ref$reconnect, | ||
_ref$reconnect_interv = _ref.reconnect_interval, | ||
reconnect_interval = _ref$reconnect_interv === undefined ? 1000 : _ref$reconnect_interv, | ||
_ref$max_reconnects = _ref.max_reconnects, | ||
max_reconnects = _ref$max_reconnects === undefined ? 5 : _ref$max_reconnects; | ||
var _ref$autoconnect = _ref.autoconnect; | ||
var autoconnect = _ref$autoconnect === undefined ? true : _ref$autoconnect; | ||
var _ref$reconnect = _ref.reconnect; | ||
var reconnect = _ref$reconnect === undefined ? true : _ref$reconnect; | ||
var _ref$reconnect_interv = _ref.reconnect_interval; | ||
var reconnect_interval = _ref$reconnect_interv === undefined ? 1000 : _ref$reconnect_interv; | ||
var _ref$max_reconnects = _ref.max_reconnects; | ||
var max_reconnects = _ref$max_reconnects === undefined ? 5 : _ref$max_reconnects; | ||
(0, _classCallCheck3.default)(this, Client); | ||
@@ -87,0 +87,0 @@ |
@@ -109,2 +109,5 @@ /** | ||
// stores all connected sockets in the appropriate namespace | ||
_this.namespaces = new _map2.default(); | ||
// stores all events as keys and subscribed users in array as value | ||
@@ -118,3 +121,3 @@ _this.events = {}; | ||
_this.wss.on("connection", function (socket) { | ||
var path = _url2.default.parse(socket.upgradeReq.url, true).pathname.split("/"); | ||
var ns = _url2.default.parse(socket.upgradeReq.url).pathname; | ||
socket._id = _uuid2.default.v1(); | ||
@@ -154,11 +157,10 @@ | ||
// use an RPC handler if rpc request | ||
if (options.rpc && "/" + path[1] === options.rpc.root_path) { | ||
if (path[2] !== options.rpc.version) return socket.close(404); | ||
// store socket | ||
_this.clients.set(socket._id, socket); | ||
// store socket | ||
_this.clients.set(socket._id, socket); | ||
if (!_this.namespaces.has(ns)) _this.namespaces.set(ns, new Array()); | ||
return _this._handleRPC(socket); | ||
} | ||
_this.namespaces.get(ns).push(socket._id); | ||
return _this._handleRPC(socket); | ||
}); | ||
@@ -212,2 +214,6 @@ | ||
// TODO: NAPRAVI novu funkciju 'of' koja prima ns name kao argument i | ||
// vraca neki objekt koji ima funkciju 'emit' koja radi samo na | ||
// klijentima iz tog ns-a | ||
// forward emitted event to subscribers | ||
@@ -250,2 +256,46 @@ this.on(name, function () { | ||
/** | ||
* Returns a requested namespace object | ||
* @method | ||
* @param {String} name - namespace identifier | ||
* @throws {TypeError} | ||
* @return {Namespace} | ||
*/ | ||
}, { | ||
key: "of", | ||
value: function of(name) { | ||
(0, _assertArgs2.default)(arguments, { | ||
"name": "string" | ||
}); | ||
var self = this; | ||
return { | ||
emit: function emit(event, params) { | ||
var socket_ids = self.namespaces.get(name); | ||
for (var i = 0, id; id = socket_ids[i]; ++i) { | ||
self.clients.get(id).send((0, _stringify2.default)({ | ||
notification: event, | ||
params: params || [] | ||
})); | ||
} | ||
}, | ||
name: name, | ||
connected: function connected() { | ||
var clients = {}; | ||
var socket_ids = self.namespaces.get(name); | ||
for (var i = 0, id; id = socket_ids[i]; ++i) { | ||
clients[id] = self.clients.get(id); | ||
}return clients; | ||
}, | ||
clients: function clients() { | ||
return self.namespaces.get(name); | ||
} | ||
}; | ||
} | ||
/** | ||
* Lists all created events. | ||
@@ -252,0 +302,0 @@ * @method |
{ | ||
"name": "rpc-websockets", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "JSON-RPC 2.0 implementation over WebSockets for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -26,7 +26,3 @@ [![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/qaap/rpc-websockets/blob/master/LICENSE) | ||
port: 8080, | ||
host: 'localhost', | ||
rpc: { | ||
root_path: '/rpc', | ||
version: '1.0' | ||
} | ||
host: 'localhost' | ||
}) | ||
@@ -52,3 +48,3 @@ | ||
// instantiate Client and connect to an RPC server | ||
var ws = new WebSocket('ws://localhost:8080/rpc/1.0') | ||
var ws = new WebSocket('ws://localhost:8080') | ||
@@ -83,3 +79,3 @@ ws.on('open', function() { | ||
var WebSocket = require('rpc-websockets').Client | ||
var ws = new WebSocket('ws://localhost:8080/rpc/1.0') | ||
var ws = new WebSocket('ws://localhost:8080') | ||
``` | ||
@@ -92,3 +88,3 @@ | ||
Parameters: | ||
* `address` {String}: The URL of the WebSocket server. Defaults to 'ws://localhost:8080/rpc/1.0'. | ||
* `address` {String}: The URL of the WebSocket server. Defaults to 'ws://localhost:8080'. | ||
* `options` {Object}: Client options that are also forwarded to `ws`. | ||
@@ -173,7 +169,3 @@ * `autoconnect` {Boolean}: Client autoconnect upon Client class instantiation. Defaults to `true`. | ||
port: 8080, | ||
host: 'localhost', | ||
rpc: { | ||
root_path: '/rpc', | ||
version: '1.0' | ||
} | ||
host: 'localhost' | ||
}) | ||
@@ -190,5 +182,2 @@ ``` | ||
* `host` {String}: Address on which the server will listen for incoming requests. | ||
* `rpc` {Object}: | ||
* `root_path` {String}: RPC server endpoint. | ||
* `version` {String}: RPC API version. | ||
@@ -224,2 +213,12 @@ Once the Server class is instantiated, you can use a `ws` library's instance via server.wss object. | ||
### server.of(name) | ||
Returns a Namespace object initialized by the provided pathname upon connecting(eg: ```/chat```). | ||
Defaults to ```/```. | ||
Parameters: | ||
* `name` {String}: Namespace identifier. | ||
More information on Namespaces below. | ||
### server.createError(code, message[, data]) -> Object | ||
@@ -247,1 +246,24 @@ | ||
Emits when a server error is raised. | ||
## Namespaces | ||
Namespace represents a pool of sockets connected under a given scope identified by a pathname (eg: ```/chat```). Basically borrows ideas from ```socket.io```. | ||
### namespace.name -> String | ||
Returns a namespace identifier. | ||
### namespace.connected() -> Object | ||
Returns a hash of websocket objects connected to this namespace, identified by ```id```. | ||
### namespace.emit(name[, ...params]) | ||
Emits a created event to clients connected to this namespace. | ||
Parameters: | ||
* `name` {String}: Name of the event. | ||
* `...params`: Parameters forwarded to clients in this namespace. | ||
### namespace.clients() -> Array | ||
Returns a list of client unique identifiers connected to this namespace. |
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
59052
1054
262