Selaa lähdekoodia

Docker.vue 中动态获取 websocket url 的地址

reghao 3 kuukautta sitten
vanhempi
commit
2cb646787d
1 muutettua tiedostoa jossa 25 lisäystä ja 1 poistoa
  1. 25 1
      src/views/devops/machine/Docker.vue

+ 25 - 1
src/views/devops/machine/Docker.vue

@@ -290,9 +290,33 @@ export default {
     // ****************************************************************************************************************
     // WebSocket相关
     // ****************************************************************************************************************
+    getUrl() {
+      const protocol = location.protocol
+      const hostname = location.hostname
+      const port = location.port
+      let prefix
+      if (protocol === 'https:') {
+        if (port === '' || port === 443) {
+          prefix = 'wss://' + hostname
+        } else {
+          prefix = 'wss://' + hostname + ':' + port
+        }
+      } else {
+        if (port === '' || port === 80) {
+          prefix = 'ws://' + hostname
+        } else {
+          prefix = 'ws://' + hostname + ':' + port
+        }
+      }
+
+      var token = '0123456789'
+      var params = 'token=' + token
+      return prefix + '/bgws/frontend?' + params
+    },
     initWebSocket() {
       if ('WebSocket' in window) {
-        const wsUrl = 'ws://localhost:4030/bgws/frontend'
+        const wsUrl = this.getUrl()
+        console.log(wsUrl)
         this.wsClient = new WebSocket(wsUrl)
         const that = this
         this.wsClient.onopen = function() {