| | |
| | | using HslCommunication.WebSocket; |
| | | using Microsoft.Extensions.Hosting; |
| | | using Microsoft.Extensions.Logging; |
| | | using Newtonsoft.Json; |
| | | using Org.BouncyCastle.Asn1.Ocsp; |
| | | using SqlSugar.Extensions; |
| | | using System; |
| | |
| | | using System.Text; |
| | | using System.Text.Json; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Caches; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.Seed; |
| | | |
| | | namespace WIDESEA_Core |
| | | { |
| | | public class WebSocketHostedService : IHostedService |
| | | { |
| | | private readonly DBContext _dbContext; |
| | | private readonly ICacheService _cacheService; |
| | | private readonly WebSocketServer _webSocketServer; |
| | | |
| | | public WebSocketHostedService(WebSocketServer webSocketServer) |
| | | public WebSocketHostedService(DBContext dbContext, ICacheService cacheService, WebSocketServer webSocketServer) |
| | | { |
| | | _webSocketServer = webSocketServer; |
| | | _dbContext = dbContext; |
| | | _cacheService = cacheService; |
| | | _webSocketServer.OnClientConnected += webSocketServer_OnClientConnected; |
| | | } |
| | | |
| | | private void webSocketServer_OnClientConnected(WebSocketSession session) |
| | | { |
| | | string MessageInfo = _cacheService.Get("MessageInfo"); |
| | | if (!string.IsNullOrEmpty(MessageInfo)) |
| | | { |
| | | List<Message>? messages = JsonConvert.DeserializeObject<List<Message>>(MessageInfo); |
| | | if (messages != null && messages.Count > 0) |
| | | { |
| | | foreach (var item in messages) |
| | | { |
| | | object obj = new |
| | | { |
| | | title = item.MessageGroupBy, |
| | | name = item.MessageName, |
| | | message = item.MessageInfo, |
| | | date = DateTime.Now.ToString(), |
| | | }; |
| | | //_webSocketServer.PublishAllClientPayload(obj.Serialize()); |
| | | _webSocketServer.SendClientPayload(session, obj.Serialize()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | { |
| | | WebSocketSession? webSocketSession = _webSocketServer.OnlineSessions.FirstOrDefault(x => x.Url.Contains(App.User.UserName)); |
| | | string MessageInfo = _cacheService.Get("MessageInfo"); |
| | | if (!string.IsNullOrEmpty(MessageInfo)) |
| | | { |
| | | List<Message>? messages = JsonConvert.DeserializeObject<List<Message>>(MessageInfo); |
| | | if (messages != null && messages.Count > 0) |
| | | { |
| | | foreach (var item in messages) |
| | | { |
| | | object obj = new |
| | | { |
| | | title = item.MessageGroupBy, |
| | | name = item.MessageName, |
| | | message = item.MessageInfo, |
| | | date = item.Date, |
| | | }; |
| | | //_webSocketServer.PublishAllClientPayload(obj.Serialize()); |
| | | if (webSocketSession != null) |
| | | _webSocketServer.SendClientPayload(webSocketSession, obj.Serialize()); |
| | | } |
| | | } |
| | | } |
| | | await Task.CompletedTask; |
| | | } |
| | | |
| | |
| | | { |
| | | await Task.CompletedTask; |
| | | } |
| | | |
| | | public class Message |
| | | { |
| | | public int Id { get; set; } |
| | | public string MessageGroupBy { get; set; } |
| | | public string MessageName { get; set; } |
| | | public string MessageInfo { get; set; } |
| | | public DateTime Date { get; set; } |
| | | } |
| | | } |
| | | } |