/
IamDeveloper
/
Edk
Обзор
Документация
Войти
/
IamDeveloper
/
Edk
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
JabberNotify.cs
70 строк
3 KB
IamDeveloper
init
13 дек 2024, 15:07
13 дек 2024, 15:07
9714be3
Код
Авторство
О чём код?
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Runtime.InteropServices.JavaScript; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace Edk2directum { [Serializable] public struct Message { public string Type { get; set; } public string Text { get; set; } public bool Error { get; set; } } internal class JabberNotify(IConfiguration configuration, ILogger<JabberNotify> logger) : INotify { private readonly ILogger<JabberNotify> _logger = logger; private readonly IConfiguration _configuration = configuration; private readonly string? _msgType = configuration["Jabber:msg_type"]; public async Task NotifyError(string message) { Message notifyMessage = new Message { Error = true, Type = _msgType ?? "NEWEDK", Text = string.Format("{0}:{1}", _msgType ?? "NEWEDK", message) }; await NotifyAsync(notifyMessage); } public async Task NotifyInfo(string message) { Message notifyMessage = new Message { Error = false, Type = _msgType ?? "NEWEDK", Text = string.Format("{0}:{1}", _msgType ?? "NEWEDK", message) }; await NotifyAsync(notifyMessage); } private async Task NotifyAsync(Message message) { string? jabberServerIp = _configuration["Jabber:server"]; string? jabberServerPort = _configuration["Jabber:port"]; if (jabberServerIp == null || jabberServerPort == null) return; try { TcpClient client = new TcpClient(jabberServerIp, int.Parse(jabberServerPort)); MemoryStream ms = new MemoryStream(); XmlSerializer formatter = new XmlSerializer(typeof(Message)); formatter.Serialize(ms, message); byte[] data = ms.ToArray(); NetworkStream stream = client.GetStream(); await stream.WriteAsync(data); stream.Close(); client.Close(); } catch (Exception err) { _logger.LogError($"Ошибка отправки Notify = {err.Message}"); } } } }