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);
}