Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Websocket no se volverá a conectar a menos que cierre la pestaña del navegador y lo reinicie

Tengo un servidor web con websockets configurados en un ESP8266. La aplicación funciona bien tanto en el lado del cliente como en el del servidor, enviando y recibiendo datos. Sin embargo, si el lado del servidor se desconecta (apaga y enciende o carga un nuevo código), el cliente (Chrome) no se volverá a conectar al websocket. Puedo volver a cargar/actualizar la página web y afirma (según el registro de la consola) que se está conectando al websocket, pero no es así. La única solución que he encontrado que funciona es cerrar la pestaña y luego reiniciar una nueva sesión.

Mi código se basa en gran medida en este tutorial de Random Nerd Tutorials

 var gateway = `ws://${window.location.hostname}/ws`; var websocket; function initWebSocket() { console.log('Trying to open a WebSocket connection...'); websocket = new WebSocket(gateway); websocket.onopen = onOpen; websocket.onclose = onClose; websocket.onmessage = onMessage; // <-- add this line } function onOpen(event) { console.log('Connection opened'); } function onClose(event) { console.log('Connection closed'); setTimeout(initWebSocket, 2000); }

¿Hay algo que falta en el código anterior para hacerlo más confiable?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Probablemente necesite usar setInterval . Prueba esto, puede que tengas que modificarlo un poco.

 var gateway = `ws://${window.location.hostname}/ws`; var websocket, sockTimer=null; function initWebSocket() { console.log('Trying to open a WebSocket connection...'); websocket = new WebSocket(gateway); websocket.onopen = onOpen; websocket.onerror = onError; //  new websocket.onclose = onClose; websocket.onmessage = onMessage; // <-- add this line } function onOpen(event) { clearInterval(sockTimer) // <= better console.log('Connection opened'); } function onError() { // <= New sockTimer = setInterval(init, 1000 * 60); }; function onClose(event) { console.log('Connection closed'); //setTimeout(initWebSocket, 2000); sockTimer = setInterval(initWebSocket, 1000 * 60); // <=new }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!