环境:
.net core3.1
一.服务端配置(Startup)
ConfigureServices

Configure

二、自定义类继承Hub?
public class SignalRHub: Hub
{
private readonly IHubContext<SignalRHub> _hubContext = null;
public SignalRHub(IHubContext<SignalRHub> context)
{
_hubContext = context;
}
public override Task OnConnectedAsync()
{
Console.WriteLine($"{Context.ConnectionId}已连接");
return base.OnConnectedAsync();
}
public void ReadClientMsgMethod(string msg)
{
Console.WriteLine($"{ Context.ConnectionId}客户端发来信息:" + msg);
}
/// <summary>
/// 全员发送
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public string CallAllClients(string msg)
{
Console.WriteLine("============全员发送==========");
Clients.All.SendAsync("CallByHub", "全员发送>>>>>>>>>>" + msg);
return MethodInfo.GetCurrentMethod()?.Name + "";
}
//发送消息--发送给所有连接的客户端
public Task SendMessage(string msg)
{
return _hubContext.Clients.All.SendAsync("CallByHub", msg);
}
}
每天进步一点!
|