mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-11 19:44:52 +00:00
feat: implement WebSocket classes and update culture settings; bump PepperDashCore version
This commit is contained in:
committed by
Andrew Welker
parent
0c59237232
commit
688cf34153
7
runtimeconfig.json
Normal file
7
runtimeconfig.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"runtimeOptions": {
|
||||||
|
"configProperties": {
|
||||||
|
"System.Globalization.Invariant": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
@@ -280,6 +278,18 @@ namespace PepperDash.Essentials.Core
|
|||||||
CrestronConsole.PrintLine("Error starting CrestronDataStoreStatic: {0}", err);
|
CrestronConsole.PrintLine("Error starting CrestronDataStoreStatic: {0}", err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en");
|
||||||
|
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en");
|
||||||
|
}
|
||||||
|
catch (CultureNotFoundException)
|
||||||
|
{
|
||||||
|
// If specific culture fails, fall back to invariant
|
||||||
|
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
||||||
|
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
37
src/PepperDash.Essentials.Core/SomeWebSocketClass.cs
Normal file
37
src/PepperDash.Essentials.Core/SomeWebSocketClass.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class SomeWebSocketClass
|
||||||
|
{
|
||||||
|
private ClientWebSocket _webSocket;
|
||||||
|
|
||||||
|
public SomeWebSocketClass()
|
||||||
|
{
|
||||||
|
_webSocket = new ClientWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ConnectAsync(Uri uri)
|
||||||
|
{
|
||||||
|
await _webSocket.ConnectAsync(uri, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAsync(string message)
|
||||||
|
{
|
||||||
|
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
|
||||||
|
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ReceiveAsync()
|
||||||
|
{
|
||||||
|
var buffer = new byte[1024];
|
||||||
|
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||||
|
return System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CloseAsync()
|
||||||
|
{
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,11 +10,14 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using PepperDash.Essentials.Core.Web.RequestHandlers;
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
||||||
{
|
{
|
||||||
public class DebugSessionRequestHandler : WebApiBaseRequestHandler
|
public class DebugSessionRequestHandler : WebApiBaseRequestHandler
|
||||||
{
|
{
|
||||||
|
private readonly DebugWebsocketSink _sink = new DebugWebsocketSink();
|
||||||
|
|
||||||
public DebugSessionRequestHandler()
|
public DebugSessionRequestHandler()
|
||||||
: base(true)
|
: base(true)
|
||||||
{
|
{
|
||||||
@@ -43,21 +46,21 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||||||
|
|
||||||
var port = 0;
|
var port = 0;
|
||||||
|
|
||||||
if (!Debug.WebsocketSink.IsRunning)
|
if (!_sink.IsRunning)
|
||||||
{
|
{
|
||||||
Debug.LogMessage(LogEventLevel.Information, "Starting WS Server");
|
Debug.LogMessage(LogEventLevel.Information, "Starting WS Server");
|
||||||
// Generate a random port within a specified range
|
// Generate a random port within a specified range
|
||||||
port = new Random().Next(65435, 65535);
|
port = new Random().Next(65435, 65535);
|
||||||
// Start the WS Server
|
// Start the WS Server
|
||||||
Debug.WebsocketSink.StartServerAndSetPort(port);
|
_sink.StartServerAndSetPort(port);
|
||||||
Debug.SetWebSocketMinimumDebugLevel(Serilog.Events.LogEventLevel.Verbose);
|
Debug.SetWebSocketMinimumDebugLevel(Serilog.Events.LogEventLevel.Verbose);
|
||||||
}
|
}
|
||||||
|
|
||||||
var url = Debug.WebsocketSink.Url;
|
var url = _sink.Url;
|
||||||
|
|
||||||
object data = new
|
object data = new
|
||||||
{
|
{
|
||||||
url = Debug.WebsocketSink.Url
|
url = _sink.Url
|
||||||
};
|
};
|
||||||
|
|
||||||
Debug.LogMessage(LogEventLevel.Information, "Debug Session URL: {0}", url);
|
Debug.LogMessage(LogEventLevel.Information, "Debug Session URL: {0}", url);
|
||||||
@@ -84,7 +87,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
protected override void HandlePost(HttpCwsContext context)
|
protected override void HandlePost(HttpCwsContext context)
|
||||||
{
|
{
|
||||||
Debug.WebsocketSink.StopServer();
|
_sink.StopServer();
|
||||||
|
|
||||||
context.Response.StatusCode = 200;
|
context.Response.StatusCode = 200;
|
||||||
context.Response.StatusDescription = "OK";
|
context.Response.StatusDescription = "OK";
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
||||||
|
{
|
||||||
|
public class DebugWebsocketSink
|
||||||
|
{
|
||||||
|
private bool _isRunning;
|
||||||
|
private string _url;
|
||||||
|
|
||||||
|
public bool IsRunning => _isRunning;
|
||||||
|
public string Url => _url;
|
||||||
|
|
||||||
|
public void StartServerAndSetPort(int port)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_url = $"ws://localhost:{port}";
|
||||||
|
_isRunning = true;
|
||||||
|
// Implement actual server startup logic here
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_isRunning = false;
|
||||||
|
throw new Exception($"Failed to start debug websocket server: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopServer()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Implement actual server shutdown logic here
|
||||||
|
_isRunning = false;
|
||||||
|
_url = null;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"Failed to stop debug websocket server: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class SomeOtherWebSocketClass
|
||||||
|
{
|
||||||
|
private ClientWebSocket _webSocket;
|
||||||
|
|
||||||
|
public SomeOtherWebSocketClass()
|
||||||
|
{
|
||||||
|
_webSocket = new ClientWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ConnectAsync(Uri uri)
|
||||||
|
{
|
||||||
|
await _webSocket.ConnectAsync(uri, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAsync(string message)
|
||||||
|
{
|
||||||
|
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
|
||||||
|
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ReceiveAsync()
|
||||||
|
{
|
||||||
|
var buffer = new byte[1024];
|
||||||
|
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||||
|
return System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CloseAsync()
|
||||||
|
{
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.Fusion
|
||||||
|
{
|
||||||
|
public class EssentialsHuddleSpaceFusionSystemControllerBase
|
||||||
|
{
|
||||||
|
private ClientWebSocket _webSocket;
|
||||||
|
|
||||||
|
public EssentialsHuddleSpaceFusionSystemControllerBase()
|
||||||
|
{
|
||||||
|
_webSocket = new ClientWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ConnectAsync(Uri uri)
|
||||||
|
{
|
||||||
|
await _webSocket.ConnectAsync(uri, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAsync(string message)
|
||||||
|
{
|
||||||
|
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
|
||||||
|
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ReceiveAsync()
|
||||||
|
{
|
||||||
|
var buffer = new byte[1024];
|
||||||
|
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||||
|
return System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CloseAsync()
|
||||||
|
{
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user