Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Mar 4, 2019
1 parent 4e18112 commit bc9fb9c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 deletions.
12 changes: 11 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,18 @@ export default class client {
return reps;
}

async subscribe() {
async subscribe(...args) {
if (!this.isConnected) {
return this._onReq('request', 'subscribe_subscribe', ...args);
}

const rep: RPCresponse = await this._provider.request('subscribe_subscribe', ...args);
if (rep.error) {
throw rep.error
};

// const subscription = rep.result;
// this._provider.on()
}

async unSubscribe() {
Expand Down
14 changes: 13 additions & 1 deletion src/provider/Communication/ipc_ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Communication from './index.js';

class IPC_WS extends Communication {
constructor({
onEventTypes, sendFuncName
onEventTypes, sendFuncName, path
}) {
super();

this.path = path;
this._onEventTypes = onEventTypes || [];
this._sendFuncName = sendFuncName;

Expand Down Expand Up @@ -42,13 +43,16 @@ class IPC_WS extends Communication {

try {
let res = JSON.parse(ele);
// console.log('ele', ele);
if ( !(res instanceof Array) && res.result ) {
// Compatible: somtimes data.result is a json string, sometimes not.
try {
res.result = JSON.parse(res.result);
} catch (e) {
// console.log(e);
}
} else {
// console.log('res', res);
}

results.push(res);
Expand Down Expand Up @@ -145,6 +149,14 @@ class IPC_WS extends Communication {
});
}

_send(payloads) {
if (!this.connectStatus) {
return Promise.reject( this.ERRORS.CONNECT(this.path) );
}
this.socket[this._sendFuncName]( JSON.stringify(payloads) );
return this._onSend(payloads);
}

on (type, cb) {
let eventType = this._checkOnType(type);
if ( eventType < 0 ) {
Expand Down
18 changes: 9 additions & 9 deletions src/provider/IPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class IPC_RPC extends IPC_WS {
}) {
super({
onEventTypes: ['error', 'end', 'timeout', 'data', 'close', 'connect'],
sendFuncName: 'write'
sendFuncName: 'write',
path
});

if (!path) {
Expand All @@ -18,7 +19,6 @@ class IPC_RPC extends IPC_WS {
}

this.type = 'ipc';
this.path = path;
this.timeout = timeout;
this.delimiter = options.delimiter;

Expand Down Expand Up @@ -66,13 +66,13 @@ class IPC_RPC extends IPC_WS {
});
}

_send(payloads) {
if (!this.connectStatus) {
return Promise.reject( this.ERRORS.CONNECT(this.path) );
}
this.socket.write( JSON.stringify(payloads) );
return this._onSend(payloads);
}
// _send(payloads) {
// if (!this.connectStatus) {
// return Promise.reject( this.ERRORS.CONNECT(this.path) );
// }
// this.socket.write( JSON.stringify(payloads) );
// return this._onSend(payloads);
// }

reconnect() {
this.socket.connect({ path: this.path });
Expand Down
29 changes: 14 additions & 15 deletions src/provider/WS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import IPC_WS from './Communication/ipc_ws';
const websocket = require('websocket').w3cwebsocket;

class WS_RPC extends IPC_WS {
constructor(url = 'ws://localhost:31420', timeout = 60000, options = {
constructor(path = 'ws://localhost:31420', timeout = 60000, options = {
protocol: '',
headers: '',
clientConfig: '',
Expand All @@ -11,16 +11,15 @@ class WS_RPC extends IPC_WS {
}) {
super({
onEventTypes: ['error', 'close', 'connect'],
sendFuncName: 'send'
sendFuncName: 'send',
path
});

if (!url) {
console.error( this.ERRORS.CONNECT(url) );
return this.ERRORS.CONNECT(url);
if (!path) {
console.error( this.ERRORS.CONNECT(path) );
return this.ERRORS.CONNECT(path);
}

this.type = 'ws';
this.url = url;
this.timeout = timeout;
this.protocol = options.protocol;
this.headers = options.headers;
Expand All @@ -45,7 +44,7 @@ class WS_RPC extends IPC_WS {
}

reconnect() {
this.socket = new websocket(this.url, this.protocol, undefined, this.headers, undefined, this.clientConfig);
this.socket = new websocket(this.path, this.protocol, undefined, this.headers, undefined, this.clientConfig);
this.socket.onopen = () => {
(this.socket.readyState === this.socket.OPEN) && this._connected();
};
Expand All @@ -61,13 +60,13 @@ class WS_RPC extends IPC_WS {
};
}

_send(payloads) {
if (!this.connectStatus) {
return Promise.reject( this.ERRORS.CONNECT(this.url) );
}
this.socket.send( JSON.stringify(payloads) );
return this._onSend(payloads);
}
// _send(payloads) {
// if (!this.connectStatus) {
// return Promise.reject( this.ERRORS.CONNECT(this.path) );
// }
// this.socket.send( JSON.stringify(payloads) );
// return this._onSend(payloads);
// }

disconnect() {
this.socket && this.socket.close && this.socket.close();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/abi/coder/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getBytesData(type, params) {
}

let isHex = /^0x[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0;
let isCommonHex = /^[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0;
let isCommonHex = /^[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0 && type === 'bytes';

if (type === 'bytes' && !isCommonHex && !isHex) {
throw '[Error] Illegal params. Should be hex-string.';
Expand Down
5 changes: 5 additions & 0 deletions test/utils/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ describe('utils/tools', function () {

assert.equal(data, _data);
});

it('encode2decode', function() {
let result = abi.encodeParameter('string', '15');
assert.equal('15', abi.decodeParameter('string', result));
});
});


Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4504,7 +4504,6 @@ pure-uuid@^1.5.3:
qs@^6.6.0:
version "6.6.0"
resolved "http://registry.npm.taobao.org/qs/download/qs-6.6.0.tgz#a99c0f69a8d26bf7ef012f871cdabb0aee4424c2"
integrity sha1-qZwPaajSa/fvAS+HHNq7Cu5EJMI=

qs@~6.5.2:
version "6.5.2"
Expand Down Expand Up @@ -4954,13 +4953,6 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

source-map-support@^0.5.10:
version "0.5.10"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.10.tgz#2214080bc9d51832511ee2bab96e3c2f9353120c"
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map-support@^0.5.6, source-map-support@^0.5.9, source-map-support@~0.5.6:
version "0.5.9"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.9.tgz#41bc953b2534267ea2d605bccfa7bfa3111ced5f"
Expand Down

0 comments on commit bc9fb9c

Please sign in to comment.