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

151
Views
Javascript envía solo un mensaje a través de websocket en el navegador

Tengo un servidor websocket ejecutándose en un ESP32 que funciona perfectamente.

Mi problema es que si envío un mensaje al servidor a través de un sitio web, el servidor de socket en el ESP32 ejecuta el .send (mensaje) solo la primera vez y ya no envía ningún mensaje.

Si envío un mensaje desde las herramientas de desarrollo/la consola en Chrome, siempre funciona.

En este punto, supongo que mi problema parece no estar en el lado del servidor sino en el lado del cliente.

Este es mi código html/javascript que se ejecuta dentro de Chrome:

 <!DOCTYPE html> <head> <title>WH_Quiz</title> <style> body { background: url(Images/SET_1/A1_INTRO.png) no-repeat center center fixed; -webkit-background-size: cover; background-size: cover; } </style> </head> <body> <script> // clear localStorage window.localStorage.clear(); // open websocket var url = "ws://192.168.4.1:1337"; websocket = new WebSocket(url); websocket.onopen = function (event) { console.log('websocket is open'); websocket.send("L"); }; // Keep track of clicked keys var isKeyPressed = { 'a': false, // ASCII code for 'a' 'b': false, // ASCII code for 'b' // ... Other keys to check for custom key combinations }; document.onkeydown = (keyDownEvent) => { // Prevent default key actions, if desired keyDownEvent.preventDefault(); // Track down key click isKeyPressed[keyDownEvent.key] = true; // Check described custom shortcut if (isKeyPressed['a']) { //for example we want to check if a is clicked //do something console.log('a has been pressed'); websocket.onopen = function (event) { console.log('a has been pressed and wesocket is open'); window.location.href = '1_1.html'; // öffne Fragenkatalog 1 websocket.send("4G"); }; }; // Check described custom shortcut if (isKeyPressed['b']) { //for example we want to check if b is clicked //do something console.log('b has been pressed'); websocket.onopen = function (event) { window.location.href = '2_1.html'; // öffne Fragenkatalog 2 websocket.send("4R"); }; }; }; </script> </body> </html>

En los registros de mi consola, puedo ver que ingresa la declaración if "isKeyPressed" pero no está ejecutando la función "websocket.onopen = (evento)".

¿Qué estoy haciendo mal?

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

0

websocket.onopen solo se activa la primera vez que se abre el socket. Como ya está abierto en este punto, debería poder llamar directamente a la función de envío.

En otras palabras:

 if (isKeyPressed['b']) { //for example we want to check if b is clicked //do something console.log('b has been pressed'); websocket.onopen = function (event) { window.location.href = '2_1.html'; // öffne Fragenkatalog 2 websocket.send("4R"); }; };

se convierte en:

 if (isKeyPressed['b']) { //for example we want to check if b is clicked //do something console.log('b has been pressed'); window.location.href = '2_1.html'; // öffne Fragenkatalog 2 websocket.send("4R"); };
about 4 years ago · Juan Pablo Isaza Report

0

No sé mucho sobre websockets, pero parece que la función onopen() es como la función onready() en discord.js. Solo se ejecuta cuando se inicia el script/se abre el websocket.

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!