2024-09-01 10:11:10 +00:00
|
|
|
|
using langguanApi.Extensions.AutoDI;
|
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace langguanApi.Common.WebSocket
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 客户端push消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ServiceInjection(InjectionType.Singleton)]
|
|
|
|
|
|
public class PushService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IHubContext<SocketHub> _hubContext;
|
|
|
|
|
|
/// <summary>
|
2024-09-02 06:24:54 +00:00
|
|
|
|
/// 构造函数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hubContext"></param>
|
|
|
|
|
|
public PushService(IHubContext<SocketHub> hubContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_hubContext = hubContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
2024-09-01 10:11:10 +00:00
|
|
|
|
/// 全部消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task SendMessageToAll(object message)
|
|
|
|
|
|
{
|
|
|
|
|
|
var settings = new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
|
};
|
2024-09-02 06:24:54 +00:00
|
|
|
|
Console.WriteLine($"gps 推送数据:{JsonConvert.SerializeObject(message, settings)}");
|
2024-09-01 10:11:10 +00:00
|
|
|
|
var data = JsonConvert.SerializeObject(message, settings);
|
|
|
|
|
|
await _hubContext.Clients.All.SendAsync("ReceiveMessage", data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|