0

In vertx example websocket url hardcoded in html as localhost:

 socket = new WebSocket("ws://localhost:8080/myapp");

Which is the correct, "production" way for specifying the remote address in html, especially in vertx?

zella
  • 4,645
  • 6
  • 35
  • 60

2 Answers2

0

This is not specific to Vert.x at all, but you can use window.location.host to get the current (page) host:

socket = new WebSocket("ws://" + window.location.host + "/myapp");
tsegismont
  • 8,591
  • 1
  • 17
  • 27
0

Based on environment you can load websocket :

 if(location.origin.includes("localhost")){
       this.wsUrl = "http://localhost:8080/myapp";          
    }else{
      this.wsUrl = location.origin +"/myapp";
    }

   socket = new WebSocket(this.wsUrl);

you can use sockjs-client to open websocket : sockjs-client

Have a look at this example . I hope this will help you :)

Community
  • 1
  • 1
Shasha
  • 669
  • 1
  • 8
  • 26