123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using Microsoft.AspNetCore.SignalR;
- using OASystem.API.OAMethodLib.Hub.HubClients;
- using OASystem.API.OAMethodLib.SignalR.Hubs;
- using OASystem.API.OAMethodLib.SignalR.HubService;
- using static OASystem.API.OAMethodLib.JWTHelper;
- namespace OASystem.API.OAMethodLib.Hub.Hubs
- {
-
- [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
- public class ChatHub : Hub<IChatClient>
- {
- private readonly ILogger<ChatHub> _logger;
- private readonly CommonService _common;
-
-
-
-
- public ChatHub(ILogger<ChatHub> logger, CommonService common)
- {
- _logger = logger;
- _common = common;
- }
-
-
-
- public async Task SignalRLogin(int userId)
- {
- string connid = Context.ConnectionId;
- string result = $"[{connid}]";
- string userId1 = Context.UserIdentifier;
- if (!UserStore.OnlineUser.Exists(u => u.ConnectionId == connid))
- {
- result += "上线成功!";
- UserStore.OnlineUser.Add(new UserModel() { UserId = userId, ConnectionId = connid, GroupName = "FMGJ-OASystem" });
- }
- else
- {
- result += "已上线!";
- }
-
- await Clients.Client(connid).SendAsync("SignalRLoginResponse", result);
- }
-
-
-
-
- public override Task OnConnectedAsync()
- {
- var httpContext = Context.GetHttpContext();
- if (httpContext != null)
- {
- string token = httpContext.Request.Headers["Authorization"];
-
- if (!string.IsNullOrEmpty(token))
- {
- TokenModelJwt tokenModelJwt = JwtHelper.SerializeJwt(token.ToString().Split(" ")[1]);
- string result = "";
- if (tokenModelJwt != null)
- {
- var connId = Context.ConnectionId;
-
- UserStore.OnlineUser.Add(new UserModel() { UserId = tokenModelJwt.UserId, ConnectionId = connId, GroupName = "FMGJ-OASystem" });
- _logger.LogInformation($"Client ConnectionId=> [[{connId}]] UserId=> [[{tokenModelJwt.UserId}]] Already Connection Server!");
-
- Clients.Clients(connId).ReceiveMessage(_common.ReceiveMessage($"[{tokenModelJwt.UserName}({tokenModelJwt.Role})] 已上线!"));
-
- }
- }
- }
-
-
- return base.OnConnectedAsync();
- }
-
-
-
-
-
- public override Task OnDisconnectedAsync(Exception exception)
- {
- var connId = Context.ConnectionId;
- _logger.LogInformation($"Client ConnectionId=> [[{connId}]] Already Close Connection Server!");
- var model = UserStore.OnlineUser.Find(u => u.ConnectionId == connId);
- int count = UserStore.OnlineUser.RemoveAll(u => u.ConnectionId == connId);
- if (model != null)
- {
- var onlineUser = UserStore.OnlineUser.FindAll(u => u.GroupName == model.GroupName);
- Clients.Clients(connId).ReceiveMessage(_common.ReceiveMessage($"[UserID=>{model.UserId} ConnectionId=> {model.ConnectionId} ] 已下线!"));
- }
- return base.OnDisconnectedAsync(exception);
- }
-
-
-
-
-
- public async Task SendMessage(string data)
- {
- Console.WriteLine("Have one Data!");
- await Clients.All.SendAll(_common.SendAll(data));
- await Clients.Caller.SendAll(_common.SendCaller(data));
- }
-
-
-
-
-
-
- public async Task SendSystemToUser(string id, string message) => await Clients.Client(id).SendAsync("ReceiveMessage", message);
-
-
-
-
-
-
- public async Task SendSystemToAllUser(string message) => await Clients.All.SendAsync("ReceiveMessage", message);
- }
- }
|