Testing frontend sockets

This commit is contained in:
Rick Falkvinge
2017-01-12 20:27:06 +01:00
parent d739a3d583
commit f28646c0d6
6 changed files with 62 additions and 79 deletions
+1 -1
View File
@@ -56,7 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="SocketServices.cs" />
<Compile Include="MasterServices.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
+1 -1
View File
@@ -32,7 +32,7 @@ namespace Swarmops.Frontend.Socket
Console.WriteLine(" * Swarmops Frontend Socket Server starting up.");
_socketServer = new WebSocketServer (12172); // TODO: Read from database
_socketServer.AddWebSocketService<SocketServices> ("/ws");
_socketServer.AddWebSocketService<MasterServices> ("/Master");
// _socketServer.KeepClean = false; // as per the author's recommendation - this may be bad in the long run
_socketServer.Start();
-74
View File
@@ -1,74 +0,0 @@
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;
}
}
+4 -1
View File
@@ -142,10 +142,13 @@
</script>
<!-- initialize tooltips -->
<!-- initialize socket, tabs, and search -->
<script language="javascript" type="text/javascript">
$(document).ready(function () {
_masterInitializeSocket('<%=CommonV5.JavascriptEscape(this.CurrentAuthority.ToEncryptedXml())%>');
$('#divTabsEditPerson').tabs();
$('#<%=this.SearchBox.ClientID%>').focus(function() {
+55 -1
View File
@@ -1,4 +1,58 @@
var SwarmopsJS = (function() {
// ------------------- _masterSocket functions -----------------------
var _masterSocket;
var _masterSocketHeartbeatsLost;
function _masterInitializeSocket(authenticationTicket) {
if (_masterSocket != null) {
_masterSocket.close();
_masterSocket = null;
}
alertify.log("Opening socket " + getMasterSocketAddress() + "...");
var socketUrl = getMasterSocketAddress() + "?Auth=" + authenticationTicket;
_masterSocket = new WebSocket(socketUrl);
_masterSocket.onopen = function(data) {
alertify.success('Socket Connected!');
if (_masterSocketHeartbeatsLost) {
// Reload all edits and the edit pool. If there are discrepancies, we can probably live with them. (Detect editing?)
alertify.log("Backend connection restored without interruption.");
_masterSocketHeartbeatsLost = false;
}
//watchHeartbeat();
};
_masterSocket.onclose = function(data) { alertify.log("Connection Lost"); };
_masterSocket.onerror = function(data) { alertify.log("Connection Error"); };
_masterSocket.onmessage = function(data) {
console.log(data.data);
var message = $.parseJSON(data.data);
if (message.messageType == "Heartbeat") {
alertify.log("Master socket heartbeat");
}
};
}
function getMasterSocketAddress() {
if (location.host.contains ("localhost")) { // Assume dev environment, go for sandbox socket
return 'ws://sandbox.swarmops.com/ws/Master';
} else {
var protocol = ('https:' == document.location.protocol ? 'wss://' : 'ws://');
return protocol + location.host + "/ws/Master";
}
}
// ------------------- SwarmopsJS object ----------------------
var SwarmopsJS = (function () {
var publicSymbols = {},
initFunctions = [];
+1 -1
View File
@@ -4,7 +4,7 @@ DocumentRoot /usr/share/swarmops/frontend/
# Redirect websocket requests to Swarmops FrontSocket server
ProxyRequests Off
ProxyPass "/ws/" "ws:/localhost:12172"
ProxyPass "/ws/" "ws://localhost:12172/"
# Explicitly specify Mono version 4
MonoServerPath swarmops-frontend "/usr/bin/mod-mono-server4"