github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/assets/scripts/oauth-clients.js (about)

     1  function listenToRealtimeOAuthClientsUpdates(w, d) {
     2    const token = d.querySelector('body').dataset.token
     3  
     4    let url = w.location.host + '/realtime/'
     5    if (w.location.protocol === 'http:') {
     6      url = 'ws://' + url
     7    } else {
     8      url = 'wss://' + url
     9    }
    10  
    11    const socket = new WebSocket(url)
    12    socket.onopen = () => {
    13      const authMsg = JSON.stringify({ method: 'AUTH', payload: token })
    14      socket.send(authMsg)
    15      const subscribeMsg = JSON.stringify({
    16        method: 'SUBSCRIBE',
    17        payload: { type: 'io.cozy.oauth.clients' },
    18      })
    19      socket.send(subscribeMsg)
    20    }
    21    socket.onerror = (err) => {
    22      console.error(err)
    23    }
    24    socket.onmessage = (message) => {
    25      const { event } = JSON.parse(message.data)
    26  
    27      if (event === 'DELETED') {
    28        w.location.reload()
    29      }
    30    }
    31  }
    32  
    33  ;(function (w, d) {
    34    listenToRealtimeOAuthClientsUpdates(w, d)
    35  
    36    const refreshBtn = d.querySelector('#refresh-btn')
    37    refreshBtn.addEventListener('click', () => {
    38      window.location.reload()
    39    })
    40  })(window, document)