feat: Adds WebSocketBehaviour

This commit is contained in:
Neil Dorin
2023-11-07 12:08:50 -07:00
parent 7517666614
commit a82e53ce2b
2 changed files with 114 additions and 12 deletions

View File

@@ -8,27 +8,33 @@ using Serilog.Core;
using Serilog.Events;
using Serilog.Configuration;
using WebSocketSharp.Server;
using Crestron.SimplSharp;
using System.Net.Http;
using System.Text.RegularExpressions;
using WebSocketSharp;
namespace PepperDash.Core
{
public class DebugWebsocketSink : ILogEventSink
{
private WebSocketServer _wssv;
private HttpServer _server;
private string _path = "/join";
public int Port
{ get
{
if(_wssv == null) return 0;
return _wssv.Port;
if(_server == null) return 0;
return _server.Port;
}
}
public bool IsListening
{ get
{
if (_wssv == null) return false;
return _wssv.IsListening;
if (_server == null) return false;
return _server.IsListening;
}
}
@@ -36,29 +42,35 @@ namespace PepperDash.Core
public DebugWebsocketSink()
{
_wssv = new WebSocketServer();
CrestronEnvironment.ProgramStatusEventHandler += type =>
{
if (type == eProgramStatusEventType.Stopping)
{
StopServer();
}
};
}
public void Emit(LogEvent logEvent)
{
if (_wssv == null || !_wssv.IsListening) return;
if (_server == null || !_server.IsListening) return;
var message = logEvent.RenderMessage(_formatProvider);
_wssv.WebSocketServices.Broadcast(message);
_server.WebSocketServices.Broadcast(message);
}
public void StartServerAndSetPort(int port)
{
Debug.Console(0, "Starting Websocket Server on port: {0}", port);
_wssv = new WebSocketServer(port);
_wssv.Start();
_server = new HttpServer(port);
_server.AddWebSocketService<DebugClient>(_path);
_server.Start();
}
public void StopServer()
{
Debug.Console(0, "Stopping Websocket Server");
_wssv.Stop();
_server.Stop();
}
}
@@ -71,4 +83,93 @@ namespace PepperDash.Core
return loggerConfiguration.Sink(new DebugWebsocketSink());
}
}
public class DebugClient : WebSocketBehavior
{
private DateTime _connectionTime;
public TimeSpan ConnectedDuration
{
get
{
if (Context.WebSocket.IsAlive)
{
return DateTime.Now - _connectionTime;
}
else
{
return new TimeSpan(0);
}
}
}
public DebugClient()
{
}
protected override void OnOpen()
{
base.OnOpen();
var url = Context.WebSocket.Url;
Debug.Console(2, Debug.ErrorLogLevel.Notice, "New WebSocket Connection from: {0}", url);
//var match = Regex.Match(url.AbsoluteUri, "(?:ws|wss):\\/\\/.*(?:\\/mc\\/api\\/ui\\/join\\/)(.*)");
//if (match.Success)
//{
// var clientId = match.Groups[1].Value;
// // Inform controller of client joining
// if (Controller != null)
// {
// var clientJoined = new MobileControlResponseMessage
// {
// Type = "/system/roomKey",
// ClientId = clientId,
// Content = RoomKey,
// };
// Controller.SendMessageObjectToDirectClient(clientJoined);
// var bridge = Controller.GetRoomBridge(RoomKey);
// SendUserCodeToClient(bridge, clientId);
// bridge.UserCodeChanged += (sender, args) => SendUserCodeToClient((MobileControlEssentialsRoomBridge)sender, clientId);
// }
// else
// {
// Debug.Console(2, "WebSocket UiClient Controller is null");
// }
//}
_connectionTime = DateTime.Now;
// TODO: Future: Check token to see if there's already an open session using that token and reject/close the session
}
protected override void OnMessage(MessageEventArgs e)
{
base.OnMessage(e);
Debug.Console(0, "WebSocket UiClient Message: {0}", e.Data);
}
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
Debug.Console(2, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Closing: {0} reason: {1}", e.Code, e.Reason);
}
protected override void OnError(ErrorEventArgs e)
{
base.OnError(e);
Debug.Console(2, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Error: {0} message: {1}", e.Exception, e.Message);
}
}
}

View File

@@ -14,6 +14,7 @@
<RepositoryUrl>https://github.com/PepperDash/PepperDashCore</RepositoryUrl>
<PackageTags>crestron;4series;</PackageTags>
<Version>$(Version)</Version>
<InformationalVersion>$(Version)</InformationalVersion>
<PackageOutputPath>../../package</PackageOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">