Skip to content

Commit

Permalink
fix(ws): when connected failed, it have not trigger a error event
Browse files Browse the repository at this point in the history
  • Loading branch information
soliury committed Apr 13, 2021
1 parent 4ada37e commit b38c399
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/WS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class WsRpc extends IPC_WS {
this.socket.onclose = () => {
this._closed();
};
this.socket.onerror = () => {
this._errored();
this.socket.onerror = (err) => {
this._errored(err);
};
this.socket.onmessage = e => {
const data = (typeof e.data === 'string') ? e.data : '';
Expand Down
4 changes: 2 additions & 2 deletions src/communication/ipc_ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class IpcWs extends Communication {
this.responseCbs = {};

this._connectEnd = null;
this._connectErr = null;
this._connectError = null;
this._connectTimeout = null;
this._connectConnect = null;
this._connectClose = null;
Expand All @@ -31,7 +31,7 @@ class IpcWs extends Communication {
}

_errored(err) {
this._connectErr && this._connectErr(err);
this._connectError && this._connectError(err);
}

_parse(data) {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Communication Test: src/communication', function () {
describe('ViteAPI Test: src/viteAPI', function () {
require('./packages/viteAPI/provider');
require('./packages/viteAPI/index');
require('./packages/viteAPI/ws');
});

require('./packages/accountBlock/transaction');
Expand Down
26 changes: 26 additions & 0 deletions test/packages/viteAPI/ws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const assert = require('assert');

import Provider from '../../../src/viteAPI/provider';
import WS_RPC from '../../../src/WS';

describe('test ws provider', () => {
it('should trigger event when connect successfully', (done) => {
const wsServer = new WS_RPC('wss://node-tokyo.vite.net/ws', 2000, {
retryInterval: 1,
retryTimes: -1
});
const viteClient = new Provider(wsServer, () => {
done();
});
});

it('should trigger error event when connected failed', (done) => {
const wsServer = new WS_RPC('wss://no.vite.net/ws', 2000, {
retryInterval: 1,
retryTimes: -1
});
wsServer.on('error', (err) => {
done();
});
});
});

0 comments on commit b38c399

Please sign in to comment.