using langguanApi.Extensions.AutoDI;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
namespace langguanApi.Common.WebSocket
{
///
/// 客户端push消息
///
[ServiceInjection(InjectionType.Singleton)]
public class PushService
{
private readonly IHubContext _hubContext;
///
/// 构造函数
///
///
public PushService(IHubContext hubContext)
{
_hubContext = hubContext;
}
///
/// 全部消息
///
///
///
public async Task SendMessageToAll(object message)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
Console.WriteLine($"gps 推送数据:{JsonConvert.SerializeObject(message, settings)}");
var data = JsonConvert.SerializeObject(message, settings);
await _hubContext.Clients.All.SendAsync("ReceiveMessage", data);
}
}
}