log.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <BasicLayout>
  3. <template #wrapper>
  4. <el-card class="box-card">
  5. <el-form>
  6. <el-form-item>
  7. <el-button type="success" icon="el-icon-search" size="mini">状态</el-button>
  8. <el-button type="primary" icon="el-icon-search" size="mini">清空</el-button>
  9. </el-form-item>
  10. </el-form>
  11. <el-row ref="log" :gutter="10" class="mb8">
  12. <el-scrollbar style="height:500px;background-color: black;color: cornflowerblue;">
  13. <ul
  14. style="line-height: 25px;padding-top: 15px;padding-bottom: 15px;min-height: 500px; margin: 0;list-style-type: none;"
  15. >
  16. <li v-for="(item,index) in arrs" :key="index">
  17. {{ item }}
  18. </li>
  19. </ul>
  20. </el-scrollbar>
  21. </el-row>
  22. </el-card>
  23. </template>
  24. </BasicLayout>
  25. </template>
  26. <script>
  27. import { unWsLogout } from '@/api/ws'
  28. export default {
  29. name: 'SysJobLogManage',
  30. data() {
  31. return {
  32. websock: null,
  33. arrs: [],
  34. id: undefined,
  35. group: undefined
  36. }
  37. },
  38. created() {
  39. this.id = this.guid()
  40. this.group = 'log'
  41. this.initWebSocket()
  42. },
  43. destroyed() {
  44. console.log('断开websocket连接')
  45. this.websock.close() // 离开路由之后断开websocket连接
  46. unWsLogout(this.id, this.group).then(response => {
  47. console.log(response.data)
  48. }
  49. )
  50. },
  51. methods: {
  52. initWebSocket() { // 初始化weosocket
  53. console.log(this.$store.state.user.token)
  54. const wsuri = 'ws://127.0.0.1:8000/ws/' + this.id + '/' + this.group + '?token=' + this.$store.state.user.token
  55. this.websock = new WebSocket(wsuri)
  56. this.websock.onmessage = this.websocketonmessage
  57. this.websock.onopen = this.websocketonopen
  58. this.websock.onerror = this.websocketonerror
  59. this.websock.onclose = this.websocketclose
  60. },
  61. websocketonopen() { // 连接建立之后执行send方法发送数据
  62. console.log('连接打开')
  63. // const actions = { 'test': '12345' }
  64. // this.websocketsend(JSON.stringify(actions))
  65. },
  66. websocketonerror() { // 连接建立失败重连
  67. this.initWebSocket()
  68. },
  69. websocketonmessage(e) { // 数据接收
  70. console.log(e.data)
  71. // console.log(this.binaryAgent(e))
  72. // const redata = JSON.parse(e.data)
  73. // console.log(redata)
  74. // this.$refs.log.innerText = e.data + '\n' + this.$refs.log.innerText
  75. this.arrs.unshift(e.data)
  76. },
  77. websocketsend(Data) { // 数据发送
  78. // this.websock.send(Data)
  79. },
  80. websocketclose(e) { // 关闭
  81. unWsLogout(this.id, this.group).then(response => {
  82. console.log(response.data)
  83. }
  84. )
  85. console.log('断开连接', e)
  86. },
  87. guid() {
  88. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  89. var r = Math.random() * 16 | 0; var v = c === 'x' ? r : (r & 0x3 | 0x8)
  90. return v.toString(16)
  91. })
  92. }
  93. }
  94. }
  95. </script>