yl
2022-08-30 72d62c3296c968bbca7c610fd6248306fe2b46c2
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
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace VueWebApi.Hubs
{
    public class ChatHub : Hub
    {
        /// <summary>
        /// 静态用户列表
        /// </summary>
        private IList<string> userList = UserInfo.userList;
 
 
 
        /// <summary>
        /// 用户的connectionID与用户名对照表
        /// </summary>
        private readonly static Dictionary<string, string> _connections = new Dictionary<string, string>();
 
 
 
        /// <summary>
        /// 发送函数,前端触发该函数给服务器,服务器在将消息发送给前端,(Clients.All.(函数名)是全体广播,另外Clients提供了组播,广播排除,组播排除,指定用户播发等等)
        /// 该函数名在前端使用时一定要注意,前端调用该函数时,函数首字母一定要小写
        /// </summary>
        /// <param name="name1">发起者</param>
        /// <param name="name2">消息接收者</param>
        /// <param name="cont">消息内容</param>
        public void SendByGroup(string name1, string name2, string cont)
        {
            //Client内为用户的id,是唯一的,SendMessage函数是前端函数,意思是服务器将该消息推送至前端
            Clients.Client(_connections[name2]).SendMessage("来自用户:" + name1 + " 内容:" + cont + "" + DateTime.Now.ToString("yyyy/MM/ddhh:mm:ss") + "的消息推送!");
        }
 
 
 
        /// <summary>
        /// 用户上线函数
        /// </summary>
        /// <param name="name"></param>
        public void SendLogin(string name)
        {
            if (!userList.Contains(name))
            {
                userList.Add(name);
                //这里便是将用户id和姓名联系起来
                _connections.Add(name, Context.ConnectionId);
            }
            else
            {
                //每次登陆id会发生变化
                _connections[name] = Context.ConnectionId;
            }
            //新用户上线,服务器广播该用户名
            Clients.All.loginUser(userList);
        }
    }
    public class UserInfo
    {
        public static IList<string> userList = new List<string>();
    }
}