鍍金池/ 問(wèn)答/Java  HTML/ vue 中使用stompjs和sockjs 比較急

vue 中使用stompjs和sockjs 比較急

發(fā)送兩次鏈接一次未定義
clipboard.png
<template>
<div>
<div class="page-header" id="tou">

webSocket-stomp測(cè)試程序
<button @click="onStop" >關(guān)閉連接</button>

</div>
<div v-for="x in msg">

<div>{{x}}</div>

</div>
<div class="input-group">

<input type="text" placeholder="發(fā)送信息..." v-model="text">
<span class="input-group-btn"><button type="button" @click="onSend" >發(fā)送</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) {
      //錯(cuò)誤信息
      console.log('Failed: ' + frame)
    },
    onmessage: function (frame) {
      //接收消息
      this.msg.push(frame.body);
    },
    connect: function () {
      //連接服務(wù)器
      this.stomp.connect('', this.onConnected, this.onError);
    },
    onSend(){
      //發(fā)送消息
      this.stomp.send("/app/hi",{},this.text);
    },
    onStop() {
      //關(guān)閉消息
      this.sock.close();
    }
  }
}

</script>

<style scoped>

</style>

回答
編輯回答
兔囡囡

怎么引入stomp和sockjs的啊

2018年5月12日 04:11
編輯回答
情已空

這個(gè)是沒(méi)有問(wèn)題的!我查了一下這沒(méi)有問(wèn)題!只要你能接收到后臺(tái)返回的消息就行!

2018年3月15日 03:24