diff --git a/Backend/Backend.csproj b/Backend/Backend.csproj index e1005fc5..7afe2779 100644 --- a/Backend/Backend.csproj +++ b/Backend/Backend.csproj @@ -79,6 +79,7 @@ + diff --git a/Backend/BackendLoop.cs b/Backend/BackendLoop.cs index f410fb98..251e8dda 100644 --- a/Backend/BackendLoop.cs +++ b/Backend/BackendLoop.cs @@ -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; } } \ No newline at end of file diff --git a/Backend/SocketServices/BackendServices.cs b/Backend/SocketServices/BackendServices.cs new file mode 100644 index 00000000..3744eba2 --- /dev/null +++ b/Backend/SocketServices/BackendServices.cs @@ -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; + } +} +} diff --git a/FrontSocket/FrontendLoop.cs b/FrontSocket/FrontendLoop.cs index c794713a..753887cf 100644 --- a/FrontSocket/FrontendLoop.cs +++ b/FrontSocket/FrontendLoop.cs @@ -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 ("/Master"); // _socketServer.KeepClean = false; // as per the author's recommendation - this may be bad in the long run _socketServer.Start();