user 6 gadi atpakaļ
vecāks
revīzija
f4fdcc1050
4 mainītis faili ar 111 papildinājumiem un 60 dzēšanām
  1. 2 1
      lib/index.d.ts
  2. 53 28
      lib/index.js
  3. 1 1
      lib/index.js.map
  4. 55 30
      src/index.ts

+ 2 - 1
lib/index.d.ts

@@ -26,12 +26,13 @@ export declare class BankClient {
     uploadSlimText(item: string): Promise<any>;
     appendBank(bankAddress: string, bankTopic: string, itemHash: string): Promise<void>;
     retrievePrivate(peerAddr: string, topic: string): Promise<string>;
-    subscribePrivate(peerAddr: string, topic: string, callback: () => void): Promise<unknown>;
+    subscribePrivate(peerAddr: string, topic: string, callback: () => void): Promise<void>;
     appendPrivate(peerAddr: string, topic: string, hash?: string, replaceHash?: string, deleteHash?: string): Promise<void>;
     getOrCreateContact(peerId: string, contactAddr: string): Promise<any>;
     getContactById(peerId: string, contactId: string): Promise<any>;
     updateContact(peerId: string, contactId: string, newProperties: any): Promise<any>;
     getContentItemByHash(hash: string): Promise<ContentItem>;
+    private connectWebsocket;
     private getItemsForCommaList;
     private getPriv;
     private makePlaintextPayload;

+ 53 - 28
lib/index.js

@@ -220,34 +220,9 @@ class BankClient {
         });
     }
     subscribePrivate(peerAddr, topic, callback) {
-        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
-            const nonce = yield this.getNonce();
-            const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
-                _date: new Date().toISOString(),
-                _nonce: nonce,
-                addr: peerAddr,
-                topic: topic
-            }));
-            const ws = new ws_1.default(this.wsUrlBase + '/bank/ws', {
-                headers: {
-                    'x-msg': retrieveRequest.msg,
-                    'x-pub': retrieveRequest.pub,
-                    'x-pubhash': retrieveRequest.pubHash,
-                    'x-sig': retrieveRequest.sig
-                }
-            });
-            ws.on('open', function open() {
-                console.log('opened ws');
-                resolve();
-            });
-            ws.on('message', data => {
-                // console.log('ws message', data);
-                callback();
-            });
-            ws.on('error', err => {
-                reject(err);
-            });
-        }));
+        return __awaiter(this, void 0, void 0, function* () {
+            yield this.connectWebsocket(peerAddr, topic, callback);
+        });
     }
     appendPrivate(peerAddr, topic, hash, replaceHash, deleteHash) {
         return __awaiter(this, void 0, void 0, function* () {
@@ -325,6 +300,56 @@ class BankClient {
             return new content_item_1.ContentItem(hash, contentParams);
         });
     }
+    connectWebsocket(peerAddr, topic, callback) {
+        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
+            const nonce = yield this.getNonce();
+            const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
+                _date: new Date().toISOString(),
+                _nonce: nonce,
+                addr: peerAddr,
+                topic: topic
+            }));
+            const arg = encodeURIComponent(Buffer.from(JSON.stringify(retrieveRequest)).toString('base64'));
+            const ws = new ws_1.default(this.wsUrlBase + '/bank/ws?arg=' + arg, {
+                headers: {
+                    'x-msg': retrieveRequest.msg,
+                    'x-pub': retrieveRequest.pub,
+                    'x-pubhash': retrieveRequest.pubHash,
+                    'x-sig': retrieveRequest.sig
+                }
+            });
+            ws.on('open', (param) => {
+                console.log('opened ws', param);
+                resolve();
+            });
+            ws.on('message', data => {
+                // console.log('ws message', data);
+                callback();
+            });
+            ws.on('error', err => {
+                try {
+                    ws.terminate();
+                }
+                finally {
+                    console.log('reconnecting in 5s');
+                    setTimeout(() => {
+                        this.connectWebsocket(peerAddr, topic, callback);
+                    }, 5000);
+                }
+            });
+            ws.on('close', err => {
+                try {
+                    ws.terminate();
+                }
+                finally {
+                    console.log('reconnecting in 5s');
+                    setTimeout(() => {
+                        this.connectWebsocket(peerAddr, topic, callback);
+                    }, 5000);
+                }
+            });
+        }));
+    }
     getItemsForCommaList(commaList) {
         return __awaiter(this, void 0, void 0, function* () {
             const itemHashes = commaList.split(',').filter(x => x.trim() !== '');

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 1 - 1
lib/index.js.map


+ 55 - 30
src/index.ts

@@ -213,36 +213,8 @@ export class BankClient {
       return result;
     }
 
-    public subscribePrivate(peerAddr: string, topic: string, callback: () => void) {
-      return new Promise(async (resolve, reject) => {
-        const nonce = await this.getNonce();
-        const retrieveRequest: any = await this.makePlaintextPayload(JSON.stringify({
-          _date: new Date().toISOString(),
-          _nonce: nonce,
-          addr: peerAddr,
-          topic: topic
-        }));
-
-        const ws = new WebSocket(this.wsUrlBase + '/bank/ws', {
-          headers: {
-            'x-msg': retrieveRequest.msg,
-            'x-pub': retrieveRequest.pub,
-            'x-pubhash': retrieveRequest.pubHash,
-            'x-sig': retrieveRequest.sig
-          }
-        });
-        ws.on('open', function open() {
-          console.log('opened ws');
-          resolve();
-        });
-        ws.on('message', data => {
-          // console.log('ws message', data);
-          callback();
-        });
-        ws.on('error', err => {
-          reject(err);
-        });
-      });
+    public async subscribePrivate(peerAddr: string, topic: string, callback: () => void) {
+      await this.connectWebsocket(peerAddr, topic, callback);
     }
 
     public async appendPrivate(peerAddr: string, topic: string, hash?: string, replaceHash?: string, deleteHash?: string) {
@@ -316,6 +288,59 @@ export class BankClient {
       return new ContentItem(hash, contentParams);
     }
 
+    private connectWebsocket(peerAddr: string, topic: string, callback: () => void) {
+      return new Promise(async (resolve, reject) => {
+        const nonce = await this.getNonce();
+        const retrieveRequest: any = await this.makePlaintextPayload(JSON.stringify({
+          _date: new Date().toISOString(),
+          _nonce: nonce,
+          addr: peerAddr,
+          topic: topic
+        }));
+
+        const arg = encodeURIComponent(Buffer.from(JSON.stringify(retrieveRequest)).toString('base64'));
+        const ws = new WebSocket(this.wsUrlBase + '/bank/ws?arg=' + arg, {
+          headers: {
+            'x-msg': retrieveRequest.msg,
+            'x-pub': retrieveRequest.pub,
+            'x-pubhash': retrieveRequest.pubHash,
+            'x-sig': retrieveRequest.sig
+          }
+        });
+
+        ws.on('open', (param: any) => {
+          console.log('opened ws', param);
+          resolve();
+        });
+
+        ws.on('message', data => {
+          // console.log('ws message', data);
+          callback();
+        });
+
+        ws.on('error', err => {
+          try {
+            ws.terminate();
+          } finally {
+            console.log('reconnecting in 5s');
+            setTimeout(() => {
+              this.connectWebsocket(peerAddr, topic, callback);
+            }, 5000);
+          }
+        });
+        ws.on('close', err => {
+          try {
+            ws.terminate();
+          } finally {
+            console.log('reconnecting in 5s');
+            setTimeout(() => {
+              this.connectWebsocket(peerAddr, topic, callback);
+            }, 5000);
+          }
+        });
+      });
+    }
+
     private async getItemsForCommaList(commaList: string): Promise<any[]> {
       const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
       const items: any[] = await Promise.all(itemHashes.map(itemId => {