Updated tester example

This commit is contained in:
EonaCat 2025-03-23 20:04:29 +01:00
parent 6f1dd0db77
commit bf62dbed87
2 changed files with 46 additions and 9 deletions

View File

@ -1,7 +1,9 @@
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
using System.Net.Security;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using EonaCat.WebSockets;
@ -58,7 +60,6 @@ internal class Program
{
var serverUri = "wss://localhost:8443";
var clientUri = "wss://localhost:8443/Welcome";
var clientName = "TestClient";
var certificatePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "localhost.pfx");
var certificatePassword = "";
var requiredPassword = "";
@ -67,11 +68,22 @@ internal class Program
StartServer(serverUri, certificatePath, certificatePassword, requiredPassword);
// Start the client in the main thread
await StartClientAsync(clientUri, certificatePath, certificatePassword).ConfigureAwait(false);
Console.WriteLine("Connected to the server.");
}
private static async Task StartClientAsync(string clientUri, string certificatePath, string certificatePassword)
{
var configuration = new AsyncWebSocketClientConfiguration();
configuration.SslTargetHost = "localhost";
configuration.SslEncryptionPolicy = EncryptionPolicy.RequireEncryption;
configuration.SslCheckCertificateRevocation = false;
configuration.SslPolicyErrorsBypassed = true;
configuration.SslEnabledProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
configuration.SslClientCertificates = new X509CertificateCollection { new X509Certificate2(certificatePath, certificatePassword) };
_client = new AsyncWebSocketClient(new Uri(clientUri), OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, OnServerFragmentationStreamOpened, OnServerFragmentationStreamOpened, OnServerFragmentationStreamClosed, configuration);
await _client.ConnectAsync().ConfigureAwait(false);
//(sender, e) => Console.WriteLine($"Error: {sender}\n{e}");
Console.WriteLine("Connected to the server.");
}
private static Task OnServerFragmentationStreamClosed(AsyncWebSocketClient arg1, byte[] arg2, int arg3, int arg4)
@ -156,12 +168,21 @@ internal class Program
private static void StartServer(string serverUri, string certificatePath, string certificatePassword,
string requiredPassword)
{
// split the server uri to get the port
var uri = new Uri(serverUri);
var port = uri.Port;
var test = new AsyncWebSocketServerModuleCatalog();
//var module = new AsyncWebSocketServerModule
//test.RegisterModule();
//_server = new AsyncWebSocketServer(serverUri);
//_server.SSL.Certificate = new X509Certificate2(certificatePath, certificatePassword);
//_server.AddEndpoint<WelcomeEndpoint>("/Welcome");
//_server.Start();
test.RegisterModule(new TestModule());
var configuration = new AsyncWebSocketServerConfiguration();
configuration.SslEnabled = true;
configuration.SslEncryptionPolicy = EncryptionPolicy.RequireEncryption;
configuration.SslCheckCertificateRevocation = false;
configuration.SslPolicyErrorsBypassed = true;
configuration.SslClientCertificateRequired = true;
configuration.SslEnabledProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
configuration.SslServerCertificate = new X509Certificate2(certificatePath, certificatePassword);
_server = new AsyncWebSocketServer(port, test, configuration);
_server.Listen();
}
}

View File

@ -0,0 +1,16 @@
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
using EonaCat.WebSockets;
internal class TestModule : AsyncWebSocketServerModule
{
public TestModule()
: base("/Welcome")
{
}
public override async Task OnSessionTextReceived(AsyncWebSocketSession session, string text)
{
await session.SendTextAsync(text);
}
}