UNPKG

dirigera

Version:

A TypeScript client for IKEA's DIRIGERA smart home hub

44 lines (43 loc) 1.41 kB
import crypto from 'node:crypto'; import ReconnectingWebSocket from 'reconnecting-websocket'; import WebSocket from 'ws'; let ws = null; let timeout = null; export function initializeWebSocket({ ip, accessToken, callback, }) { // https://github.com/pladaria/reconnecting-websocket/issues/196 ws = new ReconnectingWebSocket(`wss://${ip}:8443/v1`, [], { minReconnectionDelay: 10, maxReconnectionDelay: 10000, maxRetries: Number.MAX_SAFE_INTEGER, WebSocket: class extends WebSocket { constructor(url, protocols) { super(url, protocols, { headers: { authorization: `Bearer ${accessToken}`, }, rejectUnauthorized: false, }); } }, debug: process.env['NODE_ENV'] === 'development', }); ws.addEventListener('message', (message) => { callback(JSON.parse(String(message.data))); }); timeout = setInterval(() => { ws?.send(JSON.stringify({ id: crypto.randomUUID(), specversion: '1.1.0', source: `urn:lpgera:dirigera`, time: new Date().toISOString(), type: 'ping', data: null, })); }, 30000); } export function closeWebSocket() { ws?.close(); if (timeout) { clearInterval(timeout); } }