【发布时间】:2018-03-30 19:54:05
【问题描述】:
我正在尝试将 Spring websockets (STOMP) 与 Vue 一起使用,但不知道该怎么做,甚至不知道它是否可行。我的 websockets 可以使用普通的 JS,但是当我尝试使用 Vue 时我卡住了。这是我的vue代码:
var app = new Vue({
el: '#app',
data: {
stompClient: null,
gold: 0
},
methods: {
sendEvent: function () {
this.stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
}
},
created: function () {
this.stompClient = Stomp.over(new SockJS('/gs-guide-websocket'));
this.stompClient.connect()
this.stompClient.subscribe('/topic/greetings', function (greeting) {
console.log(JSON.parse(greeting.body).content);
});
},
})
我的连接和发送功能正在工作,我可以在后端看到消息,但问题是订阅功能。它需要一个回调函数,但这永远不会触发。我也尝试在 vue 中创建一个方法并调用它
this.stompClient.subscribe('/topic/greetings', vueFunc())
但这也不起作用。我在https://github.com/FlySkyBear/vue-stomp 找到了一些库,但我不知道如何使用它,它看起来真的很乱。那我宁愿用纯JS。
有人有解决办法吗?谢谢
【问题讨论】:
标签: javascript spring vuejs2 stomp spring-websocket