Issues Establishing Secure WebSocket (WSS) Connection in C# Console App Using WebSocketSharp

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:

  1. In the Unity implementation, I encountered the “HMAC is not supported” error, which I tried to resolve using Bouncy Castle but without success.
  2. 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:

  1. What might be causing the immediate closure of the WSS connection in the C# console app?
  2. How can I resolve the “HMAC is not supported” error in Unity for establishing a secure WebSocket connection?
  3. 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!

New contributor

ABYZA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật