index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div>
  3. <div class="title">滚筒2</div>
  4. <ul class="flex-ul">
  5. <li >温度:<span class="content">{{data.wendu_after}}℃</span></li>
  6. <li >张力:<span class="content">{{data.zhangli_after}}N/m</span></li>
  7. <li >振动:<span class="content">{{data.zhendong_after}}mm</span> </li>
  8. </ul>
  9. </div>
  10. </template>
  11. <script>
  12. import { mapState } from "vuex";
  13. export default {
  14. data() {
  15. return {
  16. data: {
  17. zhangli_after: "1943",
  18. wendu_after: "10",
  19. zhendong_after:"100",
  20. },
  21. };
  22. },
  23. mounted() {
  24. this.websocket = new WebSocket(
  25. `ws://${this.websocketIP}/hbase/ws/belt/263`
  26. );
  27. this.initWebSocket();
  28. },
  29. methods: {
  30. initWebSocket() {
  31. // 连接错误
  32. this.websocket.onerror = () => {
  33. console.log(
  34. "WebSocket连接发生错误 状态码:" + this.websocket.readyState
  35. );
  36. };
  37. // 连接成功
  38. this.websocket.onopen = () => {
  39. console.log(
  40. "WebSocket连接成功 状态码:" + this.websocket.readyState
  41. );
  42. };
  43. // 收到消息的回调
  44. this.websocket.onmessage = (event) => {
  45. if (JSON.parse(event.data).length) {
  46. this.changeState(JSON.parse(event.data));
  47. }
  48. };
  49. // 连接关闭的回调
  50. this.websocket.onclose = () => {
  51. console.log(
  52. "WebSocket连接关闭 状态码:" + this.websocket.readyState
  53. );
  54. };
  55. // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
  56. this.$once("hook:beforeDestroy", () => {
  57. this.websocket.close();
  58. console.log("关闭websocket连接");
  59. });
  60. },
  61. changeState(data) {
  62. this.data = data[0];
  63. console.log("下级搭接设备数据展示为:", this.data);
  64. },
  65. close() {
  66. this.websocket.close();
  67. console.log("关闭websocket连接");
  68. },
  69. },
  70. computed: {
  71. ...mapState(["websocketIP"]),
  72. },
  73. };
  74. </script>
  75. <style scoped lang="less">
  76. .title {
  77. color: #4adefe;
  78. font-size: 20px;
  79. font-weight: bolder;
  80. text-align: center;
  81. margin-top: 10px;
  82. }
  83. .flex-ul {
  84. display: flex;
  85. justify-content: center;
  86. align-items: center;
  87. list-style: none;
  88. width: 100%;
  89. flex-flow: row wrap;
  90. .content {
  91. color: #f3db5c;
  92. right: 10px;
  93. position: absolute;
  94. margin-right: -10px;
  95. }
  96. ul {
  97. margin-top: 10px;
  98. margin-left: -20px;
  99. }
  100. li {
  101. width: 120%;
  102. height: 100%;
  103. color: #4adefe;
  104. display: flex;
  105. /* flex-direction: column; */
  106. font-size: 18px;
  107. position: relative;
  108. margin-top: -5px;
  109. margin-left: -30px;
  110. line-height: 50px; //30
  111. padding-left: 0px;
  112. }
  113. }
  114. </style>