Good day,
I am a self-taught developer aiming to expand my knowledge and improve my skills. Currently, I am working on establishing a secure WebSocket (WSS) connection in a C# console application using the WebSocketSharp library. Despite my efforts, I am encountering several issues and would appreciate some guidance.
Goal: Establish a secure WebSocket (WSS) connection between a server and a client using the WebSocketSharp library in a C# console application.
Current Status:
- WebSocket (ws) connection works over the internet using the domain name.
- WSS connection is problematic: The server starts without “HMAC is not supported” error, but the client immediately triggers ws.OnClose without ever triggering ws.OnOpen.
Problems:
- In the Unity implementation, I encountered the “HMAC is not supported” error, which I tried to resolve using Bouncy Castle but without success.
- In the Visual Studio C# console app, the WSS connection starts and immediately closes without establishing a successful connection.
Environment:
- C# Console App in Visual Studio 2022
- Unity Game Engine
- WebSocketSharp library: websocket-sharp-core(https://github.com/ImoutoChan/websocket-sharp-core)
- Windows OS
- Router port forwarding for server setup
- DuckDNS for domain name
- SSL certificate from Let’s Encrypt
Client Code:
using System;a
using WebSocketSharp;
namespace ClientLast
{
class Program
{
static void Main(string[] args)
{
try
{
using (var ws = new WebSocket("ws://localhost:50001/Echo"))
//using (var ws = new WebSocket("wss://abyza.duckdns.org:50001/Echo"))
{
ws.OnMessage += (sender, e) =>
Console.WriteLine("Received from server: " + e.Data);
ws.OnOpen += (sender, e) =>
Console.WriteLine("Connection opened");
ws.OnClose += (sender, e) =>
Console.WriteLine("Connection closed: " + e.Reason);
ws.OnError += (sender, e) =>
Console.WriteLine("Error: " + e.Message);
ws.Connect();
ws.Send("Hello, World!");
Console.ReadKey(true);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
}
}
}
}
Server Code:
using System;
using System.Security.Cryptography.X509Certificates;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace serverLast
{
class Program
{
static void Main(string[] args)
{
try
{
var wssv = new WebSocketServer(System.Net.IPAddress.Any, 50001);
//var wssv = new WebSocketServer(System.Net.IPAddress.Any, 50001,true);
// Load the PFX certificate
var cert = new X509Certificate2("D:\3_apps\5_vscode_projects\website server\sslcert - Copy//certificate.pfx", "pass");
wssv.SslConfiguration.ServerCertificate = cert;
wssv.AddWebSocketService<Echo>("/Echo");
wssv.Start();
Console.WriteLine("WebSocket server started on wss://localhost:443");
}
catch (Exception ex)
{
Console.WriteLine($"Error starting server: {ex.Message}");
}
Console.ReadKey();
}
public class Echo : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
Console.WriteLine($"Received message: {e.Data}");
Send(e.Data);
}
}
}
}
SSL Certificate:
- Obtained using DuckDNS and Let’s Encrypt’s Certbot
- Certificate files: cert.pem, chain.pem, fullchain.pem, privkey.pem
- Converted to .pfx using:
openssl pkcs12 -export -out certificate.pfx -in combined.pem
Other Condiderations:
- I used port forwarding on my home router to my laptop to serve the website, WebSocket, and SSL certificate server.
- I know the certificate works because I used the fullchain.pem and privkey.pem files in a Python file to run a simple website, which I could successfully open using HTTPS.
- To create the .pfx file, I first combined the fullchain.pem and privkey.pem into a single combined.pem file.
Questions:
- What might be causing the immediate closure of the WSS connection in the C# console app?
- How can I resolve the “HMAC is not supported” error in Unity for establishing a secure WebSocket connection?
- Are there any additional configurations or steps I might have missed in setting up the WebSocketSharp library for secure connections?
Thank you for your assistance!
ABYZA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.