-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotifyServiceHelper.cs
More file actions
113 lines (104 loc) · 3.96 KB
/
NotifyServiceHelper.cs
File metadata and controls
113 lines (104 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using NLog;
using System.Text;
namespace AGVSystemCommonNet6.Notify
{
public class NotifyServiceHelper
{
static Logger logger = LogManager.GetCurrentClassLogger();
public static async Task WebsocketNotification(HttpContext context)
{
var tcs = new TaskCompletionSource<object>();
System.Net.WebSockets.WebSocket? client = null;
try
{
if (context.WebSockets.IsWebSocketRequest)
{
client = await context.WebSockets.AcceptWebSocketAsync();
NotifyServiceHelper.OnMessage += OnMessageRecieved;
await tcs.Task;
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}
catch (TaskCanceledException ex)
{
Console.WriteLine($"Notify register is disconnect.");
client?.Dispose();
}
void OnMessageRecieved(object sender, NotifyMessage notify)
{
_ = Task.Run(async () =>
{
try
{
client?.ReceiveAsync(new ArraySegment<byte>(new byte[20]), CancellationToken.None);
var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(notify));
if (client.State != System.Net.WebSockets.WebSocketState.Open)
{
NotifyServiceHelper.OnMessage -= OnMessageRecieved;
client.Dispose();
tcs.SetCanceled();
}
await client.SendAsync(new ArraySegment<byte>(data), System.Net.WebSockets.WebSocketMessageType.Text, true, CancellationToken.None);
}
catch (Exception ex)
{
client?.Dispose();
}
});
}
}
public class NotifyMessage
{
public enum NOTIFY_TYPE
{
info, warning, error, success
}
public NOTIFY_TYPE type { get; set; } = NOTIFY_TYPE.info;
public string message { get; set; } = "";
public bool show { get; set; } = true;
}
public static event EventHandler<NotifyMessage> OnMessage;
public static async Task INFO(string message, bool show = true)
{
await NotifyAsync(NotifyMessage.NOTIFY_TYPE.info, message, show);
}
public static async Task WARNING(string message, bool show = true)
{
await NotifyAsync(NotifyMessage.NOTIFY_TYPE.warning, message, show);
}
public static async Task ERROR(string message, bool show = true)
{
await NotifyAsync(NotifyMessage.NOTIFY_TYPE.error, message, show);
}
public static async Task SUCCESS(string message, bool show = true)
{
await NotifyAsync(NotifyMessage.NOTIFY_TYPE.success, message, show);
}
public static async Task NotifyAsync(NotifyMessage.NOTIFY_TYPE type, string message, bool show)
{
logger.Trace($"[Notify]-[{type}] {message}");
var handler = OnMessage;
if (handler != null)
{
try
{
OnMessage?.Invoke("", new NotifyMessage
{
type = type,
message = message,
show = show
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}