Added socket to backend

This commit is contained in:
Rick Falkvinge
2017-02-18 01:35:11 +01:00
parent 41047098f1
commit edc98b054f
4 changed files with 85 additions and 1 deletions
+1
View File
@@ -79,6 +79,7 @@
<Compile Include="CommsTransmitter.cs" />
<Compile Include="BackendLoop.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SocketServices\BackendServices.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Basic\Basic.csproj">
+12
View File
@@ -17,6 +17,7 @@ using Swarmops.Utility;
using Swarmops.Utility.BotCode;
using Swarmops.Utility.Communications;
using Swarmops.Utility.Mail;
using WebSocketSharp.Server;
namespace Swarmops.Backend
{
@@ -194,6 +195,13 @@ namespace Swarmops.Backend
BitcoinUtility.VerifyBitcoinHotWallet();
// Initialize backend socket server
int backendSocketPort = SystemSettings.WebsocketPortBackend;
_socketServer = new WebSocketServer(backendSocketPort);
// TODO: ADD SERVICE
_socketServer.Start();
// Begin maintenance loop
DateTime cycleStartTime = DateTime.UtcNow;
@@ -321,6 +329,8 @@ namespace Swarmops.Backend
System.Threading.Thread.Sleep(50);
}*/
_socketServer.Stop();
Thread.Sleep (2000);
}
@@ -807,5 +817,7 @@ namespace Swarmops.Backend
Console.Write (message);
}
}
private static WebSocketServer _socketServer;
}
}
+71
View File
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Swarmops.Logic.Security;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace Swarmops.Backend.SocketServices
{
internal class BackendServices: WebSocketBehavior
{
public BackendServices()
{
base.IgnoreExtensions = true; // Needed to suppress a Deflate which doesn't work
}
protected override void OnMessage(WebSocketSharp.MessageEventArgs e)
{
// Basically just echo whatever's sent here
JObject json = JObject.Parse(e.Data);
string serverRequest = (string)json["serverRequest"];
if (string.IsNullOrEmpty(serverRequest))
{
// Not a server request, so just echo the contents to everybody
Sessions.Broadcast(e.Data);
}
switch (serverRequest)
{
case "UpdateQueueCounts":
//Sessions.Broadcast(Program.GetQueueInfoJson());
break;
default:
// do nothing;
break;
}
}
protected override void OnOpen()
{
string authBase64 = Context.QueryString["Auth"];
authBase64 = Uri.UnescapeDataString(authBase64); // Defensive programming - % sign does not exist in base64 so this won't ever collapse a working encoding
_authority = Authority.FromEncryptedXml(authBase64);
base.OnOpen();
if (Context.QueryString["Notify"] != "false")
{
Sessions.Broadcast("{\"messageType\":\"EditorCount\"," +
String.Format("\"editorCount\":\"{0}\"", Sessions.Count) + '}');
//Send(Program.GetQueueInfoJson());
}
}
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.Count) + '}');
}
private Authority _authority = null;
}
}
}
+1 -1
View File
@@ -55,7 +55,7 @@ namespace Swarmops.Frontend.Socket
OutboundComm.CreateNotification(null, NotificationResource.System_Startup_Frontend);
_socketServer = new WebSocketServer (12172); // TODO: Read from database
_socketServer = new WebSocketServer (SystemSettings.WebsocketPortFrontend);
_socketServer.AddWebSocketService<MasterServices> ("/Master");
// _socketServer.KeepClean = false; // as per the author's recommendation - this may be bad in the long run
_socketServer.Start();