webrequest.html 631 B

123456789101112131415161718192021222324252627
  1. <script>
  2. var url = new URL(location.href)
  3. const port = new URLSearchParams(url.search).get("port")
  4. const ipcRenderer = require('electron').ipcRenderer
  5. let count = 0
  6. function checkFinish() {
  7. count++
  8. if (count === 2) {
  9. ipcRenderer.send('websocket-success')
  10. }
  11. }
  12. var conn = new WebSocket(`ws://127.0.0.1:${port}/websocket`)
  13. conn.onopen = data => conn.send('foo')
  14. conn.onmessage = wsMsg
  15. function wsMsg(msg) {
  16. if (msg.data === 'bar') {
  17. checkFinish()
  18. } else {
  19. ipcRenderer.send('fail')
  20. }
  21. }
  22. fetch(`http://127.0.0.1:${port}/`).then(() => {
  23. checkFinish()
  24. })
  25. </script>