123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Microsoft.AspNetCore.SignalR;
- using OASystem.API.OAMethodLib.Hub.HubClients;
- using OASystem.API.OAMethodLib.SignalR.Hubs;
- using OASystem.API.OAMethodLib.SignalR.HubService;
- using System.DirectoryServices.Protocols;
- using System.Text.RegularExpressions;
- using static OASystem.API.OAMethodLib.Hub.Hubs.ChatHub;
- using static OASystem.API.OAMethodLib.JWTHelper;
- namespace OASystem.API.OAMethodLib.Hub.Hubs
- {
- public class ChatHub : Hub<IChatClient>
- {
- private readonly ILogger<ChatHub> _logger;
- private readonly IHttpContextAccessor _accessor;
- private readonly CommonService _common;
- /// <summary>
- /// 已登录的用户信息
- /// </summary>
- public static List<UserModel> OnlineUser { get; set; } = new List<UserModel>();
- public ChatHub(ILogger<ChatHub> logger, CommonService common, IHttpContextAccessor accessor)
- {
- _logger = logger;
- _common = common;
- _accessor = accessor;
- }
- /// <summary>
- /// SignalR登录验证
- /// </summary>
- public async Task SignalRLogin(int userId)
- {
- string connid = Context.ConnectionId;
- bool status = false;
- if (!OnlineUser.Exists(u => u.ConnectionId == connid))
- {
- status = true;
- OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
- }
- //给当前连接返回消息
- await Clients.Client(connid).SendAsync("SignalRLoginResponse", status);
- }
- /// <summary>
- /// 客户端连接服务端
- /// </summary>
- /// <returns></returns>
- public override Task OnConnectedAsync()
- {
- var id = Context.ConnectionId;
- _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
- //验证Token
- //var token = _accessor.HttpContext.Request.Query["access_token"];
- //var user = JwtHelper.SerializeJwt(token);
- return base.OnConnectedAsync();
- }
- /// <summary>
- /// 客户端断开连接
- /// </summary>
- /// <param name="exception"></param>
- /// <returns></returns>
- public override Task OnDisconnectedAsync(Exception exception)
- {
- var id = Context.ConnectionId;
- _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
- return base.OnDisconnectedAsync(exception);
- }
-
- /**
- * 测试
- * */
- /// <summary>
- /// 给所有客户端发送消息
- /// </summary>
- /// <returns></returns>
- public async Task SendMessage(string data)
- {
- Console.WriteLine("Have one Data!");
- await Clients.All.SendAll(_common.SendAll(data));
- }
- public class UserModel
- {
- public int UserId { get; set; }
- public string ConnectionId { get; set; }
- public string GroupName { get; set; }
- }
- }
- }
|