如何在 html 中为 websocket 连接指定远程地址
How to specify remote address for websocket connection in html
在 vertx example websocket url 中硬编码在 html 中作为本地主机:
socket = new WebSocket("ws://localhost:8080/myapp");
在 html 中指定远程地址的正确 "production" 方法是什么,尤其是在 vertx 中?
这根本不是 Vert.x 特有的,但您可以使用 window.location.host
获取当前(页面)主机:
socket = new WebSocket("ws://" + window.location.host + "/myapp");
基于您可以加载 websocket 的环境:
if(location.origin.includes("localhost")){
this.wsUrl = "http://localhost:8080/myapp";
}else{
this.wsUrl = location.origin +"/myapp";
}
socket = new WebSocket(this.wsUrl);
您可以使用 sockjs-client 打开 websocket : sockjs-client
看看这个 。
我希望这会对你有所帮助:)
在 vertx example websocket url 中硬编码在 html 中作为本地主机:
socket = new WebSocket("ws://localhost:8080/myapp");
在 html 中指定远程地址的正确 "production" 方法是什么,尤其是在 vertx 中?
这根本不是 Vert.x 特有的,但您可以使用 window.location.host
获取当前(页面)主机:
socket = new WebSocket("ws://" + window.location.host + "/myapp");
基于您可以加载 websocket 的环境:
if(location.origin.includes("localhost")){
this.wsUrl = "http://localhost:8080/myapp";
}else{
this.wsUrl = location.origin +"/myapp";
}
socket = new WebSocket(this.wsUrl);
您可以使用 sockjs-client 打开 websocket : sockjs-client
看看这个