-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
66 lines (53 loc) · 1.83 KB
/
Program.cs
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
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseWebSockets();
app.Map("/ws", async context =>
{
if (context.WebSockets.IsWebSocketRequest)
{
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await EchoLoop(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
});
async Task EchoLoop(WebSocket webSocket)
{
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
string filePath = "DataBase.json";
var receivedText = Encoding.UTF8.GetString(buffer, 0, result.Count);
SaveTextToJsonFile(receivedText, filePath);
Console.WriteLine($"Received: {receivedText}");
var sendText = Encoding.UTF8.GetBytes($"Echo: {receivedText}");
await webSocket.SendAsync(new ArraySegment<byte>(sendText, 0, sendText.Length), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
static void SaveTextToJsonFile(string text, string filePath)
{
var textMessage = new TextMessage
{
Timestamp = DateTime.Now,
Content = text
};
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine(JsonConvert.SerializeObject(textMessage));
writer.WriteLine();
}
}
app.Run("http://localhost:8855");
public class TextMessage
{
public DateTime Timestamp { get; set; }
public string Content { get; set; }
}