ISocketClient

The ISocketClient interface provides the core functionalities for a socket client, allowing it to connect to and communicate with a socket server. This interface tracks the connection status, handles incoming messages, and offers the necessary properties to monitor the connection state.


Configured with SocketBuilder:

ISocketClient is created using the SocketBuilder class. The SocketBuilder takes client configuration settings (e.g., server connection details, message processing configurations) and creates an appropriate client instance. This ensures the client is set up correctly before initiating any operations, such as connecting to the server and sending or receiving messages.


Operations Possible with ISocketClient:

  • Managing Connection:

    The client can establish a connection to the server using the Connect() method and disconnect using the Disconnect() method.

  • Sending and Receiving Messages: The client can send messages to the server using the SendMessageAsync() method. Messages from the server are received through the OnMessageReceived event.


Additional Properties and Events:

  • IsConnected Property: This property returns a boolean value indicating whether the client is currently connected to the server. It returns true if connected and false if disconnected.

if (socketClient.IsConnected)
{
    Console.WriteLine("Client is connected.");
}
  • OnConnectionChanged Event: This event is triggered when the connection status changes. It passes a boolean value that indicates whether the client has connected (true) or disconnected (false).

socketClient.OnConnectionChanged += (isConnected) =>
{
    Console.WriteLine($"Client connection status: {isConnected}");
};
  • OnMessageReceived Event: This event is triggered when a message is received from the server. The message content is passed as a string to the event handler.

socketClient.OnMessageReceived += (message) =>
{
    Console.WriteLine($"Message received: {message}");
};

Operations:

The ISocketClient interface provides all the essential functions for the socket client to operate effectively. These functionalities depend on the proper configuration and the correct handling of connection and message exchange processes.

Last updated