* Used to subscribe to a combined delivery websocket endpoint * @param {string} streams - streams to connect to * @param {function} callback - the function to call when information is received * @param {object} params - Optional reconnect {boolean} (whether to reconnect on disconnect)
(streams, callback: Callback, params: Dict = {})
| 2577 | * @return {WebSocket} - websocket reference |
| 2578 | */ |
| 2579 | deliverySubscribe(streams, callback: Callback, params: Dict = {}) { |
| 2580 | if (typeof streams === 'string') return this.deliverySubscribeSingle(streams, callback, params); |
| 2581 | if (typeof params === 'boolean') params = { reconnect: params }; |
| 2582 | if (!params.reconnect) params.reconnect = false; |
| 2583 | if (!params.openCallback) params.openCallback = false; |
| 2584 | if (!params.id) params.id = false; |
| 2585 | const httpsproxy = this.getHttpsProxy(); |
| 2586 | let socksproxy = this.getSocksProxy(); |
| 2587 | const queryParams = streams.join('/'); |
| 2588 | let ws: any = undefined; |
| 2589 | if (socksproxy) { |
| 2590 | socksproxy = this.proxyReplacewithIp(socksproxy); |
| 2591 | if (this.Options.verbose) this.Options.log(`deliverySubscribe: using socks proxy server ${socksproxy}`); |
| 2592 | const agent = new SocksProxyAgent({ |
| 2593 | protocol: this.parseProxy(socksproxy)[0], |
| 2594 | host: this.parseProxy(socksproxy)[1], |
| 2595 | port: this.parseProxy(socksproxy)[2] |
| 2596 | }); |
| 2597 | ws = new WebSocket((this.getDStreamUrl()) + queryParams, { agent }); |
| 2598 | } else if (httpsproxy) { |
| 2599 | if (this.Options.verbose) this.Options.log(`deliverySubscribe: using proxy server ${httpsproxy}`); |
| 2600 | const config = url.parse(httpsproxy); |
| 2601 | const agent = new HttpsProxyAgent(config); |
| 2602 | ws = new WebSocket((this.getDStreamUrl()) + queryParams, { agent }); |
| 2603 | } else { |
| 2604 | ws = new WebSocket((this.getDStreamUrl()) + queryParams); |
| 2605 | } |
| 2606 | |
| 2607 | ws.reconnect = this.Options.reconnect; |
| 2608 | ws.endpoint = stringHash(queryParams); |
| 2609 | ws.isAlive = false; |
| 2610 | if (this.Options.verbose) { |
| 2611 | this.Options.log(`deliverySubscribe: Subscribed to [${ws.endpoint}] ${queryParams}`); |
| 2612 | } |
| 2613 | ws.on('open', this.handleDeliverySocketOpen.bind(this, ws, params.openCallback)); |
| 2614 | ws.on('pong', this.handleDeliverySocketHeartbeat.bind(this, ws)); |
| 2615 | ws.on('error', this.handleDeliverySocketError.bind(this, ws)); |
| 2616 | ws.on('close', this.handleDeliverySocketClose.bind(this, ws, params.reconnect)); |
| 2617 | ws.on('message', data => { |
| 2618 | try { |
| 2619 | if (this.Options.verbose) this.Options.log('deliverySubscribe: Received data:', data); |
| 2620 | callback(JSONbig.parse(data).data); |
| 2621 | } catch (error) { |
| 2622 | this.Options.log(`deliverySubscribe: Parse error: ${error.message}`); |
| 2623 | } |
| 2624 | }); |
| 2625 | return ws; |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * Used to terminate a delivery websocket |
no test coverage detected