Blame view

netcore/src/Modularity/Message/NCC.Message/Extensions/WebSocketClientCollection.cs 2.04 KB
de2bd2f9   “wangming”   项目初始化
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
  using NCC.Dependency;
  using NCC.Message.Entitys.Model.IM;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace NCC.Message.Extensions
  {
      /// <summary>
      /// WebSocket客户端集合
      /// </summary>
      [SuppressSniffer]
      public class WebSocketClientCollection
      { 
          /// <summary>
          /// 在线用户
          /// </summary>
          public static List<WebSocketClient> _clients { get; set; } = new List<WebSocketClient>();
  
          /// <summary>
          /// locker
          /// </summary>
          public static readonly object locker = new object();
  
          /// <summary>
          /// 添加集合
          /// </summary>
          /// <param name="client"></param>
          public static void Add(WebSocketClient client)
          {
              _clients.Add(client);
          }
  
          /// <summary>
          /// 移除集合
          /// </summary>
          /// <param name="client"></param>
          public static void Remove(WebSocketClient client)
          {
              _clients.Remove(client);
          }
  
          /// <summary>
          /// 获取WebSocket客户端
          /// </summary>
          /// <param name="clientId"></param>
          /// <returns></returns>
          public static WebSocketClient Get(string clientId)
          {
              var client = _clients.FirstOrDefault(c => c.ConnectionId == clientId);
              return client;
          }
  
          /// <summary>
          /// 获取WebSocket客户端
          /// </summary>
          /// <param name="userId"></param>
          /// <returns></returns>
          public static WebSocketClient GetUser(string userId)
          {
              var client = _clients.FirstOrDefault(c => c.UserId == userId);
              return client;
          }
          /// <summary>
          /// 获取聊天室 客户端集合
          /// </summary>
          /// <param name="roomNo"></param>
          /// <returns></returns>
          public static List<WebSocketClient> GetRoomClients(string roomNo)
          {
              var client = _clients.Where(c => c.RoomNo == roomNo);
              return client.ToList();
          }
      }
  }