0

I'm trying to add WebSocket connections to my website, so I started out with a basic code to send and receive a string. It was working like it should yesterday, and even after rewritting it exactly the same way, all I get is [object Blob] instead of my string, I've searched for hours now with no success. Here's the code:

Server:

import WebSocket, {WebSocketServer} from 'ws';
const server = new WebSocketServer({port:'8080'});

server.on('connection', socket => {
    socket.on('message', message => {
        socket.send(message);
    });
});

Client:

const socket = new WebSocket('ws://localhost:8080');
var text = document.getElementById("text");

socket.onopen = ({e}) => {
    console.log("Conectado");
}

socket.onmessage = ({data}) => {
    console.log('Mensagem do servidor! ' + data);
}

document.querySelector('button').onclick = () => {
    socket.send('Oi');
}

function updateText(){
    text = document.getElementById("text");
    socket.send(text.value);
}
1
  • I have. The point here is that I never created a blob, and it wasn't a blob the last time I wrote that code. The search doesn't solve my problem. Or does it actually need to be a blob for every string I send using WebSocket? Commented Dec 1, 2021 at 22:28

1 Answer 1

1

After some more hours tinkering with the code, it seems like it was some weird variable type stuff. Only worked when I sent a hardcoded string with the message, so I tried .toString() and it worked like it should, just wasn't reading the message as a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.