1
0
Fork 0

Create scaffolding for WebSocket connection handling logic

This commit is contained in:
Starbeamrainbowlabs 2017-01-10 20:53:50 +00:00
parent 7faac0f6a8
commit cf8fb234b4
5 changed files with 78 additions and 42 deletions

36
Nibriboard/NibriClient.cs Normal file
View File

@ -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)
{
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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" />

View File

@ -28,7 +28,7 @@ namespace Nibriboard
);
httpServer.AddWebSocketRequestHandler(
"/RipplespaceConnection",
new NibriWebSocketHandler()
new NibriClientManager()
);
httpServer.Start();
Log.WriteLine("[NibriboardServer] Started on port {0}", Port);