You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

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<string, string>
Dictionary = new ConcurrentDictionary<string, string>();
#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<RtMsg>(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
}