简体   繁体   中英

How change connection of socket.io depend of the server

I am new on this, I have a socket client connection I want to change depending on the server I am using it, I have two, one is the developer server and the other is the production server how I can change de connection It depends on which my code is?

this I have so I change before commit and push to the production server manually

var host = 'http://233.88.98.70:8080'; // local
// var host = "https://miserverprduction.com:10800" // prod

// create intance and connect sockets
var socket = io.connect(host, {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 9999,
});

Is any solution for do it automatic?

I don't know if this is what you mean, but you could have a toggle on your front-end..

For reading ENV variables and using those when hosting, we would need to know what webserver you're using.. But for educational purposes..

using localStorage for example..

localStorage.setItem("mode", 'local');

var host = 'http://233.88.98.70'; // local
var mode = localStorage.getItem("mode");
if(mode === "PROD"){
  host = "https://miserverprduction.com:10800"
}

Then you can have a toggle on your front-end OR you could manually assign it to production for exmaple :

localStorage.setItem("mode", 'PROD');

NOTE : But this would be acceissible for the user also.. So probably should be avoided!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM