From b493b1ec855942e42279bd5044dbd9ab9e3769cb Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Thu, 12 May 2022 18:42:46 +0200 Subject: [PATCH] onmessage when ws is rebuild --- index.mjs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.mjs b/index.mjs index 5a20937..6ef0705 100644 --- a/index.mjs +++ b/index.mjs @@ -6,13 +6,16 @@ let ws; const wsConnect = () => { ws = new WebSocket(secrets.wsUrl); ws.on("close", () => { + console.log(`${new Date().toString()}: Websocket closed unexpectedly`); ws = undefined; setTimeout(wsConnect, 10000); // Retry after 10 seconds }); ws.on("error", () => { + console.log(`${new Date().toString()}: Error on websocket`); ws = undefined; setTimeout(wsConnect, 10000); // Retry after 10 seconds }); + ws.on("message", wsOnMsg); } do { wsConnect(); @@ -39,7 +42,7 @@ function metaFromMetadata(metadata) { return meta; } -ws.on("message", (data) => { +function wsOnMsg(data) { if (!client.connected) return; const parsed = JSON.parse(data); @@ -65,4 +68,4 @@ ws.on("message", (data) => { } client.publish(secrets.mqttTopic, JSON.stringify(message)); -}); \ No newline at end of file +}; \ No newline at end of file