MessageStream.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="18">
  4. <div class="movie-list" style="height: 70vh;">
  5. <!-- 注意需要给 el-scrollbar 设置高度,判断是否滚动是看它的height判断的 -->
  6. <el-scrollbar style="width: 100%; height: 100%;">
  7. <el-row class="movie-list">
  8. <div
  9. v-for="(item, index) in dataList"
  10. :key="index"
  11. :md="6"
  12. :sm="12"
  13. :xs="12"
  14. >
  15. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  16. <el-card class="box-card">
  17. <el-row>
  18. <el-col :md="20">
  19. <el-row>
  20. <div style="padding: 14px">
  21. <span style="left: 0;margin-bottom: 0px;color: black;">{{ item.content }}</span>
  22. </div>
  23. </el-row>
  24. </el-col>
  25. </el-row>
  26. </el-card>
  27. </el-row>
  28. </div>
  29. </el-row>
  30. </el-scrollbar>
  31. </div>
  32. </el-col>
  33. <el-col :md="6">
  34. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  35. <el-col>
  36. <el-button
  37. size="mini"
  38. type="danger"
  39. class="el-icon-delete"
  40. @click="clear"
  41. >清空</el-button>
  42. </el-col>
  43. </el-row>
  44. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  45. <el-card class="box-card">
  46. <el-input
  47. v-model="text"
  48. type="textarea"
  49. :rows="3"
  50. placeholder="有什么新鲜事想分享给大家?"
  51. />
  52. <el-row style="text-align: right">
  53. <el-button
  54. size="mini"
  55. type="submit"
  56. class="el-icon-message"
  57. @click="onSubmit"
  58. >发送</el-button>
  59. </el-row>
  60. </el-card>
  61. </el-row>
  62. </el-col>
  63. </el-row>
  64. </template>
  65. <script>
  66. export default {
  67. name: 'MessageStream',
  68. filters: {
  69. ellipsis(value) {
  70. if (!value) return ''
  71. const max = 50
  72. if (value.length > max) {
  73. return value.slice(0, max) + '...'
  74. }
  75. return value
  76. }
  77. },
  78. data() {
  79. return {
  80. dataList: [],
  81. text: '',
  82. websocket: null
  83. }
  84. },
  85. created() {
  86. document.title = 'MessageStream'
  87. this.initWebSocket()
  88. },
  89. methods: {
  90. clear() {
  91. this.dataList = []
  92. },
  93. onSubmit() {
  94. this.$notify({
  95. message: this.text,
  96. type: 'info',
  97. duration: 1000
  98. })
  99. this.text = ''
  100. },
  101. initWebSocket: function() {
  102. var url = 'wss:' + process.env.VUE_APP_SERVER_URL + '/ws/log?token=1234567890'
  103. this.websock = new WebSocket(url)
  104. this.websock.onopen = this.websocketOnopen
  105. this.websock.onerror = this.websocketOnerror
  106. this.websock.onmessage = this.websocketOnmessage
  107. this.websock.onclose = this.websocketOnclose
  108. },
  109. websocketOnopen: function() {
  110. console.log('WebSocket连接成功')
  111. // 心跳检测重置
  112. // this.heartCheck.reset().start();
  113. },
  114. websocketOnerror: function(e) {
  115. console.log('WebSocket连接发生错误')
  116. this.reconnect()
  117. },
  118. websocketOnmessage: function(e) {
  119. const evtData = JSON.parse(e.data)
  120. const payload = evtData.payload
  121. this.dataList.push(payload)
  122. },
  123. websocketOnclose: function(e) {
  124. console.log('connection closed (' + e.code + ')')
  125. this.reconnect()
  126. },
  127. websocketSend(text) { // 数据发送
  128. try {
  129. this.websock.send(text)
  130. } catch (err) {
  131. console.log('send failed (' + err.code + ')')
  132. }
  133. },
  134. reconnect() {
  135. const that = this
  136. if (that.lockReconnect) return
  137. that.lockReconnect = true
  138. // 没连接上会一直重连,设置延迟避免请求过多
  139. setTimeout(function() {
  140. console.info('尝试重连...')
  141. that.initWebSocket()
  142. that.lockReconnect = false
  143. }, 5000)
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. /*处于手机屏幕时*/
  150. @media screen and (max-width: 768px) {
  151. .movie-list {
  152. padding-top: 8px;
  153. padding-left: 0.5%;
  154. padding-right: 0.5%;
  155. }
  156. }
  157. .movie-list {
  158. padding-top: 15px;
  159. padding-left: 6%;
  160. padding-right: 6%;
  161. }
  162. </style>