Using WebSocket Client in .NET

TsgcWebSocketClient is the .NET wrapper around the sgcWebSockets runtime client. It inherits all core WebSocket capabilities from TsgcWebSocketClient_base, giving .NET applications a component-style API for connecting, sending, and receiving over WebSockets  

Key Properties

  • Host & Port – target server endpoint
  • Options.Parameters – URL path or query parameters for the WebSocket handshake
  • TLS & TLSOptions – enable TLS and select protocol version/IO handler
  • Specifications.RFC6455 – toggle RFC6455 WebSocket framing compliance
  • Proxy – HTTP proxy configuration (Enabled, Username, Password, Host, Port)
  • Extensions.PerMessage_Deflate.Enabled – enable per‑message compression
  • Authentication – basic authentication credentials when needed
  • Active – set true to connect, false to disconnect

Events

Register event handlers to react to connection lifecycle and data:

  • OnConnect – fired after a successful connection; sample handler logs the peer IP
  • OnDisconnect – triggered when the connection closes, providing the close code
  • OnMessage – receives text messages from the server
  • OnError – reports protocol or socket errors
  • OnException – exposes unexpected exceptions raised within the component

Example

using esegece.sgcWebSockets;

var client = new TsgcWebSocketClient();
client.OnConnect    += OnConnectEvent;
client.OnDisconnect += OnDisconnectEvent;
client.OnException  += OnExceptionEvent;
client.OnError      += OnErrorEvent;
client.OnMessage    += OnMessageEvent;

client.Host = "www.esegece.com";
client.Port = 2052;
client.Options.Parameters = "/";
client.TLS = false;
client.Specifications.RFC6455 = true;
client.Active = true;

// Sending data once connected
client.WriteData("Hello WebSocket!");
 

This snippet mirrors the demo usage: events are wired up before connecting, key properties are configured (host, port, TLS, RFC6455, etc.), and setting Active to true initiates the connection.

With the event handlers defined (as shown above), the client can log connections, receive messages, and send data through WriteData

×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

WebAuthn, Passkeys, and the End of Passwords
sgcWebSockets 2025.7

Related Posts