Websocket connection failed during Websocket handshake

Vajihk Morotell5 years ago

Hi Anton,

I am facing this issue while connecting websocket from webview of my android application, It is working fine in browser but whenever it is getting loaded into webview of android then it is giving below mentioned error.

Websocket connection to ws://xxxx:8082/api/socket : failed error during websocket handshake unexpected response code 503

Anton Tananaev5 years ago

Try using secure connection.

jaroja44 years ago

I Anton, I am using secure connection but I have the same error (failed error during websocket handshake unexpected response code 503). I am using javascript and this is the example code:

  $.ajax({
    type: "POST",
    dataType: "json",
    xhrFields: {
      withCredentials: true,
    },
    data: {
      email: "MyEmail@gmail.com",
      password: "MyPasswd",
    },
    url: "https://MyServer.com/gps/api/session/",
  })
    .done(function (response) {
      console.log("Login OK: " + JSON.stringify(response));
      openWebsocket();
    })
    .fail(function (e) {
      console.log("Login FAIL: " + JSON.stringify(response));

    });

  var openWebsocket = function () {
    var socket;
    socket = new WebSocket("wss://MyServer.com/gps/api/socket");

    socket.onclose = function (event) {
      console.log("WebSocket closed");
    };

    socket.onmessage = function (event) {
      console.log(event.data);
    };
  };

This code works fine on the server where traccar is running but in my local development environment it doesn't work.

Any recommendation?
And thank you very much for the help.

jaroja44 years ago

UPDATE:
https://www.traccar.org/forums/topic/chrome-cross-site-cookie/
I also disable the feature SameSite for default cookie on chrome://flags/#same-site-by-default-cookies and it works to develop