Skip to content

Commit 93ae914

Browse files
committed
source
1 parent 135cb31 commit 93ae914

2 files changed

Lines changed: 537 additions & 0 deletions

File tree

Program.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Sockets;
5+
using System.Threading;
6+
using NAudio.CoreAudioApi;
7+
using WebSocketSharp;
8+
using WebSocketSharp.Server;
9+
10+
namespace SoundCloudRemoteSite
11+
{
12+
class Program
13+
{
14+
public static string GetLocalIPAddress()
15+
{
16+
var host = Dns.GetHostEntry(Dns.GetHostName());
17+
foreach (var ip in host.AddressList)
18+
{
19+
if (ip.AddressFamily == AddressFamily.InterNetwork)
20+
{
21+
return ip.ToString();
22+
}
23+
}
24+
throw new Exception("No network adapters with an IPv4 address in the system!");
25+
}
26+
27+
private static MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
28+
private static MMDevice _playbackDevice;
29+
30+
public static void SystemVolumeConfigurator()
31+
{
32+
_playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
33+
}
34+
35+
public static int GetVolume()
36+
{
37+
return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
38+
}
39+
40+
public static void SetVolume(int volumeLevel)
41+
{
42+
if (volumeLevel < 0 || volumeLevel > 100)
43+
throw new ArgumentException("Volume must be between 0 and 100!");
44+
45+
_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
46+
}
47+
48+
public class Mobile : WebSocketBehavior
49+
{
50+
protected override void OnOpen()
51+
{
52+
Console.WriteLine("[MOBILE] Connected");
53+
}
54+
55+
protected override void OnMessage(MessageEventArgs e)
56+
{
57+
if (e.Data == "volume")
58+
{
59+
SystemVolumeConfigurator();
60+
this.Send("volume:" + GetVolume());
61+
return;
62+
}
63+
if (e.Data.StartsWith("setvolume:"))
64+
{
65+
SetVolume(int.Parse(e.Data.Split(':')[1]));
66+
Console.WriteLine("[MOBILE] Volume: " + GetVolume());
67+
return;
68+
}
69+
desktop.WebSocketServices.Broadcast(e.Data);
70+
}
71+
72+
protected override void OnClose(CloseEventArgs e)
73+
{
74+
Console.WriteLine("[MOBILE] Disconnected");
75+
}
76+
}
77+
78+
public class Desktop : WebSocketBehavior
79+
{
80+
protected override void OnOpen()
81+
{
82+
Console.WriteLine("[DESKTOP] Connected");
83+
}
84+
85+
protected override void OnMessage(MessageEventArgs e)
86+
{
87+
mobile.WebSocketServices.Broadcast(e.Data);
88+
}
89+
90+
protected override void OnClose(CloseEventArgs e)
91+
{
92+
Console.WriteLine("[DESKTOP] Disconnected");
93+
}
94+
}
95+
96+
static WebSocketServer mobile = new WebSocketServer(IPAddress.Parse(GetLocalIPAddress()), 15768);
97+
static WebSocketServer desktop = new WebSocketServer(IPAddress.Parse("127.0.0.1"), 15769);
98+
99+
static void Main(string[] args)
100+
{
101+
Console.Title = "SoundCloud Server";
102+
SystemVolumeConfigurator();
103+
new Thread(() =>
104+
{
105+
HttpListener listener = new HttpListener();
106+
listener.Prefixes.Add("http://localhost:80/");
107+
listener.Prefixes.Add("http://" + GetLocalIPAddress() + ":80/");
108+
listener.Start();
109+
while (true)
110+
{
111+
HttpListenerContext context = listener.GetContext();
112+
HttpListenerResponse response = context.Response;
113+
Console.WriteLine("[MOBILE] Player opened");
114+
byte[] buffer = Properties.Resources.index;
115+
response.ContentLength64 = buffer.Length;
116+
Stream output = response.OutputStream;
117+
output.Write(buffer, 0, buffer.Length);
118+
119+
output.Close();
120+
}
121+
}).Start();
122+
123+
mobile.AddWebSocketService<Mobile>("/soundcloud/mobile");
124+
desktop.AddWebSocketService<Desktop>("/soundcloud/desktop");
125+
126+
mobile.Start();
127+
desktop.Start();
128+
129+
Console.Read();
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)