mirror of
https://github.com/sbrl/Nibriboard.git
synced 2018-01-10 21:33:49 +00:00
Create scaffolding for WebSocket connection handling logic
This commit is contained in:
parent
7faac0f6a8
commit
cf8fb234b4
5 changed files with 78 additions and 42 deletions
36
Nibriboard/NibriClient.cs
Normal file
36
Nibriboard/NibriClient.cs
Normal 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
Nibriboard/NibriClientManager.cs
Normal file
39
Nibriboard/NibriClientManager.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -57,9 +57,10 @@
|
||||||
<Compile Include="RippleSpace\Reference.cs" />
|
<Compile Include="RippleSpace\Reference.cs" />
|
||||||
<Compile Include="Utilities.cs" />
|
<Compile Include="Utilities.cs" />
|
||||||
<Compile Include="NibriboardServer.cs" />
|
<Compile Include="NibriboardServer.cs" />
|
||||||
<Compile Include="NibriWebSocketHandler.cs" />
|
<Compile Include="NibriClientManager.cs" />
|
||||||
<Compile Include="Log.cs" />
|
<Compile Include="Log.cs" />
|
||||||
<Compile Include="Utilities\EmbeddedFiles.cs" />
|
<Compile Include="Utilities\EmbeddedFiles.cs" />
|
||||||
|
<Compile Include="NibriClient.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="Client\index.html" />
|
<EmbeddedResource Include="Client\index.html" />
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace Nibriboard
|
||||||
);
|
);
|
||||||
httpServer.AddWebSocketRequestHandler(
|
httpServer.AddWebSocketRequestHandler(
|
||||||
"/RipplespaceConnection",
|
"/RipplespaceConnection",
|
||||||
new NibriWebSocketHandler()
|
new NibriClientManager()
|
||||||
);
|
);
|
||||||
httpServer.Start();
|
httpServer.Start();
|
||||||
Log.WriteLine("[NibriboardServer] Started on port {0}", Port);
|
Log.WriteLine("[NibriboardServer] Started on port {0}", Port);
|
||||||
|
|
Loading…
Reference in a new issue