-
Notifications
You must be signed in to change notification settings - Fork 11
/
socket-client.ts
61 lines (56 loc) · 1.86 KB
/
socket-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as mqtt from 'mqtt'
import config from '../services/config'
// @ts-ignore
import { getProxyForUrl } from 'proxy-from-env'
import { httpsOverHttp, httpsOverHttps } from 'tunnel'
const isHttps = (protocol: string) => protocol.startsWith('https')
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
async function backOffConnect (url: string, options: mqtt.IClientOptions, retryCount = 0):
Promise<mqtt.MqttClient> {
try {
return mqtt.connectAsync(url, options, false)
} catch (error) {
if (retryCount > 3) {
throw error
}
retryCount += 1
await wait(100 * retryCount)
return backOffConnect(url, options, retryCount)
}
}
export class SocketClient {
static connect (): Promise<mqtt.MqttClient> {
const url = config.getMqttUrl()
const accountId = config.getAccountId()
const apiKey = config.getApiKey()
const options: mqtt.IClientOptions = {
reconnectPeriod: 100,
username: accountId,
password: apiKey,
}
// Replace wss with https so the get proxy url thing the env path
const proxyUrlEnv = getProxyForUrl(url.replace('wss', 'https'))
if (proxyUrlEnv) {
const parsedProxyUrl = new URL(proxyUrlEnv)
const isProxyHttps = isHttps(parsedProxyUrl.protocol)
const proxy: any = {
host: parsedProxyUrl.hostname,
port: parsedProxyUrl.port,
protocol: parsedProxyUrl.protocol,
}
if (parsedProxyUrl.username && parsedProxyUrl.password) {
proxy.proxyAuth = `${parsedProxyUrl.username}:${parsedProxyUrl.password}`
}
if (isProxyHttps) {
options.wsOptions = {
agent: httpsOverHttps({ proxy }),
}
} else {
options.wsOptions = {
agent: httpsOverHttp({ proxy }),
}
}
}
return backOffConnect(`${url}?authenticationScheme=userApiKey`, options, 0)
}
}