-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
132 lines (114 loc) · 4.31 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using NAudio.CoreAudioApi;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace SoundCloudRemoteSite
{
class Program
{
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
private static MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
private static MMDevice _playbackDevice;
public static void SystemVolumeConfigurator()
{
_playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
}
public static int GetVolume()
{
return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
public static void SetVolume(int volumeLevel)
{
if (volumeLevel < 0 || volumeLevel > 100)
throw new ArgumentException("Volume must be between 0 and 100!");
_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
}
public class Mobile : WebSocketBehavior
{
protected override void OnOpen()
{
Console.WriteLine("[MOBILE] Connected");
}
protected override void OnMessage(MessageEventArgs e)
{
if (e.Data == "volume")
{
SystemVolumeConfigurator();
this.Send("volume:" + GetVolume());
return;
}
if (e.Data.StartsWith("setvolume:"))
{
SetVolume(int.Parse(e.Data.Split(':')[1]));
Console.WriteLine("[MOBILE] Volume: " + GetVolume());
return;
}
desktop.WebSocketServices.Broadcast(e.Data);
}
protected override void OnClose(CloseEventArgs e)
{
Console.WriteLine("[MOBILE] Disconnected");
}
}
public class Desktop : WebSocketBehavior
{
protected override void OnOpen()
{
Console.WriteLine("[DESKTOP] Connected");
}
protected override void OnMessage(MessageEventArgs e)
{
mobile.WebSocketServices.Broadcast(e.Data);
}
protected override void OnClose(CloseEventArgs e)
{
Console.WriteLine("[DESKTOP] Disconnected");
}
}
static WebSocketServer mobile = new WebSocketServer(IPAddress.Parse(GetLocalIPAddress()), 15768);
static WebSocketServer desktop = new WebSocketServer(IPAddress.Parse("127.0.0.1"), 15769);
static void Main(string[] args)
{
Console.Title = "SoundCloud Server";
SystemVolumeConfigurator();
new Thread(() =>
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:80/");
listener.Prefixes.Add("http://" + GetLocalIPAddress() + ":80/");
listener.Start();
while (true)
{
HttpListenerContext context = listener.GetContext();
HttpListenerResponse response = context.Response;
Console.WriteLine("[MOBILE] Player opened");
byte[] buffer = Properties.Resources.index;
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
}
}).Start();
mobile.AddWebSocketService<Mobile>("/soundcloud/mobile");
desktop.AddWebSocketService<Desktop>("/soundcloud/desktop");
mobile.Start();
desktop.Start();
Console.Read();
}
}
}