loulijun2021
2023-07-19 d218467c185497d51e7fde256394da831a37ed18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template>
  <div>
    <!--    <input v-model="user" type="text">-->
    <!--    <input v-model="message" type="text"><br>-->
    <!--    <button @click="SendAll">发送</button>-->
    <!--    <hr>-->
    <!--    <ul>-->
    <!--      <li v-for="(item, index) in msgList" :key="index">-->
    <!--        {{ item.user }}:&nbsp;&nbsp;&nbsp;&nbsp;{{ item.msg }}-->
    <!--      </li>-->
    <!--    </ul>-->
 
    <button @click="onClickButton">
      手动发送
    </button>
 
    <div style="margin: 200px">{{ msg }}</div>
  </div>
</template>
 
<script>
import * as signalR from '@microsoft/signalr'
 
export default {
  data() {
    return {
      // connection: '',
      // user: '',
      // message: '',
      // msgList: []
      msg: ''
    }
  },
  created() {
    // this.init()
  },
 
  methods: {
 
    onClickButton() {
      this.$signalr.invoke('SendMessage', 'xx')// 用于手动发送消息的方式
        .catch(function(err) {
          return console.error(err)
        })
    }
    //
    // init() {
    //   this.connection = new signalR.HubConnectionBuilder()
    //     .withUrl('http://121.196.36.24:8019/chatHub', {})
    //     .configureLogging(signalR.LogLevel.Error)
    //     .build()
    //   this.connection.on('ReceiveMessage', data => {
    //     this.msgList.push(data)
    //   })
    //
    //   this.connection.start().then(() => {
    //     if (window.Notification) {
    //       if (Notification.permission === 'granted') {
    //         console.log('允许通知')
    //       } else if (Notification.permission !== 'denied') {
    //         console.log('需要通知权限')
    //         Notification.requestPermission((permission) => {
    //           console.log('权限通知', permission)
    //         })
    //       } else if (Notification.permission === 'denied') {
    //         console.log('拒绝通知')
    //       }
    //     } else {
    //       console.error('浏览器不支持Notification')
    //     }
    //     console.log('连接成功')
    //   })
    //
    //   this.connection.on('SendAll', (res) => {
    //     console.log(res, '收到消息')
    //   })
    //
    //   // this.connection.start().then(() => {
    //   //   this.connection.invoke('SendAll', '123', '我连上啦')
    //   // })
    // },
    // SendAll() {
    //   const params = {
    //     user: this.user,
    //     message: this.message
    //   }
    //   this.connection.invoke('SendAll', params)
    // }
  }
}
</script>
 
<style></style>