websocket-rpc-client
Advanced tools
Comparing version
@@ -20,2 +20,8 @@ 'use strict'; | ||
/** | ||
* @param {string} name - channel name | ||
* @param {function} cb - callback | ||
* | ||
* @return {WSEvents} | ||
* */ | ||
subscribe: function subscribe(name, cb) { | ||
@@ -30,3 +36,12 @@ | ||
} | ||
return this; | ||
}, | ||
/** | ||
* @param {string} name - channel name | ||
* @param {function} cb - callback | ||
* | ||
* @return {WSEvents} | ||
* */ | ||
unsubscribe: function unsubscribe(name, cb) { | ||
@@ -45,3 +60,11 @@ if (name) { | ||
} | ||
return this; | ||
}, | ||
/** | ||
* @private | ||
* @param {Object} resp - JSON-RPC event | ||
* | ||
* */ | ||
resolveEvent: function resolveEvent(resp) { | ||
@@ -48,0 +71,0 @@ var cbs = this.pool.get(resp.method); |
@@ -13,2 +13,6 @@ 'use strict'; | ||
var _defer = require('lodash/defer'); | ||
var _defer2 = _interopRequireDefault(_defer); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -21,78 +25,75 @@ | ||
* @class | ||
* @classdesc ΠΠΏΠΈΡΡΠ²Π°Π΅Ρ ΠΏΠ°ΠΊΠ΅Ρ Π·Π°ΠΏΡΠΎΡΠ°. ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π»Ρ ΡΠ΅Π·ΠΎΠ»Π²Π° Π²ΡΠ·ΠΎΠ²Π°, ΡΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΡΡ Π΄Π°Π½Π½ΡΡ ΠΈ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ | ||
* | ||
* @property {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* @property {object} data - Π΄Π°Π½Π½ΡΠ΅ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ Π·Π°ΠΏΡΠΎΡΠ° | ||
* @property {object|string} error - ΡΠ΅ΡΠ²Π΅ΡΠ½Π°Ρ ΠΎΡΠΈΠ±ΠΊΠ° | ||
* @property {function} reject - ΠΌΠ΅ΡΠΎΠ΄ ΠΏΡΠΎΠΌΠΈΡΠ° | ||
* @propery {function} resolve - ΠΌΠ΅ΡΠΎΠ΄ ΠΏΡΠΎΠΌΠΈΡΠ° | ||
* @propery {object} config - ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΡΠ΅Π»Π° Π·Π°ΠΏΡΠΎΡΠ° | ||
* @property {number} id - id message | ||
* @property {object} data - request data | ||
* @property {object|string} error - server error | ||
* @property {function} reject - reject promise | ||
* @propery {function} resolve - resolve promise | ||
* @propery {object} config - describe body | ||
* */ | ||
exports.default = function () { | ||
var WSPacket = function () { | ||
/** | ||
* @constructor | ||
* @param {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* */ | ||
var WSPacket = function () { | ||
/** | ||
* @constructor | ||
* @param {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* */ | ||
function WSPacket(id) { | ||
_classCallCheck(this, WSPacket); | ||
function WSPacket(id) { | ||
_classCallCheck(this, WSPacket); | ||
this.id = id; | ||
this.message = {}; | ||
this.id = id; | ||
this.message = {}; | ||
} | ||
_createClass(WSPacket, [{ | ||
key: 'create', | ||
value: function create(method, params, resolve, reject) { | ||
this.message.id = this.id; | ||
this.message.method = method; | ||
this.message.params = params; | ||
this.rejectCb = reject; | ||
this.resolveCb = resolve; | ||
} | ||
_createClass(WSPacket, [{ | ||
key: 'create', | ||
value: function create(method, params, resolve, reject) { | ||
this.message.id = this.id; | ||
this.message.method = method; | ||
this.message.params = params; | ||
this.rejectCb = reject; | ||
this.resolveCb = resolve; | ||
}, { | ||
key: 'getMessage', | ||
value: function getMessage() { | ||
return this.message; | ||
} | ||
}, { | ||
key: 'resolve', | ||
value: function resolve(data) { | ||
if ((0, _isFunction2.default)(this.resolveCb)) { | ||
(0, _defer2.default)(this.resolveCb, { | ||
data: data | ||
}); | ||
} | ||
}, { | ||
key: 'getMessage', | ||
value: function getMessage() { | ||
return this.message; | ||
return this; | ||
} | ||
}, { | ||
key: 'reject', | ||
value: function reject(error) { | ||
if ((0, _isFunction2.default)(this.rejectCb)) { | ||
(0, _defer2.default)(this.rejectCb, { | ||
error: error | ||
}); | ||
} | ||
}, { | ||
key: 'resolve', | ||
value: function resolve(data) { | ||
if ((0, _isFunction2.default)(this.resolveCb)) { | ||
this.resolveCb({ | ||
data: data | ||
}); | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'reject', | ||
value: function reject(error) { | ||
if ((0, _isFunction2.default)(this.rejectCb)) { | ||
this.rejectCb({ | ||
error: error | ||
}); | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'clear', | ||
value: function clear() { | ||
/*delete this.id; | ||
this.message = {}; | ||
delete this.data; | ||
delete this.rejectCb; | ||
delete this.resolveCb; | ||
delete this.sourceOut; | ||
delete this.sourceIn; | ||
delete this.error;*/ | ||
} | ||
}]); | ||
return this; | ||
} | ||
}, { | ||
key: 'clear', | ||
value: function clear() { | ||
/*delete this.id; | ||
this.message = {}; | ||
delete this.data; | ||
delete this.rejectCb; | ||
delete this.resolveCb; | ||
delete this.sourceOut; | ||
delete this.sourceIn; | ||
delete this.error;*/ | ||
} | ||
}]); | ||
return WSPacket; | ||
}(); | ||
return WSPacket; | ||
}(); | ||
return WSPacket; | ||
}(); | ||
exports.default = WSPacket; |
@@ -15,6 +15,2 @@ 'use strict'; | ||
var _packet = require('./packet'); | ||
var _packet2 = _interopRequireDefault(_packet); | ||
var _isString = require('lodash/isString'); | ||
@@ -80,3 +76,3 @@ | ||
_this2.ws.onmessage = _this2.onMessage.bind(_this2); | ||
_pool2.default.init(100); | ||
_pool2.default.init(_this2.config.poolSize); | ||
resolve(e); | ||
@@ -116,14 +112,14 @@ }; | ||
onMessage: function onMessage(data) { | ||
if (data.data instanceof ArrayBuffer) { | ||
this.ws.isBinary = true; | ||
//try { | ||
this.resolve(JSON.parse(_pako2.default.inflate(new Uint8Array(data.data), { to: 'string' }))); | ||
/*} catch (e) { | ||
this.resolve({ | ||
error: e | ||
}) | ||
}*/ | ||
} else { | ||
this.resolve(JSON.parse(data.data)); | ||
try { | ||
var msg = data.data; | ||
if (data.data instanceof ArrayBuffer) { | ||
this.ws.isBinary = true; | ||
msg = _pako2.default.inflate(new Uint8Array(data.data), { to: 'string' }); | ||
} | ||
this.resolve(JSON.parse(msg)); | ||
} catch (e) { | ||
this.reject({ | ||
error: e | ||
}); | ||
} | ||
}, | ||
@@ -130,0 +126,0 @@ resolve: function resolve(response) { |
{ | ||
"name": "websocket-rpc-client", | ||
"version": "0.1.12", | ||
"version": "0.2.0", | ||
"description": "websocket JSON-RPC 2.0 client ", | ||
@@ -5,0 +5,0 @@ "main": "./dest/index.js", |
@@ -8,2 +8,8 @@ import defer from 'lodash/defer'; | ||
/** | ||
* @param {string} name - channel name | ||
* @param {function} cb - callback | ||
* | ||
* @return {WSEvents} | ||
* */ | ||
subscribe(name, cb) { | ||
@@ -18,4 +24,11 @@ | ||
} | ||
return this; | ||
}, | ||
/** | ||
* @param {string} name - channel name | ||
* @param {function} cb - callback | ||
* | ||
* @return {WSEvents} | ||
* */ | ||
unsubscribe(name, cb) { | ||
@@ -34,5 +47,11 @@ if(name) { | ||
} | ||
return this; | ||
}, | ||
/** | ||
* @private | ||
* @param {Object} resp - JSON-RPC event | ||
* | ||
* */ | ||
resolveEvent(resp) { | ||
@@ -39,0 +58,0 @@ var cbs = this.pool.get(resp.method); |
import isFunction from 'lodash/isFunction'; | ||
import defer from 'lodash/defer'; | ||
@@ -7,65 +8,60 @@ /*** | ||
* @class | ||
* @classdesc ΠΠΏΠΈΡΡΠ²Π°Π΅Ρ ΠΏΠ°ΠΊΠ΅Ρ Π·Π°ΠΏΡΠΎΡΠ°. ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π»Ρ ΡΠ΅Π·ΠΎΠ»Π²Π° Π²ΡΠ·ΠΎΠ²Π°, ΡΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΡΡ Π΄Π°Π½Π½ΡΡ ΠΈ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ | ||
* | ||
* @property {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* @property {object} data - Π΄Π°Π½Π½ΡΠ΅ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ Π·Π°ΠΏΡΠΎΡΠ° | ||
* @property {object|string} error - ΡΠ΅ΡΠ²Π΅ΡΠ½Π°Ρ ΠΎΡΠΈΠ±ΠΊΠ° | ||
* @property {function} reject - ΠΌΠ΅ΡΠΎΠ΄ ΠΏΡΠΎΠΌΠΈΡΠ° | ||
* @propery {function} resolve - ΠΌΠ΅ΡΠΎΠ΄ ΠΏΡΠΎΠΌΠΈΡΠ° | ||
* @propery {object} config - ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΡΠ΅Π»Π° Π·Π°ΠΏΡΠΎΡΠ° | ||
* @property {number} id - id message | ||
* @property {object} data - request data | ||
* @property {object|string} error - server error | ||
* @property {function} reject - reject promise | ||
* @propery {function} resolve - resolve promise | ||
* @propery {object} config - describe body | ||
* */ | ||
export default (function(){ | ||
class WSPacket { | ||
/** | ||
* @constructor | ||
* @param {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* */ | ||
constructor(id) { | ||
this.id = id; | ||
this.message = {} | ||
} | ||
export default class WSPacket { | ||
/** | ||
* @constructor | ||
* @param {number} id - ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ | ||
* */ | ||
constructor(id) { | ||
this.id = id; | ||
this.message = {} | ||
} | ||
create(method, params, resolve, reject) { | ||
this.message.id = this.id; | ||
this.message.method = method; | ||
this.message.params = params; | ||
this.rejectCb = reject; | ||
this.resolveCb = resolve; | ||
} | ||
create(method, params, resolve, reject) { | ||
this.message.id = this.id; | ||
this.message.method = method; | ||
this.message.params = params; | ||
this.rejectCb = reject; | ||
this.resolveCb = resolve; | ||
} | ||
getMessage() { | ||
return this.message; | ||
} | ||
getMessage() { | ||
return this.message; | ||
} | ||
resolve(data) { | ||
if(isFunction(this.resolveCb)) { | ||
this.resolveCb({ | ||
data: data | ||
}); | ||
} | ||
return this; | ||
resolve(data) { | ||
if(isFunction(this.resolveCb)) { | ||
defer(this.resolveCb, { | ||
data: data | ||
}); | ||
} | ||
return this; | ||
} | ||
reject(error) { | ||
if(isFunction(this.rejectCb )) { | ||
this.rejectCb({ | ||
error: error | ||
}); | ||
} | ||
return this; | ||
reject(error) { | ||
if(isFunction(this.rejectCb )) { | ||
defer(this.rejectCb, { | ||
error: error | ||
}); | ||
} | ||
return this; | ||
} | ||
clear() { | ||
/*delete this.id; | ||
this.message = {}; | ||
delete this.data; | ||
delete this.rejectCb; | ||
delete this.resolveCb; | ||
delete this.sourceOut; | ||
delete this.sourceIn; | ||
delete this.error;*/ | ||
} | ||
clear() { | ||
/*delete this.id; | ||
this.message = {}; | ||
delete this.data; | ||
delete this.rejectCb; | ||
delete this.resolveCb; | ||
delete this.sourceOut; | ||
delete this.sourceIn; | ||
delete this.error;*/ | ||
} | ||
return WSPacket; | ||
})() | ||
} |
import WSPool from './pool'; | ||
import WSEvents from './events'; | ||
import WSPacket from './packet'; | ||
import isString from 'lodash/isString'; | ||
@@ -47,3 +46,3 @@ import isUndefined from 'lodash/isUndefined'; | ||
this.ws.onmessage = this.onMessage.bind(this); | ||
WSPool.init(100); | ||
WSPool.init(this.config.poolSize); | ||
resolve(e); | ||
@@ -88,13 +87,13 @@ }; | ||
onMessage(data) { | ||
if (data.data instanceof ArrayBuffer) { | ||
this.ws.isBinary = true; | ||
//try { | ||
this.resolve(JSON.parse(pako.inflate(new Uint8Array(data.data), {to: 'string'}))) | ||
/*} catch (e) { | ||
this.resolve({ | ||
error: e | ||
}) | ||
}*/ | ||
} else { | ||
this.resolve(JSON.parse(data.data)); | ||
try { | ||
var msg = data.data; | ||
if(data.data instanceof ArrayBuffer) { | ||
this.ws.isBinary = true; | ||
msg = pako.inflate(new Uint8Array(data.data), {to: 'string'}); | ||
} | ||
this.resolve(JSON.parse(msg)); | ||
} catch (e) { | ||
this.reject({ | ||
error: e | ||
}) | ||
} | ||
@@ -101,0 +100,0 @@ }, |
647
5.03%23540
-1.65%