[英]Unable to connect to WebSocket with Vue plugin
I'd like to write a vue-plugin to get handy WebSocket methods like connect()
and subscribe()
in my Vue application.我想编写一个 vue 插件,以便在我的 Vue 应用程序中获得方便的 WebSocket 方法,例如
connect()
和subscribe()
。 I've got a problem with connecting to WebSocket, it only works when I call connect()
method in the mounted hook and load the whole page (like with the browser refresh button).我在连接到 WebSocket 时遇到问题,它仅在我在挂载的钩子中调用
connect()
方法并加载整个页面时才有效(例如使用浏览器刷新按钮)。 In another case, when I first load the page and then call the connect()
method explicitly by the button click, the connection isn't established.在另一种情况下,当我第一次加载页面然后通过单击按钮显式调用
connect()
方法时,连接没有建立。
My vue-plugin code:我的 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;
My vue component:我的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>
In the case, I uncomment the mounted hook, after page load I see the console log like this:在这种情况下,我取消注释挂载的钩子,页面加载后我看到控制台日志如下:
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: ""}
And everything works correct.一切正常。 But, if I comment the mounted hook and want to connect with the WebSocket by the button click, the console log looks like this:
但是,如果我评论挂载的钩子并希望通过单击按钮与 WebSocket 连接,控制台日志如下所示:
websocket tester launched
connecting...
trying to connect websocket
Opening Web Socket...
and that's it, the connection isn't established.就是这样,连接没有建立。 Why this happens and how to fix it?
为什么会发生这种情况以及如何解决?
OK I figured it out.好的,我想通了。 The problem line was
const stompClient = getStompClient("http://localhost:8080/ws");
问题行是
const stompClient = getStompClient("http://localhost:8080/ws");
in the plugin.在插件中。 I've moved it to the connect method and store as
ws.object
.我已将其移至 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 => {
Later, I use ws.stompClient
and it works fine.后来,我使用
ws.stompClient
并且效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.