lg_backend/langguanApi/Common/WebSocket/PushService.cs

32 lines
1018 B
C#

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>
/// 全部消息
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
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);
}
}
}