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()
{
console.log("Connection is closed...");
};
window.onbeforeunload = function(event) {
socket.close();
};
};
}
Did you find a solution to the problem ?
I'm facing the same problem in Chrome and Firefox
It's probably not working because of the CORS restrictions on cookies.
Thanks for your answer. The only way to log into the websocket is cookies, right?
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(); }; }; }