Home > Enterprise >  Websocket sending blob object on live server and string on local server
Websocket sending blob object on live server and string on local server

Time:09-27

I am new to Ubuntu and recently created a project with WebSocket.

Everything works as expected on local server but on live server I get following error:

Uncaught SyntaxError: Unexpected token 'o', "[object Blob]" is not valid JSON at JSON.parse (anonymous) at WebSocket.gotMessageFromServer

I get this error here (wss.onmessage)

function gotMessageFromServer(message) {
    var signal = JSON.parse(message.data); // here
..

And server side code is

wss.broadcast = function (data) {
    this.clients.forEach(function (client) {
        if (client.readyState === WebSocket.OPEN) {
            client.send(data);
        }
    });
};

And message format is:

const dataToServer = JSON.stringify({
    'type': 'Initiate',
    'params': {
        'params1': value1,
        'params2': value1,
    }
});
wss.send(dataToServer);

Please help. Thanks in advance.

CodePudding user response:

Server updated:

websocket.on('message', function message(data, isBinary) {
  const message = isBinary ? data : data.toString();
  // Continue as before.
});

websocket.on('close', function close(code, data) {
  const reason = data.toString();
  // Continue as before.
});
  • Related