|
| 1 | +import { getAccessToken } from './auth' |
| 2 | + |
| 3 | +const HEARTBEAT_MESSAGE = `{ |
| 4 | + "type": "WebSocketEvent", |
| 5 | + "topic": "openhab/websocket/heartbeat", |
| 6 | + "payload": "PING", |
| 7 | + "source": "WebSocketTestInstance" |
| 8 | +}` |
| 9 | + |
| 10 | +const openWSConnections = [] |
| 11 | + |
| 12 | +function newWSConnection (path, messageCallback, readyCallback, errorCallback, heartbeatCallback, heartbeatInterval) { |
| 13 | + // Create a new WebSocket connection |
| 14 | + const socket = new WebSocket(path, [`org.openhab.ws.accessToken.base64.${btoa(getAccessToken())}`, 'org.openhab.ws.protocol.default']) |
| 15 | + |
| 16 | + // Handle WebSocket connection opened |
| 17 | + socket.onopen = (event) => { |
| 18 | + socket.setKeepalive(heartbeatInterval) |
| 19 | + if (readyCallback) { |
| 20 | + readyCallback(event) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // Handle WebSocket message received |
| 25 | + socket.onmessage = (event) => { |
| 26 | + let evt = event.data |
| 27 | + try { |
| 28 | + evt = JSON.parse(event.data) |
| 29 | + } catch (e) { |
| 30 | + console.error('Error while parsing message', e) |
| 31 | + } |
| 32 | + messageCallback(evt) |
| 33 | + } |
| 34 | + |
| 35 | + // Handle WebSocket error |
| 36 | + socket.onerror = (event) => { |
| 37 | + console.error('WebSocket error', event) |
| 38 | + if (errorCallback) { |
| 39 | + errorCallback(event) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // WebSocket keep alive |
| 44 | + socket.setKeepalive = (seconds = 5) => { |
| 45 | + console.debug('Setting keepalive interval seconds', seconds) |
| 46 | + socket.clearKeepalive() |
| 47 | + socket.keepaliveTimer = setInterval(() => { |
| 48 | + if (heartbeatCallback) { |
| 49 | + heartbeatCallback() |
| 50 | + } else { |
| 51 | + socket.send(HEARTBEAT_MESSAGE) |
| 52 | + } |
| 53 | + }, seconds * 1000) |
| 54 | + } |
| 55 | + |
| 56 | + socket.clearKeepalive = () => { |
| 57 | + if (socket.keepaliveTimer) clearInterval(socket.keepaliveTimer) |
| 58 | + delete socket.keepaliveTimer |
| 59 | + } |
| 60 | + |
| 61 | + // Add the new WebSocket connection to the list |
| 62 | + openWSConnections.push(socket) |
| 63 | + console.debug(`new WS connection: ${socket.url}, ${openWSConnections.length} open connections`) |
| 64 | + console.debug(openWSConnections) |
| 65 | + |
| 66 | + return socket |
| 67 | +} |
| 68 | + |
| 69 | +export default { |
| 70 | + /** |
| 71 | + * Connect to the websocket at the given path. |
| 72 | + * |
| 73 | + * @param {string} path path to connect to, e.g. `/ws` |
| 74 | + * @param {fn} messageCallback |
| 75 | + * @param {fn} [readyCallback=null] |
| 76 | + * @param {fn} [errorCallback=null] |
| 77 | + * @param {fn} [heartbeatCallback=null] heartbeat callback to use instead of the default PING/PONG |
| 78 | + * @param {number} [heartbeatInterval=5] heartbeat interval in seconds |
| 79 | + * @return {WebSocket} |
| 80 | + */ |
| 81 | + connect (path, messageCallback, readyCallback = null, errorCallback = null, heartbeatCallback = null, heartbeatInterval = 5) { |
| 82 | + return newWSConnection(path, messageCallback, readyCallback, errorCallback, heartbeatCallback, heartbeatInterval) |
| 83 | + }, |
| 84 | + /** |
| 85 | + * Close the given websocket connection. |
| 86 | + * |
| 87 | + * @param {WebSocket} socket |
| 88 | + * @param {fn} [callback=null] callback to execute on connection close |
| 89 | + */ |
| 90 | + close (socket, callback = null) { |
| 91 | + if (!socket) return |
| 92 | + if (openWSConnections.indexOf(socket) >= 0) { |
| 93 | + openWSConnections.splice(openWSConnections.indexOf(socket), 1) |
| 94 | + } |
| 95 | + console.debug(`WS connection closed: ${socket.url}, ${openWSConnections.length} open connections`) |
| 96 | + console.debug(openWSConnections) |
| 97 | + socket.onclose = (event) => { |
| 98 | + if (callback) { |
| 99 | + callback(event) |
| 100 | + } |
| 101 | + } |
| 102 | + socket.close() |
| 103 | + socket.clearKeepalive() |
| 104 | + } |
| 105 | +} |
0 commit comments