Files
OliveTin/webui.dev/js/websocket.js
James Read ac0f3ab6f8 feature: Live command output! (#325)
* feature: Live command output WIP

* cicd: Fix live command output build errors

* feature: Live command output WIP, remove stdout/stderr split

* feature: Live command output

* Restore output im stepExecAfter, rename so to ost for OutputStreamer

* Update executor.go

* feature: Remove output from log message, bad for long messages, binary output, and security

* feature: Live command output WIP
2024-05-31 20:56:49 +00:00

80 lines
1.9 KiB
JavaScript

import {
refreshServerConnectionLabel
} from './marshaller.js'
window.ws = null
export function checkWebsocketConnection () {
if (window.ws === null || window.ws.readyState === 3) {
reconnectWebsocket()
}
}
function reconnectWebsocket () {
window.websocketAvailable = false
const websocketConnectionUrl = new URL(window.location.toString())
websocketConnectionUrl.hash = ''
websocketConnectionUrl.pathname += 'websocket'
if (window.location.protocol === 'https:') {
websocketConnectionUrl.protocol = 'wss'
} else {
websocketConnectionUrl.protocol = 'ws'
}
window.websocketConnectionUrl = websocketConnectionUrl
const ws = window.ws = new WebSocket(websocketConnectionUrl.toString())
ws.addEventListener('open', websocketOnOpen)
ws.addEventListener('message', websocketOnMessage)
ws.addEventListener('error', websocketOnError)
ws.addEventListener('close', websocketOnClose)
}
function websocketOnOpen (evt) {
window.websocketAvailable = true
window.ws.send('monitor')
refreshServerConnectionLabel()
window.refreshLoop()
}
function websocketOnMessage (msg) {
// FIXME check msg status is OK
const j = JSON.parse(msg.data)
const e = new Event(j.type)
e.payload = j.payload
switch (j.type) {
case 'EventOutputChunk':
case 'EventConfigChanged':
case 'EventExecutionFinished':
case 'EventEntityChanged':
window.dispatchEvent(e)
break
default:
window.showBigError('ws-unhandled-message', 'handling websocket message', 'Unhandled websocket message type from server: ' + j.type, true)
}
}
function websocketOnError (err) {
window.websocketAvailable = false
window.refreshLoop()
console.log(err)
window.showBigError('ws-connect-error', 'connecting to the websocket', 'Please see your browser console for debugging information.', true)
refreshServerConnectionLabel()
}
function websocketOnClose () {
window.websocketAvailable = false
refreshServerConnectionLabel()
}