using System.Collections.Concurrent; using HybirdFrameworkCore.Utils; using log4net; using Microsoft.AspNetCore.SignalR; using Newtonsoft.Json; namespace Service.RealTime; public class MyHub : Hub { private static readonly JsonSerializerSettings settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss", NullValueHandling = NullValueHandling.Ignore }; private static readonly ILog Log = LogManager.GetLogger(typeof(MyHub)); private static readonly ConcurrentDictionary Dictionary = new ConcurrentDictionary(); #region rpc public async Task SendMsg(string toUser, string msg) { Log.Info($"receive {msg}"); await Clients.All.SendAsync("ReceiveMsg", toUser +":"+msg); } public void Login(string uid) { if (!Dictionary.ContainsKey(uid)) { Dictionary[uid] = Context.ConnectionId; } } public void Logout(string uid) { if (ObjUtils.IsNotNullOrWhiteSpace(uid)) { Dictionary.Remove(uid); } } public void InvokeWithoutResult(string s) { RtMsg? rtMsg = JsonConvert.DeserializeObject(s, settings); if (rtMsg != null) { } } public RtMsg? Invoke(RtMsg rtMsg) { if (rtMsg != null) { object result = new object(); RtMsg msg = new RtMsg() { Id = rtMsg.Id, Cmd = rtMsg.Cmd, Datetime = DateTime.Now, FromUser = "swap", Msg = JsonConvert.SerializeObject(result, settings) }; //TODO 根据cmd执行业务 return msg; } return null; } #endregion }