* Used to subscribe to a single websocket endpoint * @param {string} endpoint - endpoint to connect to * @param {function} callback - the function to call when information is received * @param {boolean} reconnect - whether to reconnect on disconnect * @param {object} opened_callb
(endpoint: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback)
| 1438 | * @return {WebSocket} - websocket reference |
| 1439 | */ |
| 1440 | subscribe(endpoint: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback) { |
| 1441 | const httpsproxy = this.getHttpsProxy(); |
| 1442 | let socksproxy = this.getSocksProxy(); |
| 1443 | let ws: WebSocket = undefined; |
| 1444 | |
| 1445 | if (socksproxy) { |
| 1446 | socksproxy = this.proxyReplacewithIp(socksproxy); |
| 1447 | if (this.Options.verbose) this.Options.log('using socks proxy server ' + socksproxy); |
| 1448 | const agent = new SocksProxyAgent({ |
| 1449 | protocol: this.parseProxy(socksproxy)[0], |
| 1450 | host: this.parseProxy(socksproxy)[1], |
| 1451 | port: this.parseProxy(socksproxy)[2] |
| 1452 | }); |
| 1453 | ws = new WebSocket(this.getStreamUrl() + endpoint, { agent: agent }); |
| 1454 | } else if (httpsproxy) { |
| 1455 | const config = url.parse(httpsproxy); |
| 1456 | const agent = new HttpsProxyAgent(config); |
| 1457 | if (this.Options.verbose) this.Options.log('using proxy server ' + agent); |
| 1458 | ws = new WebSocket(this.getStreamUrl() + endpoint, { agent: agent }); |
| 1459 | } else { |
| 1460 | ws = new WebSocket(this.getStreamUrl() + endpoint); |
| 1461 | } |
| 1462 | |
| 1463 | if (this.Options.verbose) this.Options.log('Subscribed to ' + endpoint); |
| 1464 | (ws as any).reconnect = this.Options.reconnect; |
| 1465 | (ws as any).endpoint = endpoint; |
| 1466 | (ws as any).isAlive = false; |
| 1467 | ws.on('open', this.handleSocketOpen.bind(this, ws, opened_callback)); |
| 1468 | ws.on('pong', this.handleSocketHeartbeat.bind(this, ws)); |
| 1469 | ws.on('error', this.handleSocketError.bind(this, ws)); |
| 1470 | ws.on('close', this.handleSocketClose.bind(this, ws, reconnect)); |
| 1471 | ws.on('message', data => { |
| 1472 | try { |
| 1473 | if (this.Options.verbose) this.Options.log('WebSocket data:', data); |
| 1474 | callback(JSONbig.parse(data as any)); |
| 1475 | } catch (error) { |
| 1476 | this.Options.log('Parse error: ' + error.message); |
| 1477 | } |
| 1478 | }); |
| 1479 | return ws; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * Used to subscribe to a combined websocket endpoint |
no test coverage detected