guoshilong
2023-03-16 6926dbab01be34206f8ed9940e60efea5f77b0a1
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
50
51
52
<template>
  <div>
    <h1>{{ msg }}</h1>
    <button @click="connect">连接</button>
    <button @click="disconnect">断开连接</button>
    <button @click="sendMessage">msg</button>
    <button @click="sendMessage2">msg2</button>
  </div>
</template>
 
 
<script>
import  io from "socket.io-client";
export default {
  name: 'HelloWorld',
  data () {
    return {
      socketIoClient: null,
      msg: 'Socket.io 测试页'
    }
  },
  methods: {
    connect(){
      let serveUri = 'http://192.168.0.200:10246'
      this.socketIoClient = io.connect(serveUri)
      //监听与服务器的连接状态
      this.socketIoClient.on("connect",()=>{
        console.log(this.socketIoClient,"------------")
      })
 
      this.socketIoClient.on("ClientReceive", (arg) => {
        console.log(arg); // world
      });
 
 
      // this.socketIoClient = io.connect(serveUri, {
      //   'force new connection': true,
      //   'query': 'UID=' + 22222
      // });
    },
    disconnect(){
      this.socketIoClient.disconnect()
    },
    sendMessage(){
      this.socketIoClient.emit("msg","99999999999")
    },
    sendMessage2(){
      this.socketIoClient.emit("msg2","00000")
    }
  },
}
</script>