40 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			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="hubContext"></param>
 | 
						|
        public PushService(IHubContext<SocketHub> hubContext)
 | 
						|
        {
 | 
						|
            _hubContext = 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);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |