index.vue 2.8 KB

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