yl
2022-11-28 6999a662958e1601d78a2db7d698bbd9c9a44f4d
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using VueWebApi.Models;
using VueWebApi.Tools;
 
namespace VueWebApi.Hubs
{
    public class ChatHub2 : Hub
    {
        /// < summary>
        ///
        /// </ summary>
        public static List<string> Users = new List<string>();
 
        /// <summary>
        /// 未连接事件
        /// </summary>
        /// <returns></returns>
        public override Task OnConnected()
        {
            LogHelper.WriteLogData("执行OnConnected");
            string clientId = GetClientId();
            if (Users.IndexOf(clientId) == -1)
            {
                Users.Add(clientId);
            }
            Send(Users.Count);
            var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
            context.Clients.Client(clientId).updateUserName(clientId);
            return base.OnConnected();
        }
 
        /// <summary>
        /// 重新连接事件
        /// </summary>
        /// <returns></returns>
        public override Task OnReconnected()
        {
            LogHelper.WriteLogData("执行OnReconnected");
            string clientId = GetClientId();
            if (Users.IndexOf(clientId) == -1)
            {
                Users.Add(clientId);
            }
            Send(Users.Count);
            return base.OnReconnected();
        }
 
        /// <summary>
        /// 断开连接事件
        /// </summary>
        /// <param name="stopCalled"></param>
        /// <returns></returns>
        public override Task OnDisconnected(bool stopCalled)
        {
            LogHelper.WriteLogData("执行OnDisconnected");
            string clientId = GetClientId();
 
            if (Users.IndexOf(clientId) > -1)
            {
                Users.Remove(clientId);
            }
            Send(Users.Count);
            return base.OnDisconnected(stopCalled);
        }
 
        /// <summary>
        /// 这对于每个客户端都是唯一的,用于标识连接。
        /// </summary>
        /// <returns></returns>
        private string GetClientId()
        {
            string clientId = "";
 
            //从应用程序传递的客户ID
            if (Context.QueryString["clientId"] != null)
            {
                clientId = this.Context.QueryString["clientId"];
            }
 
            if (string.IsNullOrEmpty(clientId.Trim()))
            {
                clientId = Context.ConnectionId;
            }
            LogHelper.WriteLogData("执行1:" + clientId);
            return clientId;
        }
 
        /// <summary>
        /// Sends the update user count to the listening view.
        /// </summary>
        /// <param name="count">
        /// The count.
        /// </param>
        public void Send(int count)
        {
            LogHelper.WriteLogData("执行count:" + count);
            // Call the addNewMessageToPage method to update clients.
            var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
            context.Clients.All.updateUsersOnlineCount(count);
        }
        /// <summary>
        /// 自己写的一个服务端方法Hello.
        /// </summary>
        /// <param name="msg">参数
        /// </param>
        public void Hello(string msg)
        {
            LogHelper.WriteLogData("执行msg:" + msg);
            var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
            context.Clients.All.clientMethod("server:" + msg);
        }
 
    }
}