43 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			43 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								using LY.App.Extensions.DI;
							 | 
						|||
| 
								 | 
							
								using Microsoft.AspNetCore.SignalR;
							 | 
						|||
| 
								 | 
							
								using Newtonsoft.Json;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace LY.App.Common.WebSocket
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    /// <summary>
							 | 
						|||
| 
								 | 
							
								    /// 客户端push消息
							 | 
						|||
| 
								 | 
							
								    /// </summary>
							 | 
						|||
| 
								 | 
							
								    [ServiceInjection(InjectionType.Singleton)]
							 | 
						|||
| 
								 | 
							
								    public class PushService
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        private readonly IHubContext<SocketHub> _hubContext;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        public PushService(IHubContext<SocketHub> hubContext)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            _hubContext = hubContext;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 1对1消息
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="connectionId"></param>
							 | 
						|||
| 
								 | 
							
								        /// <param name="message"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task SendMessageToClient(string connectionId, object message)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            await _hubContext.Clients.Client(connectionId).SendAsync("ReceiveMessage", message);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 全部消息
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="message"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task SendMessageToAll(object message)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            var data = JsonConvert.SerializeObject(message);
							 | 
						|||
| 
								 | 
							
								            Console.WriteLine($"推送前端消息:{data}");
							 | 
						|||
| 
								 | 
							
								            await _hubContext.Clients.All.SendAsync("ReceiveMessage", data);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |