Polished new code enough to build

This commit is contained in:
Rick Falkvinge
2017-01-11 15:37:25 +01:00
parent 490958b22c
commit 48c6b27483
2 changed files with 80 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Swarmops.Frontend.Socket;
using Swarmops.Logic.Security;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace Swarmops.Frontend.Socket
{
internal class SocketServices: WebSocketBehavior
{
public SocketServices()
{
base.IgnoreExtensions = true; // Necessary to suppress a Deflate exception that kills server otherwise
}
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;
case "Ping":
Sessions.Broadcast("{\"messageType\":\"Heartbeat\"}");
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.ActiveIDs.ToArray().Length) + '}');
Send(Program.GetQueueInfoJson());
}
}
protected override void OnClose (CloseEventArgs e)
{
base.OnClose (e);
Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.ActiveIDs.ToArray().Length) + '}');
}
private Authority _authority = null;
}
}
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Posix-4.5" version="4.5.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="WebSocketSharp" version="1.0.3-rc11" targetFramework="net452" />
</packages>