-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
90 lines (76 loc) · 3.45 KB
/
Program.cs
File metadata and controls
90 lines (76 loc) · 3.45 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
using System.Net;
using System.Text;
using Windows.Media.Control;
namespace NowPlaying
{
internal static class HttpServer
{
private static HttpListener? _listener;
private const string Url = "http://localhost:8000/";
private static string _html = File.ReadAllText("index.html");
private static async Task HandleIncomingConnections()
{
var runServer = true;
// While a user hasn't visited the `shutdown` url, keep on handling requests
while (runServer)
{
// Will wait here until we hear from a connection
var ctx = await _listener?.GetContextAsync()!;
// Peel out the requests and response objects
var req = ctx.Request;
var resp = ctx.Response;
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if ((req.HttpMethod == "POST") && (req.Url?.AbsolutePath == "/shutdown"))
{
Console.WriteLine("Shutdown requested");
runServer = false;
}
var output = "";
switch (req.Url?.AbsolutePath)
{
case "/getInfo":
var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();
var currentSession = sessionManager.GetCurrentSession();
if (currentSession != null)
{
var mediaProperties = currentSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
output = $"♫ {mediaProperties.Title} - {mediaProperties.Artist}";
}
var songData = Encoding.UTF8.GetBytes(output);
resp.ContentType = "text/plain";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = songData.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(songData);
resp.Close();
break;
case "/index.html":
case "/":
var htmlData = Encoding.UTF8.GetBytes(_html);
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = htmlData.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(htmlData);
break;
default:
resp.Close();
break;
}
}
}
public static void Main(string[] args)
{
// Create a Http server and start listening for incoming connections
_listener = new HttpListener();
_listener.Prefixes.Add(Url);
_listener.Start();
Console.WriteLine("Listening for connections on {0}", Url);
// Handle requests
var listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
// Close the listener
_listener.Close();
}
}
}