WebSocket not working in Safari (macOS)

macphisto 6 years ago

Hi,

Why websocket only works in Chrome and Firefox and not in Safari? Am I missing something?

Any help will be greatly appreciated,

My Code:

function initializeCapture(){
    $.ajax({
        url: "https://xxx.xxxxx.xxx/api/session",
        dataType: "json",
        type: "POST",
        crossDomain: true,
        xhrFields: { withCredentials: true },
        data: {
            email: "user@domain.com",
            password: "password"
        },
        success: function(err, resp, body){
            
            openWebsocket(resp);
        }
    });
    
    var openWebsocket = function(resp){
        
        var ws;
        ws = new WebSocket('wss://xxx.xxxxx.xxx/api/socket', [], {'headers': { 'Cookie': resp, 'SameSite':'none'}});
    
        ws.onmessage = function (evt) 
       { 
          var received_msg = evt.data;
          dataparsed = JSON.parse(received_msg);
        
          if (dataparsed.devices) {
            console.log(dataparsed.devices); 
          }

          if (dataparsed.positions) {
            console.log(dataparsed.positions)
          }

          
          if (dataparsed.events){
              console.log(dataparsed.events);
          }

        
       };
    
       ws.onerror = function (error){
           console.log(error);
       }
    
       ws.onclose = function()
       { 
          // websocket is closed.
          console.log("Connection is closed..."); 
       };
    
       window.onbeforeunload = function(event) {
          socket.close();
       };
    };
}
Mr.wolf 4 years ago

Did you find a solution to the problem ?

I'm facing the same problem in Chrome and Firefox

Irwin Ortiz 2 years ago

same problem

Anton Tananaev 2 years ago

It's probably not working because of the CORS restrictions on cookies.

Irwin Ortiz 2 years ago

Thanks for your answer. The only way to log into the websocket is cookies, right?

Anton Tananaev 2 years ago

Yes, correct.