0
votes

I am building a web app that uses socket.io and web worker api to get updates from the server every second. one of the buttons is opening a new window, and when the new window is opened the socket.io stop working and getting bad request response

//part of the code

var socket = io({transports: ['polling']});

worker = new Worker("web_worker.js");

worker.onmessage = function(event){
   socket.emit("check_for_updates");   
} 

//this function is called when a button is pressed
function new_window(client)
{
    var uid = client.uid;
    var new_window = window.open("/new?uid="+uid);
}

the errors shown in the console:

polling-xhr.js:270 GET http://localhost/socket.io/?EIO=3&transport=polling&t=Mv0tQCC&sid=LnzFU8ClZrY0NHK5AA-r 400 (Bad Request)

polling-xhr.js:270 POST http://localhost/socket.io/?EIO=3&transport=polling&t=Mv0tQC-&sid=LnzFU8ClZrY0NHK5AA-r 400 (Bad Request)

1

1 Answers

0
votes

So i manged to worked it out- the problem was that in the new window i opened a new socket with the default setting, that caused the first socket to crash. My solution was using namespace from the Sockt.io docks-

server side:

var main_socket = io(http);
socket = main_socket.of('/first');
socket.once("connection", (socket) =>{
    console.log("user connected");

    socket.on("disconnect", function(){
        console.log("user disconnected");
    });

var second_socket = main_socket.of('/second');
second_socket.once("connection", (second_socket) =>{
    console.log("second connection");

client side:

//part of the code

var socket = io(nsp="/first",Option={transports: ['polling']});

worker = new Worker("web_worker.js");

worker.onmessage = function(event){
   socket.emit("check_for_updates");   
} 

//this function is called when a button is pressed
function new_window(client)
{
    var socket = io(nsp="/second",Option={transports: ['polling']});
    var uid = client.uid;
    var new_window = window.open("/new?uid="+uid);
}

I hope it will help someone in the future