-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataService.cs
89 lines (78 loc) · 3 KB
/
DataService.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace TelemetryJsonService
{
internal static class DataService
{
public static int Port { get; private set; }
public static string JsonData { get; set; }
public static void StartWebServer()
{
Port = int.Parse(ConfigurationManager.AppSettings["port"]);
Task.Run(() => StartTcpListener(Port));
}
static async Task StartTcpListener(int port)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
Console.WriteLine($"Listening on port {port}");
try
{
while (true)
{
TcpClient client = await tcpListener.AcceptTcpClientAsync();
_ = HandleClientAsync(client);
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
finally
{
tcpListener.Stop();
Console.WriteLine("Listener stopped.");
}
}
/// <summary>
/// Handle an incoming request
/// </summary>
static async Task HandleClientAsync(TcpClient tcpClient)
{
using (NetworkStream stream = tcpClient.GetStream())
{
byte[] buffer = new byte[1024];
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
string request = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine($"Received request: {request}");
// Parse the HTTP method
string httpMethod = "GET";
string[] requestLines = request.Split('\n');
string firstLine = requestLines.FirstOrDefault();
string[] tokens = firstLine?.Split(' ');
if (tokens != null && tokens.Length >= 2)
{
httpMethod = tokens[0];
Console.WriteLine($"HTTP Method: {httpMethod}");
}
string responseText = "HTTP/1.1 200 OK\r\n" +
"Content-Type: application/json\r\n" +
"Access-Control-Allow-Origin: *\r\n" + // Allow requests from any origin
"Access-Control-Allow-Methods: GET, OPTIONS\r\n" + // Specify allowed methods
"Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With\r\n\r\n"; // Specify allowed headers
if ( httpMethod == "GET" )
{
responseText += JsonData;
}
byte[] responseBytes = Encoding.UTF8.GetBytes(responseText);
await stream.WriteAsync(responseBytes, 0, responseBytes.Length);
}
tcpClient.Close();
}
}
}