I'm testing websocket connection. I want to send JSON object from client to Server:
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 8082});
wss.on("connection", (ws) => {
console.log('server is connected');
ws.on("message", (mesg) => {
const a = mesg.toString();
console.log(a);
});
ws.on("close", () => {
console.log("Client has disconnected!");
});
});
On the server side:
console.log(mesg) returns <Buffer>
console.log(a) returns object Object]
JSON.parse(a) throws error message.
I'm at a loss. What am I doing wrong? Thank you!