libshv-js 3.3.3 → 3.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ws-client.ts +17 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.3.3",
3
+ "version": "3.3.4",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
package/src/ws-client.ts CHANGED
@@ -41,6 +41,7 @@ type WsClientOptions = {
41
41
  timeout?: number;
42
42
  wsUri: string;
43
43
  onConnected: () => void;
44
+ onConnectionFailure: (error: Error) => void;
44
45
  onDisconnected: () => void;
45
46
  onRequest: (rpc_msg: RpcMessage) => void;
46
47
  };
@@ -87,6 +88,7 @@ class WsClient {
87
88
  password: WsClientOptions['password'];
88
89
  loginType: WsClientOptions['loginType'];
89
90
  onConnected: WsClientOptions['onConnected'];
91
+ onConnectionFailure: WsClientOptions['onConnectionFailure'];
90
92
  onDisconnected: WsClientOptions['onDisconnected'];
91
93
  onRequest: WsClientOptions['onRequest'];
92
94
  timeout: WsClientOptions['timeout'];
@@ -107,6 +109,7 @@ class WsClient {
107
109
  this.websocket.binaryType = 'arraybuffer';
108
110
 
109
111
  this.onConnected = options.onConnected ?? (() => {/* nothing */});
112
+ this.onConnectionFailure = options.onConnectionFailure ?? (() => {/* nothing */});
110
113
  this.onDisconnected = options.onDisconnected ?? (() => {/* nothing */});
111
114
  this.onRequest = options.onRequest ?? (() => {/* nothing */});
112
115
 
@@ -114,7 +117,15 @@ class WsClient {
114
117
 
115
118
  this.websocket.addEventListener('open', () => {
116
119
  this.logDebug('CONNECTED');
117
- this.callRpcMethod(undefined, 'hello').then(() => {
120
+ const handleConnectionError = (error: Error) => {
121
+ this.logDebug('FAILURE: couldn\'t perform initial handshake', error.message);
122
+ this.onConnectionFailure(error);
123
+ };
124
+ this.callRpcMethod(undefined, 'hello').then(response => {
125
+ if (response instanceof Error) {
126
+ handleConnectionError(response);
127
+ return;
128
+ }
118
129
  const params = makeMap({
119
130
  login: makeMap({
120
131
  password: this.password,
@@ -126,7 +137,11 @@ class WsClient {
126
137
  }),
127
138
  });
128
139
  return this.callRpcMethod(undefined, 'login', params);
129
- }).then(() => {
140
+ }).then(response => {
141
+ if (response instanceof Error) {
142
+ handleConnectionError(response);
143
+ return;
144
+ }
130
145
  this.logDebug('SUCCESS: connected to shv broker');
131
146
  this.onConnected();
132
147
  }).catch(() => {