From cf8fb234b4d6b482967323f50199f0bf6face9dc Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 10 Jan 2017 20:53:50 +0000
Subject: [PATCH] Create scaffolding for WebSocket connection handling logic

---
 Nibriboard/NibriClient.cs           | 36 ++++++++++++++++++++++++++
 Nibriboard/NibriClientManager.cs    | 39 ++++++++++++++++++++++++++++
 Nibriboard/NibriWebSocketHandler.cs | 40 -----------------------------
 Nibriboard/Nibriboard.csproj        |  3 ++-
 Nibriboard/NibriboardServer.cs      |  2 +-
 5 files changed, 78 insertions(+), 42 deletions(-)
 create mode 100644 Nibriboard/NibriClient.cs
 create mode 100644 Nibriboard/NibriClientManager.cs
 delete mode 100644 Nibriboard/NibriWebSocketHandler.cs

diff --git a/Nibriboard/NibriClient.cs b/Nibriboard/NibriClient.cs
new file mode 100644
index 0000000..b5753ab
--- /dev/null
+++ b/Nibriboard/NibriClient.cs
@@ -0,0 +1,36 @@
+﻿using System;
+using IotWeb.Common.Http;
+using System.Threading.Tasks;
+namespace Nibriboard
+{
+	public class NibriClient
+	{
+		private readonly NibriClientManager manager;
+		private readonly WebSocket client;
+
+		public NibriClient(NibriClientManager inManager, WebSocket inClient)
+		{
+			manager = inManager;
+			client = inClient;
+
+			client.DataReceived += async (WebSocket clientSocket, string frame) => {
+				try
+				{
+					await onMessage(frame);
+				}
+				catch (Exception error)
+				{
+					await Console.Error.WriteLineAsync(error.ToString());
+					throw;
+				}
+
+				//Task.Run(async () => await onMessage(frame)).Wait();
+			};
+		}
+
+		private async Task onMessage(string frame)
+		{
+
+		}
+	}
+}
diff --git a/Nibriboard/NibriClientManager.cs b/Nibriboard/NibriClientManager.cs
new file mode 100644
index 0000000..da47b04
--- /dev/null
+++ b/Nibriboard/NibriClientManager.cs
@@ -0,0 +1,39 @@
+﻿using System;
+using IotWeb.Common.Http;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+using System.Diagnostics;
+namespace Nibriboard
+{
+	public class NibriClientManager : IWebSocketRequestHandler
+	{
+		List<NibriClient> clients = new List<NibriClient>();
+
+		public NibriClientManager()
+		{
+		}
+
+		/// <summary>
+		/// Whether we will accept a given new WebSocket connection or not.
+		/// </summary>
+		/// <param name="uri">The uri the user connected to.</param>
+		/// <param name="protocol">The protocol the user is connecting with.</param>
+		/// <returns>Whether we want to accept the WebSocket connection attempt or not.</returns>
+		public bool WillAcceptRequest(string uri, string protocol)
+		{
+			Log.WriteLine("[Nibriboard/Websocket] Accepting {0} via {1}.", uri, protocol);
+			return true;
+		}
+		/// <summary>
+		/// Handles WebSocket clients when they first connect, wrapping them in
+		/// a <see cref="Nibriboard.NibriClient" /> instance and adding them to
+		/// the client list.
+		/// </summary>
+		/// <param name="newSocket">New socket.</param>
+		public void Connected(WebSocket newSocket)
+		{
+			NibriClient client = new NibriClient(this, newSocket);
+			clients.Add(client);
+		}
+	}
+}
diff --git a/Nibriboard/NibriWebSocketHandler.cs b/Nibriboard/NibriWebSocketHandler.cs
deleted file mode 100644
index c730af5..0000000
--- a/Nibriboard/NibriWebSocketHandler.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-﻿using System;
-using IotWeb.Common.Http;
-using System.Threading.Tasks;
-namespace Nibriboard
-{
-	public class NibriWebSocketHandler : IWebSocketRequestHandler
-	{
-		public NibriWebSocketHandler()
-		{
-		}
-
-		public void Connected(WebSocket client)
-		{
-			
-			/*client.DataReceived += async (WebSocket clientSocket, string frame) => {
-				try {
-					await onMessage(frame);
-				}
-				catch(Exception error) {
-					await Console.Error.WriteLineAsync(error);
-					throw;
-				}
-
-				//Task.Run(async () => await onMessage(frame)).Wait();
-			};*/
-		}
-
-		/// <summary>
-		/// Whether we will accept the WebSocket connection or not.
-		/// </summary>
-		/// <param name="uri">The uri the user connected to.</param>
-		/// <param name="protocol">The protocol the user is connecting with.</param>
-		/// <returns>Whether we want to accept the WebSocket connection attempt or not.</returns>
-		public bool WillAcceptRequest(string uri, string protocol)
-		{
-			Log.WriteLine("[Nibriboard/Websocket] Accepting {0} via {1}.", uri, protocol);
-			return true;
-		}
-	}
-}
diff --git a/Nibriboard/Nibriboard.csproj b/Nibriboard/Nibriboard.csproj
index d20e6d9..f58bbaa 100644
--- a/Nibriboard/Nibriboard.csproj
+++ b/Nibriboard/Nibriboard.csproj
@@ -57,9 +57,10 @@
     <Compile Include="RippleSpace\Reference.cs" />
     <Compile Include="Utilities.cs" />
     <Compile Include="NibriboardServer.cs" />
-    <Compile Include="NibriWebSocketHandler.cs" />
+    <Compile Include="NibriClientManager.cs" />
     <Compile Include="Log.cs" />
     <Compile Include="Utilities\EmbeddedFiles.cs" />
+    <Compile Include="NibriClient.cs" />
   </ItemGroup>
   <ItemGroup>
   	<EmbeddedResource Include="Client\index.html" />
diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index 65688de..4f5ef49 100644
--- a/Nibriboard/NibriboardServer.cs
+++ b/Nibriboard/NibriboardServer.cs
@@ -28,7 +28,7 @@ namespace Nibriboard
 			);
 			httpServer.AddWebSocketRequestHandler(
 				"/RipplespaceConnection",
-				new NibriWebSocketHandler()
+				new NibriClientManager()
 			);
 			httpServer.Start();
 			Log.WriteLine("[NibriboardServer] Started on port {0}", Port);
