guoshilong
2023-03-22 10bc47a275dfd896ba4e72f073d9f901c5390597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<template>
  <div>
    <el-button @click="goPage('/wel')">首页</el-button>
    <el-button @click="goPage('/desk/notice')">通知公告</el-button>
    <el-button @click="goPage('/system/user')">用户管理</el-button>
  </div>
</template>
 
<script>
export default {
  name: "websocket-demo",
  data(){
    return{
      webSocket:null,
    }
  },
  mounted() {
    const that = this
    if ("WebSocket" in window) {
      this.webSocket = new WebSocket("ws://localhost:88/websocket/controller")
    }
 
    //连接发生错误的回调方法
    this.webSocket.onerror = function () {
      console.log("连接失败了,建议重试")
    }
 
    this.webSocket.onmessage = function (event) {
      console.log(event,"-----------------------------")
    };
 
    this.webSocket.onclose = function (event) {
      console.log("关闭:" + event)
      that.webSocket.close()
    }
 
  },
  methods:{
    goPage(path){
      this.webSocket.send(path)
    },
 
  },
}
</script>
 
<style scoped>
 
</style>