index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!--
  2. * @Author: zhangyu
  3. * @Date: 2019-08-26 15:22:13
  4. * @Info:
  5. * @LastEditTime: 2019-09-26 20:16:41
  6. -->
  7. <template>
  8. <div class="notification-box" @click="handleClickRead">
  9. <el-badge :value="unreadNum" :hidden="!unreadNum" :max="99">
  10. <i class="el-icon-message-solid"></i>
  11. </el-badge>
  12. <!-- <el-button type="primary" @click="handleClickConnectMQ">连接MQ</el-button>
  13. <el-button type="primary" @click="handleClickDisconnectMQ">断开MQ</el-button>
  14. <el-button type="primary" @click="handleClickUnsubscribe">停止接收消息</el-button><br><br>
  15. <el-input type="textarea" :autosize="{ minRows: 6, maxRows: 6}" v-model="sendMessage" placeholder="请输入要发送的内容"></el-input>
  16. <br><br>
  17. <el-button type="primary" @click="handleClickSendMessage">发送消息</el-button>
  18. <el-card style="margin-top:20px;text-align:left;">
  19. <div slot="header" class="clearfix">
  20. <span>消息历史</span>
  21. </div>
  22. <div v-for="(message, index) in messageList" :key="index" class="text item">
  23. {{ message }}
  24. </div>
  25. </el-card> -->
  26. </div>
  27. </template>
  28. <script>
  29. import { mapGetters } from 'vuex'
  30. import Stomp from 'stompjs'
  31. import Msmq from './msmq'
  32. import { MQTT_SERVICE, MQTT_USERNAME, MQTT_PASSWORD } from './mqSetting'
  33. export default {
  34. data() {
  35. return {
  36. client: Stomp.client(MQTT_SERVICE),
  37. unreadNum: 0,
  38. sendMessage: '',
  39. messageList: [], // 历史消息
  40. subList: [], // 订阅的消息实例
  41. topics: ["/topic/model.manage"] // 订阅的消息名称
  42. }
  43. },
  44. created () {
  45. this.connect()
  46. // let url = "ws://192.168.20.225:61614/stomp";
  47. // let login = "admin";
  48. // let passcode = "admin";
  49. // let destination = "/topic/datacenter.broadcast";
  50. // let client = Stomp.client(url)
  51. // console.log('钩子:',client)
  52. // var onconnect = function(frame) {
  53. // client.subscribe(destination, function(message) {
  54. // console.log(message.body);
  55. // alert(message.body);
  56. // });
  57. // };
  58. // client.connect(login, passcode, onconnect)
  59. },
  60. computed: {
  61. ...mapGetters('layout', ['userInfo', 'projectId', 'projects'])
  62. },
  63. methods:{
  64. connect() {
  65. this.client = Stomp.client(MQTT_SERVICE)
  66. this.client.reconnect_delay = 5000
  67. var clientid = `sagaMQ-${this.userInfo.userName}`
  68. var headers = {
  69. 'login': MQTT_USERNAME,
  70. 'passcode': MQTT_PASSWORD,
  71. 'client-id': clientid
  72. }
  73. this.client.connect(headers, this.onConnected, this.onFailed)
  74. },
  75. onConnected(frame) {
  76. console.log('Connected: ' + frame)
  77. //订阅多个消息
  78. this.topics.forEach(item => {
  79. let sub = this.client.subscribe(item, this.onmessage, this.onFailed)
  80. this.subList.push(sub)
  81. })
  82. // this.client.subscribe(topic, this.onmessage, this.onFailed)
  83. },
  84. //接收到消息的回调
  85. onmessage(message) {
  86. this.unreadNum = Msmq.handleMsg(message, this.projects, this.unreadNum);
  87. },
  88. // 接收消息失败回调
  89. onFailed(frame) {
  90. console.log('Failed: ' + frame)
  91. },
  92. //停止接收消息
  93. unsubscribe() {
  94. this.subList.forEach((item) => {
  95. item.unsubscribe()
  96. })
  97. },
  98. //断开连接
  99. disconnect() {
  100. this.client.disconnect(function() {
  101. console.log("连接已断开!");
  102. })
  103. },
  104. //发送消息
  105. send(destination, message, headers = {}) {
  106. this.client.send(destination, headers, JSON.stringify(message))
  107. },
  108. handleClickConnectMQ() {
  109. this.connect()
  110. },
  111. handleClickDisconnectMQ() {
  112. this.disconnect()
  113. },
  114. handleClickUnsubscribe() {
  115. this.unsubscribe()
  116. },
  117. handleClickSendMessage() {
  118. this.send('/topic/datacenter.broadcast',this.sendMessage)
  119. },
  120. handleClickRead() {
  121. this.unreadNum = 0;
  122. }
  123. }
  124. }
  125. </script>
  126. <style lang="scss" scoped>
  127. .notification-box{
  128. width: 100%;
  129. height: 100%;
  130. padding: 16px 10px 10px;
  131. box-sizing: border-box;
  132. color: #79869a;
  133. cursor: pointer;
  134. .el-icon-message-solid{
  135. font-size: 22px;
  136. }
  137. /deep/ .el-badge__content{
  138. height: 16px;
  139. line-height: 16px;
  140. border: 1px solid transparent;
  141. }
  142. }
  143. .notification-box:hover{
  144. color: #d3d8e2;
  145. background-color: #3f4f62;
  146. }
  147. </style>