发送两次链接一次未定义
<template>
<div>
<div class="page-header" id="tou">
webSocket-stomp测试程序
<button @click="onStop" >关闭连接</button>
</div>
<div v-for="x in msg">
<div>{{x}}</div>
</div>
<div class="input-group">
<input type="text" placeholder="发送信息..." v-model="text">
<span class="input-group-btn"><button type="button" @click="onSend" >发送</button></span>
</div>
</div>
</template>
<script>
//var stomp=''
export default {
name: "test",
data(){
return{
stomp :Stomp.over(this.sock),
msg:[],
text:''
}
},
mounted () {
this.connect()
},
methods:{
onConnected: function (frame) {
var topic = '/topic/hi'
this.stomp.subscribe(topic, this.onmessage)
},
onError: function (frame) {
//错误信息
console.log('Failed: ' + frame)
},
onmessage: function (frame) {
//接收消息
this.msg.push(frame.body);
},
connect: function () {
//连接服务器
this.stomp.connect('', this.onConnected, this.onError);
},
onSend(){
//发送消息
this.stomp.send("/app/hi",{},this.text);
},
onStop() {
//关闭消息
this.sock.close();
}
}
}
</script>
<style scoped>
</style>
这个是没有问题的!我查了一下这没有问题!只要你能接收到后台返回的消息就行!