簡體   English   中英

無法使用 Vue 插件連接到 WebSocket

[英]Unable to connect to WebSocket with Vue plugin

我想編寫一個 vue 插件,以便在我的 Vue 應用程序中獲得方便的 WebSocket 方法,例如connect()subscribe() 我在連接到 WebSocket 時遇到問題,它僅在我在掛載的鈎子中調用connect()方法並加載整個頁面時才有效(例如使用瀏覽器刷新按鈕)。 在另一種情況下,當我第一次加載頁面然后通過單擊按鈕顯式調用connect()方法時,連接沒有建立。

我的 vue 插件代碼:

import SockJS from "sockjs-client";
import Stomp from "webstomp-client";

const WebSocketTester = {
  install(Vue, options) {
    console.log("websocket tester launched");
    let connected = false;
    const ws = {
      connected: () => connected
    };

    const stompClient = getStompClient("http://localhost:8080/ws");

    const connect = () => {
      return new Promise((resolve, reject) => {
        if (connected) {
          reject("Already connected !");
          return;
        }
        console.log("trying to connect websocket");
        stompClient.connect({}, frame => {
          console.log("got websocket frame:");
          console.log(frame);
          if (frame.command == "CONNECTED") {
            connected = true;
            resolve();
          } else {
            reject("Could not connect with " + url);
          }
        });
      });
    };

    ws.connect = () => {
      return connect();
    };

    Vue.prototype.$ws = ws;
  }
};

const getStompClient = webSocketUrl => {
  const socket = new SockJS(webSocketUrl);
  return Stomp.over(socket);
};

export default WebSocketTester;

我的Vue組件:

<template>
  <div class="hello">
    <button @click="connect">Connect with websocket</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    msg: String
  },
  methods: {
    connect() {
      console.log("connecting...");
      this.$ws.connect().catch(error => {
        console.log("could not connect by click");
        console.log(error);
      });
    }
  },
  mounted() {
    // this works well
    // this.$ws.connect().catch(error => {
    //   console.log("could not connect in mounted");
    //   console.log(error);
    // });
  }
};
</script>

在這種情況下,我取消注釋掛載的鈎子,頁面加載后我看到控制台日志如下:

websocket tester launched
trying to connect websocket
Opening Web Socket...
Web Socket Opened...
DEPRECATED: undefined is not a recognized STOMP version. In next major client version, this will close the connection.
>>> CONNECT
>>> length 52
<<< CONNECTED
connected to server undefined
got websocket frame:
Frame {command: "CONNECTED", headers: {…}, body: ""}

一切正常。 但是,如果我評論掛載的鈎子並希望通過單擊按鈕與 WebSocket 連接,控制台日志如下所示:

websocket tester launched
connecting...
trying to connect websocket
Opening Web Socket...

就是這樣,連接沒有建立。 為什么會發生這種情況以及如何解決?

好的,我想通了。 問題行是const stompClient = getStompClient("http://localhost:8080/ws"); 在插件中。 我已將其移至 connect 方法並存儲為ws.object

        if (connected) {
          reject("Already connected !");
          return;
        }
        ws.stompClient = getStompClient("http://localhost:8080/ws");
        console.log("trying to connect websocket");
        ws.stompClient.connect({}, frame => {

后來,我使用ws.stompClient並且效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM