MCPcopy Create free account
hub / github.com/dgreif/ring / createConnection

Method createConnection

packages/ring-client-api/location.ts:218–319  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

216 }
217
218 async createConnection(): Promise<WebSocket> {
219 if (this.disconnected) {
220 return Promise.resolve({ disconnected: true } as any)
221 }
222
223 logInfo('Creating location socket.io connection - ' + this.name)
224
225 if (process.version.startsWith('v15.')) {
226 logError(
227 'Node 15 is not currently supported by the Ring client. Please install the latest Node 14 instead. May not be able to fetch devices from Ring Alarm and Smart Lighting Hubs on this version of node.',
228 )
229 }
230
231 const { assets, ticket, host } = await this.restClient.request<{
232 assets: TicketAsset[]
233 host: string
234 subscriptionTopics: string[]
235 ticket: string
236 }>({
237 url: appApi(
238 `clap/tickets?locationID=${this.id}&enableExtendedEmergencyCellUsage=true&requestedTransport=ws`,
239 ),
240 }),
241 supportedAssets = assets.filter(isWebSocketSupportedAsset)
242 this.assets = supportedAssets
243 this.receivedAssetDeviceLists.length = 0
244 this.offlineAssets.length = 0
245
246 if (!supportedAssets.length) {
247 const errorMessage = `No assets (alarm hubs or beam bridges) found for location ${this.name} - ${this.id}`
248 logError(errorMessage)
249 throw new Error(errorMessage)
250 }
251
252 const url = `wss://${host}/ws?authcode=${ticket}&ack=false`,
253 socket = new WebSocket(url),
254 reconnect = () => {
255 if (this.reconnecting && this.connectionPromise) {
256 return this.connectionPromise
257 }
258
259 this.onConnected.next(false)
260
261 if (!this.disconnected) {
262 logInfo('Reconnecting location socket.io connection')
263 }
264
265 this.reconnecting = true
266 socket.close()
267 return (this.connectionPromise = delay(1000).then(() => {
268 return this.createConnection()
269 }))
270 }
271
272 socket.addEventListener('message', (event) => {
273 try {
274 const { msg: message, channel } = JSON.parse(event.data) as {
275 msg: SocketIoMessage

Callers 2

reconnectMethod · 0.95
getConnectionMethod · 0.95

Calls 5

requestListMethod · 0.95
logInfoFunction · 0.90
logErrorFunction · 0.90
appApiFunction · 0.90
logDebugFunction · 0.90

Tested by

no test coverage detected