-// Open Websocket back to ControlPi we were loaded from:
-const websocket = new WebSocket("ws://" + window.location.host)
+var timeout = 125
-// When Websocket is ready request all clients from bus:
-websocket.addEventListener('open', function (event) {
- websocket.send(JSON.stringify({target: '', command: 'get clients'}))
-})
+function connect() {
+ // Open websocket back to ControlPi we were loaded from:
+ const ws = new WebSocket("ws://" + window.location.host)
-// When receiving message from ControlPi through Websocket:
-websocket.addEventListener('message', function (event) {
- const message = JSON.parse(event.data)
- if (message['sender'] == '') {
- processBusMessage(message)
- } else {
- processClientMessage(message)
- }
-})
+ // When websocket is ready request all clients from bus:
+ ws.addEventListener('open', function (e) {
+ timeout = 125
+ ws.send(JSON.stringify({target: '', command: 'get clients'}))
+ })
+
+ // When websocket goes away delete all clients from the container and
+ // try to reconnect:
+ ws.addEventListener('close', function (e) {
+ const container = document.getElementById('web-debug-container')
+ while (container.firstChild) {
+ container.removeChild(container.lastChild);
+ }
+ const log = document.getElementById('web-debug-log')
+ log.innerHTML += `Connection closed: ${e.reason}<br />`
+ if (timeout < 8000) {
+ timeout *= 2
+ }
+ log.innerHTML += `Trying to reconnect in ${timeout/1000} s …<br />`
+ log.scrollTop = log.scrollHeight
+ setTimeout(function() {
+ connect()
+ }, timeout)
+ })
+ ws.addEventListener('error', function (e) {
+ const log = document.getElementById('web-debug-log')
+ log.innerHTML += `Error: ${e.message} <br />`
+ log.innerHTML += "Closing connection …<br />"
+ log.scrollTop = log.scrollHeight
+ ws.close()
+ })
+
+ // When receiving message from ControlPi through websocket process it:
+ ws.addEventListener('message', function (e) {
+ const message = JSON.parse(e.data)
+ if (message['sender'] == '') {
+ processBusMessage(message, ws)
+ } else {
+ processClientMessage(message)
+ }
+ const log = document.getElementById('web-debug-log')
+ log.innerHTML += JSON.stringify(message, null, 4) + '<br />'
+ log.scrollTop = log.scrollHeight
+ })
+}
+connect()
// Remove (if unregistered) or create (if not existent) elements for
// clients and update interface information:
-function processBusMessage(message) {
+function processBusMessage(message, ws) {
if (message['event'] == 'unregistered') {
// On deregistration delete client element if it exists:
const clientElement = document.getElementById(message['client'])
const clientElement = document.getElementById(message['client'])
if (clientElement == null) {
// Create element for client if not existent:
- const main = document.getElementById('ControlPi Debug')
- main.appendChild(createClient(message['client'],
- message['plugin']))
+ const container = document.getElementById('web-debug-container')
+ container.appendChild(createClient(message['client'],
+ message['plugin']))
}
// Crate message elements for receives interface:
const receiveContainer = document.getElementById(message['client'] + ' Receives')
receiveContainer.innerHTML = ''
for (const template of message['receives']) {
if (template['command'] != null) {
- receiveContainer.appendChild(createForCommand(template))
+ receiveContainer.appendChild(createForCommand(template, ws))
} else {
receiveContainer.appendChild(createObject(template, 'template'))
}
const clientElement = document.getElementById(message['sender'])
if (clientElement == null) {
// Create section for client if not existent:
- const main = document.getElementById('ControlPi Debug')
- main.appendChild(createClient(message.sender, ''))
+ const container = document.getElementById('web-debug-container')
+ container.appendChild(createClient(message.sender, ''))
}
// Update last received message:
const lastContainer = document.getElementById(message['sender'] + ' Last')
}
// Create div, form and table for command template:
-function createForCommand(template) {
+function createForCommand(template, ws) {
const div = document.createElement('div')
div.setAttribute('class', 'object')
const form = document.createElement('form')
}
}
}
- websocket.send(JSON.stringify(data))
+ ws.send(JSON.stringify(data))
})
const table = document.createElement('table')
for (const key in template) {