mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
feat: Adds WebSocketBehaviour
This commit is contained in:
@@ -8,27 +8,33 @@ using Serilog.Core;
|
|||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Serilog.Configuration;
|
using Serilog.Configuration;
|
||||||
using WebSocketSharp.Server;
|
using WebSocketSharp.Server;
|
||||||
|
using Crestron.SimplSharp;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using WebSocketSharp;
|
||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
public class DebugWebsocketSink : ILogEventSink
|
public class DebugWebsocketSink : ILogEventSink
|
||||||
{
|
{
|
||||||
private WebSocketServer _wssv;
|
private HttpServer _server;
|
||||||
|
|
||||||
|
private string _path = "/join";
|
||||||
|
|
||||||
public int Port
|
public int Port
|
||||||
{ get
|
{ get
|
||||||
{
|
{
|
||||||
|
|
||||||
if(_wssv == null) return 0;
|
if(_server == null) return 0;
|
||||||
return _wssv.Port;
|
return _server.Port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsListening
|
public bool IsListening
|
||||||
{ get
|
{ get
|
||||||
{
|
{
|
||||||
if (_wssv == null) return false;
|
if (_server == null) return false;
|
||||||
return _wssv.IsListening;
|
return _server.IsListening;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,29 +42,35 @@ namespace PepperDash.Core
|
|||||||
|
|
||||||
public DebugWebsocketSink()
|
public DebugWebsocketSink()
|
||||||
{
|
{
|
||||||
_wssv = new WebSocketServer();
|
CrestronEnvironment.ProgramStatusEventHandler += type =>
|
||||||
|
{
|
||||||
|
if (type == eProgramStatusEventType.Stopping)
|
||||||
|
{
|
||||||
|
StopServer();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Emit(LogEvent logEvent)
|
public void Emit(LogEvent logEvent)
|
||||||
{
|
{
|
||||||
if (_wssv == null || !_wssv.IsListening) return;
|
if (_server == null || !_server.IsListening) return;
|
||||||
|
|
||||||
var message = logEvent.RenderMessage(_formatProvider);
|
var message = logEvent.RenderMessage(_formatProvider);
|
||||||
_wssv.WebSocketServices.Broadcast(message);
|
_server.WebSocketServices.Broadcast(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartServerAndSetPort(int port)
|
public void StartServerAndSetPort(int port)
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Starting Websocket Server on port: {0}", port);
|
Debug.Console(0, "Starting Websocket Server on port: {0}", port);
|
||||||
_wssv = new WebSocketServer(port);
|
_server = new HttpServer(port);
|
||||||
_wssv.Start();
|
_server.AddWebSocketService<DebugClient>(_path);
|
||||||
|
_server.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopServer()
|
public void StopServer()
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Stopping Websocket Server");
|
Debug.Console(0, "Stopping Websocket Server");
|
||||||
_wssv.Stop();
|
_server.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,4 +83,93 @@ namespace PepperDash.Core
|
|||||||
return loggerConfiguration.Sink(new DebugWebsocketSink());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
<RepositoryUrl>https://github.com/PepperDash/PepperDashCore</RepositoryUrl>
|
<RepositoryUrl>https://github.com/PepperDash/PepperDashCore</RepositoryUrl>
|
||||||
<PackageTags>crestron;4series;</PackageTags>
|
<PackageTags>crestron;4series;</PackageTags>
|
||||||
<Version>$(Version)</Version>
|
<Version>$(Version)</Version>
|
||||||
|
<InformationalVersion>$(Version)</InformationalVersion>
|
||||||
<PackageOutputPath>../../package</PackageOutputPath>
|
<PackageOutputPath>../../package</PackageOutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
|||||||
Reference in New Issue
Block a user