Websocket error

macphisto5 years ago

Hello,

I having a 503 error in websocket connection. The strange is this works in one moment but after that never works again.

This is the code (I took it from this forum)

$.ajax({
    url: "http://xxxxx.xx:8082/api/session",
    dataType: "json",
    type: "POST",
    data: {
        email: "xxxx@xxxx.xx",
        password: "xxxxxx"
    },
    success: function(sessionResponse){
        console.log(sessionResponse);
        openWebsocket();
    }
});

var openWebsocket = function(){
    var ws;
    ws = new WebSocket('ws://xxxxx.xx:8082/api/socket');

    ws.onmessage = function (evt) 
   { 
      var received_msg = evt.data;
      dataparsed = JSON.parse(received_msg);
      if (dataparsed.positions) {
          console.log("POSITIONS");    
      }
      if (dataparsed.devices) {
          console.log("DEVICES");    
      }
      console.log(dataparsed);
      // console.log("Message is received...");
   };

   ws.onclose = function()
   { 
      // websocket is closed.
      console.log("Connection is closed..."); 
   };

   window.onbeforeunload = function(event) {
      socket.close();
   };
};

Any help will be appreciated