Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
Websocket is not connecting with javascript

So i'm trying to connect to a websocket server using javascript. I tried doing this with NODEJS with the following code and it worked.

const WebSocket = require('ws');
const ws = new WebSocket('<Websocket Address>', {
  origin: '<URL>'
});

ws.on('open', function open() {
  ws.send(JSON.stringify({"event":"auth","args":["<My authentication token>"]}))
  ws.send(JSON.stringify({"event":"send logs","args":[null]}))
  console.log('connected');
});

ws.on('close', function close() {
  console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
  console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
  console.log(data);
  
  }, 500);
Is there a way I can write this in plain javascript? Like in script tags? I've read that the following code should work but it's giving me this error Uncaught DOMException: "An invalid or illegal string was specified"

const ws = new WebSocket('<Websocket address>', {
  origin: '<URL>'
});

ws.on('open', function open() {
  ws.send(JSON.stringify({"event":"auth","args":["<My authentication Token>"]}))
  ws.send(JSON.stringify({"event":"send logs","args":[null]}))
  console.log('connected');
});

ws.on('close', function close() {
  console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
  console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
  console.log(data);
  
  }, 500);
Could anyone tell me what i'm doing wrong?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda