websocket.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { buttonResults } from '../resources/vue/stores/buttonResults.js'
  2. export function checkWebsocketConnection () {
  3. reconnectWebsocket()
  4. }
  5. window.websocketAvailable = false
  6. async function reconnectWebsocket () {
  7. if (window.websocketAvailable) {
  8. return
  9. }
  10. try {
  11. window.websocketAvailable = true
  12. for await (const e of window.client.eventStream()) {
  13. handleEvent(e)
  14. }
  15. } catch (err) {
  16. console.error('Websocket connection failed: ', err)
  17. }
  18. window.websocketAvailable = false
  19. console.log('Reconnecting websocket...')
  20. }
  21. function handleEvent (msg) {
  22. const typeName = msg.event.value.$typeName.replace('olivetin.api.v1.', '')
  23. const j = new Event(typeName)
  24. j.payload = msg.event.value
  25. switch (typeName) {
  26. case 'EventOutputChunk':
  27. case 'EventConfigChanged':
  28. case 'EventEntityChanged':
  29. window.dispatchEvent(j)
  30. break
  31. case 'EventExecutionFinished':
  32. case 'EventExecutionStarted':
  33. buttonResults[msg.event.value.logEntry.executionTrackingId] = msg.event.value.logEntry
  34. window.dispatchEvent(j)
  35. break
  36. default:
  37. console.warn('Unhandled websocket message type from server: ', typeName)
  38. window.showBigError('ws-unhandled-message', 'handling websocket message', 'Unhandled websocket message type from server: ' + typeName, true)
  39. }
  40. }