|
@@ -213,8 +213,8 @@ export class BankClient {
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async subscribePrivate(peerAddr: string, topic: string, callback: () => void) {
|
|
|
|
|
- await this.connectWebsocket(peerAddr, topic, callback);
|
|
|
|
|
|
|
+ public async subscribePrivate(peerAddr: string, topic: string, connectCallback: () => void, messageCallback: (data: any) => void) {
|
|
|
|
|
+ await this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async appendPrivate(peerAddr: string, topic: string, hash?: string, replaceHash?: string, deleteHash?: string) {
|
|
public async appendPrivate(peerAddr: string, topic: string, hash?: string, replaceHash?: string, deleteHash?: string) {
|
|
@@ -288,58 +288,57 @@ export class BankClient {
|
|
|
return new ContentItem(hash, contentParams);
|
|
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
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ private connectWebsocket(peerAddr: string, topic: string, connectCallback: () => void, messageCallback: (data: any) => void) {
|
|
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
|
|
+ console.log('trying to connect');
|
|
|
|
|
+ const nonce = await this.getNonce();
|
|
|
|
|
+ const retrieveRequest: any = await this.makePlaintextPayload(JSON.stringify({
|
|
|
|
|
+ _date: new Date().toISOString(),
|
|
|
|
|
+ _nonce: nonce,
|
|
|
|
|
+ addr: peerAddr,
|
|
|
|
|
+ topic: topic
|
|
|
|
|
+ }));
|
|
|
|
|
|
|
|
- ws.on('open', (param: any) => {
|
|
|
|
|
- console.log('opened ws', param);
|
|
|
|
|
- resolve();
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ 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', () => {
|
|
|
|
|
+ connectCallback();
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- ws.on('message', data => {
|
|
|
|
|
- // console.log('ws message', data);
|
|
|
|
|
- callback();
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ ws.on('message', data => {
|
|
|
|
|
+ messageCallback(data);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const reconnect = () => {
|
|
|
|
|
+ console.log('reconnect');
|
|
|
|
|
+ try {
|
|
|
|
|
+ ws.terminate();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ console.log('reconnecting in 5s');
|
|
|
|
|
+ setTimeout(() => {
|
|
|
|
|
+ this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
|
|
|
+ }, 5000);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ ws.on('error', err => {
|
|
|
|
|
+ console.error('websocket error', err);
|
|
|
});
|
|
});
|
|
|
- }
|
|
|
|
|
|
|
+ ws.on('close', err => {
|
|
|
|
|
+ reconnect();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ resolve();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private async getItemsForCommaList(commaList: string): Promise<any[]> {
|
|
private async getItemsForCommaList(commaList: string): Promise<any[]> {
|
|
|
const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
|
|
const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
|