From 060f01cf75defc1b3c21e349c1131f341a65adb2 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 21 Jan 2017 18:38:52 +0000
Subject: [PATCH] Set up ripple link websocket connection.

---
 Nibriboard/Client/ClientSettings.cs          |  2 ++
 Nibriboard/Client/HttpEmbeddedFileHandler.cs |  9 +++++++--
 Nibriboard/Client/NibriClientManager.cs      |  9 ++++++---
 Nibriboard/ClientFiles/BoardWindow.js        |  5 +++++
 Nibriboard/ClientFiles/NibriClient.js        | 17 +++++++++++++++++
 Nibriboard/ClientFiles/RippleLink.js         |  6 +++++-
 Nibriboard/NibriboardServer.cs               |  2 +-
 7 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/Nibriboard/Client/ClientSettings.cs b/Nibriboard/Client/ClientSettings.cs
index b98f10e..0a91469 100644
--- a/Nibriboard/Client/ClientSettings.cs
+++ b/Nibriboard/Client/ClientSettings.cs
@@ -20,6 +20,8 @@ namespace Nibriboard.Client
 			}
 		}
 
+		public string WebsocketProtocol = "RippleLink";
+
 		public ClientSettings()
 		{
 
diff --git a/Nibriboard/Client/HttpEmbeddedFileHandler.cs b/Nibriboard/Client/HttpEmbeddedFileHandler.cs
index bb15e64..172bcf9 100644
--- a/Nibriboard/Client/HttpEmbeddedFileHandler.cs
+++ b/Nibriboard/Client/HttpEmbeddedFileHandler.cs
@@ -36,10 +36,11 @@ namespace Nibriboard.Client
 				return;
 			}
 
-			response.ContentType = getMimeType(request.URI);
-			response.Headers.Add("content-type", response.ContentType);
 
 			string expandedFilePath = getEmbeddedFileReference(request.URI);
+			if (!embeddedFiles.Contains(expandedFilePath)) {
+				expandedFilePath += "index.html";
+			}
 			if (!embeddedFiles.Contains(expandedFilePath)) {
 				response.ResponseCode = HttpResponseCode.NotFound;
 				response.ContentType = "text/plain";
@@ -47,6 +48,10 @@ namespace Nibriboard.Client
 				logRequest(request, response);
 				return;
 			}
+
+			response.ContentType = getMimeType(expandedFilePath);
+			response.Headers.Add("content-type", response.ContentType);
+
 			byte[] embeddedFile = EmbeddedFiles.ReadAllBytes(expandedFilePath);
 			response.ContentLength = embeddedFile.Length;
 			response.Content.Write(embeddedFile, 0, embeddedFile.Length);
diff --git a/Nibriboard/Client/NibriClientManager.cs b/Nibriboard/Client/NibriClientManager.cs
index 7884164..636ca2b 100644
--- a/Nibriboard/Client/NibriClientManager.cs
+++ b/Nibriboard/Client/NibriClientManager.cs
@@ -8,10 +8,13 @@ namespace Nibriboard.Client
 {
 	public class NibriClientManager : IWebSocketRequestHandler
 	{
+		ClientSettings clientSettings;
+
 		List<NibriClient> clients = new List<NibriClient>();
 
-		public NibriClientManager()
+		public NibriClientManager(ClientSettings inClientSettings)
 		{
+			clientSettings = inClientSettings;
 		}
 
 		/// <summary>
@@ -22,8 +25,8 @@ namespace Nibriboard.Client
 		/// <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;
+			Log.WriteLine("[Nibriboard/Websocket] Accepting new {0} connection.", protocol);
+			return clientSettings.WebsocketProtocol == protocol;
 		}
 		/// <summary>
 		/// Handles WebSocket clients when they first connect, wrapping them in
diff --git a/Nibriboard/ClientFiles/BoardWindow.js b/Nibriboard/ClientFiles/BoardWindow.js
index 380fe8b..24bb81b 100644
--- a/Nibriboard/ClientFiles/BoardWindow.js
+++ b/Nibriboard/ClientFiles/BoardWindow.js
@@ -22,6 +22,7 @@ class BoardWindow extends EventEmitter
 		FaviconNotification.add();
 		
 		get("/Settings.json").then(JSON.parse).then((function(settings) {
+			console.info("[setup]", "Obtained settings from server:", settings);
 			this.settings = settings;
 			this.setup();
 		}).bind(this), function(errorMessage) {
@@ -31,6 +32,10 @@ class BoardWindow extends EventEmitter
 		this.trackWindowSize();
 	}
 	
+	setup() {
+		this.rippleLink = new RippleLink(this.settings.WebsocketUri, this);
+	}
+	
 	nextFrame()
 	{
 		this.update();
diff --git a/Nibriboard/ClientFiles/NibriClient.js b/Nibriboard/ClientFiles/NibriClient.js
index 9400db2..d9d9b5c 100644
--- a/Nibriboard/ClientFiles/NibriClient.js
+++ b/Nibriboard/ClientFiles/NibriClient.js
@@ -1,6 +1,18 @@
 (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
 'use strict';
 
+class RippleLink
+{
+	constructor(inSocketUrl, inBoardWindow)
+	{
+		this.socketUrl = inSocketUrl;
+		this.boardWindow = inBoardWindow;
+		this.settings = this.boardWindow.settings;
+		
+		this.websocket = new WebSocket( this.socketUrl, [ this.settings.WebsocketProtocol ] );
+	}
+}
+
 function get(u){return new Promise(function(r,t,a){a=new XMLHttpRequest();a.onload=function(b,c){b=a.status;c=a.response;if(b>199&&b<300){r(c);}else{t(c);}};a.open("GET",u,true);a.send(null);})}
 
 // npm modules
@@ -22,6 +34,7 @@ class BoardWindow extends EventEmitter
 		FaviconNotification.add();
 		
 		get("/Settings.json").then(JSON.parse).then((function(settings) {
+			console.info("[setup]", "Obtained settings from server:", settings);
 			this.settings = settings;
 			this.setup();
 		}).bind(this), function(errorMessage) {
@@ -31,6 +44,10 @@ class BoardWindow extends EventEmitter
 		this.trackWindowSize();
 	}
 	
+	setup() {
+		this.rippleLink = new RippleLink(this.settings.WebsocketUri, this);
+	}
+	
 	nextFrame()
 	{
 		this.update();
diff --git a/Nibriboard/ClientFiles/RippleLink.js b/Nibriboard/ClientFiles/RippleLink.js
index 3b2a3ab..fe34c7c 100644
--- a/Nibriboard/ClientFiles/RippleLink.js
+++ b/Nibriboard/ClientFiles/RippleLink.js
@@ -3,9 +3,13 @@
 
 class RippleLink
 {
-	constructor(socketUrl)
+	constructor(inSocketUrl, inBoardWindow)
 	{
+		this.socketUrl = inSocketUrl;
+		this.boardWindow = inBoardWindow;
+		this.settings = this.boardWindow.settings;
 		
+		this.websocket = new WebSocket( this.socketUrl, [ this.settings.WebsocketProtocol ] );
 	}
 }
 
diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index b82d0c2..9dfb83a 100644
--- a/Nibriboard/NibriboardServer.cs
+++ b/Nibriboard/NibriboardServer.cs
@@ -47,7 +47,7 @@ namespace Nibriboard
 			// Websocket setup
 			httpServer.AddWebSocketRequestHandler(
 				clientSettings.WebsocketPath,
-				new NibriClientManager()
+				new NibriClientManager(clientSettings)
 			);
 		}
 
