Skip to content

Commit b506e9e

Browse files
author
Eldin Zenderink
committed
First Commit
1 parent 472d456 commit b506e9e

14 files changed

Lines changed: 3855 additions & 0 deletions

SimpleWebsocketServer.sln

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27428.2005
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleWebsocketServer", "SimpleWebsocketServer\SimpleWebsocketServer.csproj", "{AF7A3511-2D27-4BED-B81F-304C508D016B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebsocketServerTest", "WebsocketServerTest\WebsocketServerTest.csproj", "{D964861E-249F-42CA-802C-7D02A7872DBC}"
9+
ProjectSection(ProjectDependencies) = postProject
10+
{AF7A3511-2D27-4BED-B81F-304C508D016B} = {AF7A3511-2D27-4BED-B81F-304C508D016B}
11+
EndProjectSection
12+
EndProject
13+
Global
14+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15+
Debug|Any CPU = Debug|Any CPU
16+
Release|Any CPU = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{D964861E-249F-42CA-802C-7D02A7872DBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{D964861E-249F-42CA-802C-7D02A7872DBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{D964861E-249F-42CA-802C-7D02A7872DBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{D964861E-249F-42CA-802C-7D02A7872DBC}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
GlobalSection(SolutionProperties) = preSolution
29+
HideSolutionNode = FALSE
30+
EndGlobalSection
31+
GlobalSection(ExtensibilityGlobals) = postSolution
32+
SolutionGuid = {7F093CC9-7107-4046-A70E-0CD2D51FC33A}
33+
EndGlobalSection
34+
EndGlobal

SimpleWebsocketServer/Doxyfile

Lines changed: 2511 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SimpleWebsocketServer
6+
{
7+
class HttpServerSettings
8+
{
9+
}
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace SimpleWebSocketServerLibrary
4+
{
5+
/// <summary>
6+
/// Provides settings and default values for the SimpleWebSocketServer Class.
7+
/// </summary>
8+
public class SimpleWebSocketServerSettings
9+
{
10+
/// <summary>
11+
/// List with url paths where the websocket server needs to listen to. Default = "";
12+
/// </summary>
13+
public List<string> baseUrls { get; set; } = new List<string>(){"/"};
14+
/// <summary>
15+
/// Port where the server needs to listen to. Default = 80;
16+
/// </summary>
17+
public int port { get; set; } = 80;
18+
/// <summary>
19+
/// Buffer size for receiving messages. Default = 4096; (bytes)
20+
/// </summary>
21+
public int bufferSize { get; set; } = 4096;
22+
23+
}
24+
}
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
5+
namespace SimpleWebSocketServerLibrary
6+
{
7+
/// <summary>
8+
/// Middleware class for interfacing with the WebSocketHandler and WebSocketHttpServer class.
9+
/// </summary>
10+
public class SimpleWebSocketServer
11+
{
12+
/// <summary>
13+
/// Interface for the WebSocketHandler class.
14+
/// </summary>
15+
private readonly IWebSocketHandler _WebsocketHandler;
16+
/// <summary>
17+
/// Interface for the WebSocketHttpServer class.
18+
/// </summary>
19+
private readonly IWebSocketHttpServer _WebSocketHttpServer;
20+
21+
22+
/// <summary>
23+
/// Eventhandler for when events such as receiving messages and errors from the websocket server happen.
24+
/// </summary>
25+
public event EventHandler<WebSocketEventArg> WebsocketServerEvent;
26+
27+
/// <summary>
28+
/// Constructor for setting up the Library.
29+
/// </summary>
30+
/// <param name="settings">Settings with default values.</param>
31+
public SimpleWebSocketServer(SimpleWebSocketServerSettings settings)
32+
{
33+
_WebsocketHandler = new WebSocketHandler(settings.bufferSize);
34+
_WebsocketHandler.WebsocketEvent += OnWebsocketServerEvent;
35+
_WebSocketHttpServer = new WebSocketHttpServer(_WebsocketHandler, settings);
36+
}
37+
38+
/// <summary>
39+
/// Overload constructor for setting up library with default values.
40+
/// </summary>
41+
public SimpleWebSocketServer() : this(new SimpleWebSocketServerSettings()){}
42+
43+
/// <summary>
44+
/// Event handler for receiving websocket messages from the websocket handler.
45+
/// </summary>
46+
/// <param name="sender">Instance of firing class.</param>
47+
/// <param name="arg">Arguments for event.</param>
48+
private void OnWebsocketServerEvent(object sender, WebSocketEventArg arg)
49+
{
50+
WebsocketServerEvent?.Invoke(this, arg);
51+
}
52+
53+
/// <summary>
54+
/// Starts running the server async.
55+
/// </summary>
56+
public async Task StartServerAsync()
57+
{
58+
await _WebSocketHttpServer.RunServer();
59+
}
60+
61+
/// <summary>
62+
/// Starts running the server async.
63+
/// </summary>
64+
public void StartServer()
65+
{
66+
Task.Run(async()=> await _WebSocketHttpServer.RunServer());
67+
}
68+
69+
/// <summary>
70+
/// Overload method for sending text messages to a specific client, using it's id, asynchronous.
71+
/// </summary>
72+
/// <param name="messageToSend"></param>
73+
/// <param name="clientId"></param>
74+
/// <returns>True when send succesfully.</returns>
75+
public async Task<bool> SendTextMessageAsync(string messageToSend, string clientId = null)
76+
{
77+
WebSocketMessageContainer message = new WebSocketMessageContainer()
78+
{
79+
data = Encoding.UTF8.GetBytes(messageToSend),
80+
isText = true
81+
};
82+
83+
if (clientId != null)
84+
{
85+
return await _WebsocketHandler.SendMessage(message, clientId);
86+
}
87+
88+
return await _WebsocketHandler.SendMessage(message);
89+
}
90+
91+
/// <summary>
92+
/// Overload method for sending text messages to a specific client, using it's id, synchronous.
93+
/// </summary>
94+
/// <param name="messageToSend"></param>
95+
/// <param name="clientId"></param>
96+
/// <returns>True when send succesfully.</returns>
97+
public bool SendTextMessage(string messageToSend, string clientId = null)
98+
{
99+
WebSocketMessageContainer message = new WebSocketMessageContainer()
100+
{
101+
data = Encoding.UTF8.GetBytes(messageToSend),
102+
isText = true
103+
};
104+
105+
if (clientId != null)
106+
{
107+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message, clientId)).GetAwaiter().GetResult();
108+
}
109+
110+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message)).GetAwaiter().GetResult();
111+
}
112+
113+
114+
/// <summary>
115+
/// Sends a binary "message" to a specific client asynchronous.
116+
/// </summary>
117+
/// <param name="messageToSend">Message in binary format (byte array).</param>
118+
/// <param name="clientId">Client to send to.</param>
119+
/// <returns>True when succesfully send.</returns>
120+
public async Task<bool> SendBinaryMessageAsync(byte[] messageToSend, string clientId = null)
121+
{
122+
WebSocketMessageContainer message = new WebSocketMessageContainer()
123+
{
124+
data = messageToSend,
125+
isBinary= true
126+
};
127+
128+
if (clientId != null)
129+
{
130+
return await _WebsocketHandler.SendMessage(message, clientId);
131+
}
132+
133+
return await _WebsocketHandler.SendMessage(message);
134+
}
135+
136+
/// <summary>
137+
/// Sends a binary "message" to a specific client synchronous.
138+
/// </summary>
139+
/// <param name="messageToSend">Message in binary format (byte array).</param>
140+
/// <param name="clientId">Client to send to.</param>
141+
/// <returns>True when succesfully send.</returns>
142+
public bool SendBinaryMessage(byte[] messageToSend, string clientId = null)
143+
{
144+
WebSocketMessageContainer message = new WebSocketMessageContainer()
145+
{
146+
data = messageToSend,
147+
isBinary = true
148+
};
149+
150+
if (clientId != null)
151+
{
152+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message, clientId)).GetAwaiter().GetResult();
153+
}
154+
155+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message)).GetAwaiter().GetResult();
156+
}
157+
158+
/// <summary>
159+
/// Sends a ping message to all clients asynchronous, can have message.
160+
/// </summary>
161+
/// <param name="messageToSend">Possible extra message to send along ping.</param>
162+
/// <param name="clientId">Set clientId in case you want to send to a specific client.</param>
163+
/// <returns>True when succesfully send.</returns>
164+
public async Task<bool> SendPingMessageAsync(string messageToSend = "", string clientId = null)
165+
{
166+
WebSocketMessageContainer message = new WebSocketMessageContainer()
167+
{
168+
data = Encoding.UTF8.GetBytes(messageToSend),
169+
isPing = true
170+
};
171+
172+
if (clientId != null)
173+
{
174+
return await _WebsocketHandler.SendMessage(message, clientId);
175+
}
176+
return await _WebsocketHandler.SendMessage(message);
177+
}
178+
179+
/// <summary>
180+
/// Sends a ping message to all clients synchronous, can have message.
181+
/// </summary>
182+
/// <param name="messageToSend">Possible extra message to send along ping.</param>
183+
/// <param name="clientId">Set clientId in case you want to send to a specific client.</param>
184+
/// <returns>True when succesfully send.</returns>
185+
public bool SendPingMessage(string messageToSend = "", string clientId = null)
186+
{
187+
WebSocketMessageContainer message = new WebSocketMessageContainer()
188+
{
189+
data = Encoding.UTF8.GetBytes(messageToSend),
190+
isPing = true
191+
};
192+
193+
if (clientId != null)
194+
{
195+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message, clientId)).GetAwaiter().GetResult();
196+
}
197+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message)).GetAwaiter().GetResult();
198+
}
199+
200+
/// <summary>
201+
/// Send pong message to all available clients asynchronous, can have message.
202+
/// </summary>
203+
/// <param name="messageToSend">Possible extra message to send along ping.</param>
204+
/// <param name="clientId">Set clientId in case you want to send to a specific client.</param>
205+
/// <returns>True when succesfully send.</returns>
206+
public async Task<bool> SendPongMessageAsync(string messageToSend = "", string clientId = null)
207+
{
208+
WebSocketMessageContainer message = new WebSocketMessageContainer()
209+
{
210+
data = Encoding.UTF8.GetBytes(messageToSend),
211+
isPong = true
212+
};
213+
214+
if (clientId != null)
215+
{
216+
return await _WebsocketHandler.SendMessage(message, clientId);
217+
}
218+
return await _WebsocketHandler.SendMessage(message);
219+
}
220+
221+
/// <summary>
222+
/// Send pong message to all available clients synchronous, can have message.
223+
/// </summary>
224+
/// <param name="messageToSend">Possible extra message to send along ping.</param>
225+
/// <param name="clientId">Set clientId in case you want to send to a specific client.</param>
226+
/// <returns>True when succesfully send.</returns>
227+
public bool SendPongMessage(string messageToSend = "", string clientId = null)
228+
{
229+
WebSocketMessageContainer message = new WebSocketMessageContainer()
230+
{
231+
data = Encoding.UTF8.GetBytes(messageToSend),
232+
isPong = true
233+
};
234+
235+
if (clientId != null)
236+
{
237+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message, clientId)).GetAwaiter().GetResult();
238+
}
239+
return Task.Run(async () => await _WebsocketHandler.SendMessage(message)).GetAwaiter().GetResult();
240+
}
241+
242+
/// <summary>
243+
/// Stops a connection with a specific client asynchronous. Can provide reason.
244+
/// </summary>
245+
/// <param name="clientId">Specific client to close connection with.</param>
246+
/// <param name="reason">Possible reason to close the connection.</param>
247+
/// <returns>True when succesfully closed.</returns>
248+
public async Task<bool> StopClientAsync(string clientId, string reason = "")
249+
{
250+
if (reason.Length > 0)
251+
{
252+
return await _WebsocketHandler.StopClient(clientId, reason);
253+
}
254+
else
255+
{
256+
return await _WebsocketHandler.StopClient(clientId);
257+
}
258+
}
259+
260+
/// <summary>
261+
/// Stops a connection with a specific client synchronous. Can provide reason.
262+
/// </summary>
263+
/// <param name="clientId">Specific client to close connection with.</param>
264+
/// <param name="reason">Possible reason to close the connection.</param>
265+
/// <returns>True when succesfully closed.</returns>
266+
public bool StopClient(string clientId, string reason = "")
267+
{
268+
if (reason.Length > 0)
269+
{
270+
return Task.Run(async () => await _WebsocketHandler.StopClient(clientId, reason)).GetAwaiter().GetResult();
271+
}
272+
else
273+
{
274+
return Task.Run(async () => await _WebsocketHandler.StopClient(clientId)).GetAwaiter().GetResult();
275+
}
276+
}
277+
278+
279+
/// <summary>
280+
/// Stops all connections with all clients asynchronous. Can provide reason.
281+
/// </summary>
282+
/// <param name="reason">Possible reason to close the connection.</param>
283+
/// <returns>True when succesfully closed.</returns>
284+
public async Task<bool> StopAllAsync(string reason = "")
285+
{
286+
if (reason.Length > 0)
287+
{
288+
return await _WebsocketHandler.StopAll( reason);
289+
}
290+
else
291+
{
292+
return await _WebsocketHandler.StopAll();
293+
}
294+
}
295+
296+
/// <summary>
297+
/// Stops all connections with all clients synchronous. Can provide reason.
298+
/// </summary>
299+
/// <param name="reason">Possible reason to close the connection.</param>
300+
/// <returns>True when succesfully closed.</returns>
301+
public bool StopAll(string reason = "")
302+
{
303+
if (reason.Length > 0)
304+
{
305+
return Task.Run(async()=>await _WebsocketHandler.StopAll(reason)).GetAwaiter().GetResult();
306+
}
307+
else
308+
{
309+
return Task.Run(async () => await _WebsocketHandler.StopAll()).GetAwaiter().GetResult();
310+
}
311+
}
312+
313+
}
314+
}

0 commit comments

Comments
 (0)