vue 中使用stompjs和sockjs 比较急

发送两次链接一次未定义
clipboard.png
<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>

阅读 9.7k
2 个回答

这个是没有问题的!我查了一下这没有问题!只要你能接收到后台返回的消息就行!

新手上路,请多包涵

怎么引入stomp和sockjs的啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题