ProtonNet Server 1.0
An Open Solution for all your socket or web api needs.
Loading...
Searching...
No Matches
XmobiTea.ProtonNetClient.WsClient Class Reference

Implements a WebSocket client over an HTTP connection. Supports both synchronous and asynchronous operations. More...

Inheritance diagram for XmobiTea.ProtonNetClient.WsClient:
XmobiTea.ProtonNetClient.HttpClient XmobiTea.ProtonNetCommon.IWebSocket XmobiTea.ProtonNetClient.IWsClient XmobiTea.ProtonNetClient.TcpClient XmobiTea.ProtonNetClient.IHttpClient XmobiTea.ProtonNetClient.IClient XmobiTea.ProtonNetClient.ITcpClient XmobiTea.ProtonNetClient.IClient XmobiTea.ProtonNetClient.IClient XmobiTea.ProtonNet.Client.Socket.Clients.SocketWsClient

Public Member Functions

WsClient(string address, int port, TcpClientOptions options) override bool Connect ()
 Initializes a new instance of the WsClient class.
 
override bool ConnectAsync ()
 Establishes an asynchronous connection to the WebSocket server.
 
int SendText (byte[] buffer)
 Sends a text message synchronously to the server.
 
int SendText (byte[] buffer, int position, int length)
 Sends a text message synchronously to the server.
 
bool SendTextAsync (byte[] buffer)
 Sends a text message asynchronously to the server.
 
bool SendTextAsync (byte[] buffer, int position, int length)
 Sends a text message asynchronously to the server.
 
int SendBinary (byte[] buffer)
 Sends a binary message synchronously to the server.
 
int SendBinary (byte[] buffer, int position, int length)
 Sends a binary message synchronously to the server.
 
bool SendBinaryAsync (byte[] buffer)
 Sends a binary message asynchronously to the server.
 
bool SendBinaryAsync (byte[] buffer, int position, int length)
 Sends a binary message asynchronously to the server.
 
int SendClose (int status, byte[] buffer)
 Sends a close frame with a status code synchronously to the server.
 
int SendClose (int status, byte[] buffer, int position, int length)
 Sends a close frame with a status code synchronously to the server.
 
bool SendCloseAsync (int status, byte[] buffer)
 Sends a close frame with a status code asynchronously to the server.
 
bool SendCloseAsync (int status, byte[] buffer, int position, int length)
 Sends a close frame with a status code asynchronously to the server.
 
int SendPing (byte[] buffer)
 Sends a ping frame synchronously to the server.
 
int SendPing (byte[] buffer, int position, int length)
 Sends a ping frame synchronously to the server.
 
bool SendPingAsync (byte[] buffer)
 Sends a ping frame asynchronously to the server.
 
bool SendPingAsync (byte[] buffer, int position, int length)
 Sends a ping frame asynchronously to the server.
 
int SendPong (byte[] buffer)
 Sends a pong frame synchronously to the server.
 
int SendPong (byte[] buffer, int position, int length)
 Sends a pong frame synchronously to the server.
 
bool SendPongAsync (byte[] buffer)
 Sends a pong frame asynchronously to the server.
 
bool SendPongAsync (byte[] buffer, int position, int length)
 Sends a pong frame asynchronously to the server.
 
virtual void OnWsConnecting (HttpRequest request)
 Prepares the WebSocket connection by setting the necessary headers.
 
virtual void OnWsConnected (HttpResponse response)
 Called when the WebSocket connection is successfully established.
 
virtual void OnWsConnected (HttpRequest request)
 Called when the WebSocket connection is successfully established.
 
virtual bool OnWsConnecting (HttpRequest request, HttpResponse response)
 OnWsConnecting.
 
virtual void OnWsDisconnecting ()
 Called when the WebSocket connection is in the process of disconnecting.
 
virtual void OnWsDisconnected ()
 Called when the WebSocket connection is successfully disconnected.
 
virtual void OnWsReceived (byte[] buffer, int position, int length)
 Processes the WebSocket frames received from the server.
 
virtual void OnWsClose (byte[] buffer, int position, int length, int status=1000)
 Handles a close frame received from the server.
 
virtual void OnWsPing (byte[] buffer, int position, int length)
 Handles a ping frame received from the server by sending a pong frame.
 
virtual void OnWsPong (byte[] buffer, int position, int length)
 Handles a pong frame received from the server.
 
virtual void OnWsError (string error)
 Handles a WebSocket error by reporting it to the client.
 
virtual void OnWsError (SocketError error)
 Handles a WebSocket error by reporting it to the client.
 
void SendUpgrade (HttpResponse response)
 Upgrades the HTTP connection to a WebSocket connection.
 
- Public Member Functions inherited from XmobiTea.ProtonNetClient.HttpClient
 HttpClient (string address, int port, TcpClientOptions options)
 Initializes a new instance of the HttpClient class.
 
int SendRequest (HttpRequest request)
 Sends an HTTP request synchronously using the provided request data.
 
bool SendRequestAsync (HttpRequest request)
 Sends an HTTP request asynchronously using the provided request data.
 
- Public Member Functions inherited from XmobiTea.ProtonNetClient.TcpClient
 TcpClient (string address, int port, TcpClientOptions options)
 Initializes a new instance of the TcpClient class.
 
INetworkStatistics GetNetworkStatistics ()
 Gets the network statistics associated with the client.
 
virtual bool Disconnect ()
 Disconnects the client from the server synchronously.
 
virtual bool Reconnect ()
 Reconnects the client to the server by first disconnecting and then reconnecting.
 
virtual bool DisconnectAsync ()
 Disconnects the client from the server asynchronously.
 
virtual bool ReconnectAsync ()
 Reconnects the client to the server asynchronously by first disconnecting and then reconnecting.
 
virtual int Send (byte[] buffer)
 Sends data to the server synchronously.
 
virtual int Send (byte[] buffer, int position, int length)
 Sends data to the server synchronously starting from a specific position in the buffer.
 
virtual bool SendAsync (byte[] buffer)
 Sends data to the server asynchronously.
 
virtual bool SendAsync (byte[] buffer, int position, int length)
 Sends data to the server asynchronously starting from a specific position in the buffer.
 
void Dispose ()
 Disposes the TCP client, releasing all resources.
 
- Public Member Functions inherited from XmobiTea.ProtonNetClient.IClient
- Public Member Functions inherited from XmobiTea.ProtonNetClient.IHttpClient
- Public Member Functions inherited from XmobiTea.ProtonNetCommon.IWebSocket
- Public Member Functions inherited from XmobiTea.ProtonNetClient.IWsClient

Protected Member Functions

override void OnConnected ()
 Called when the client has successfully connected to the server.
 
override void OnDisconnecting ()
 Called when the client is in the process of disconnecting.
 
override void OnDisconnected ()
 Called when the client has successfully disconnected from the server.
 
override void OnReceived (byte[] buffer, int position, int length)
 Processes the data received from the server.
 
override void OnReceivedResponseHeader (HttpResponse response)
 Processes the response header received from the server.
 
override void OnReceivedResponse (HttpResponse response)
 Processes the response body received from the server.
 
override void OnReceivedResponseError (HttpResponse response, string error)
 Handles errors in the response received from the server.
 
- Protected Member Functions inherited from XmobiTea.ProtonNetClient.HttpClient
- Protected Member Functions inherited from XmobiTea.ProtonNetClient.TcpClient
virtual void OnConnecting ()
 Called when the client is in the process of connecting. Can be overridden in derived classes to handle the event.
 
virtual void OnSent (int sent, int pending)
 Called when data is successfully sent to the server. Can be overridden in derived classes to handle the event.
 
virtual void OnEmpty ()
 Called when the send buffer is empty. Can be overridden in derived classes to handle the event.
 
virtual void OnError (SocketError error)
 Called when an error occurs during socket operations. Can be overridden in derived classes to handle the event.
 
virtual void Dispose (bool disposingManagedResources)
 Disposes the TCP client, releasing managed resources if specified.
 

Additional Inherited Members

- Properties inherited from XmobiTea.ProtonNetClient.HttpClient
HttpRequest Request [get]
 Gets the current HTTP request being processed by the client.
 
HttpResponse Response [get]
 Gets the current HTTP response received from the server.
 
- Properties inherited from XmobiTea.ProtonNetClient.TcpClient
string Id [get]
 Gets the unique identifier for this TCP client instance.
 
string Address [get]
 Gets the server address to which the client is connected.
 
int Port [get]
 Gets the port number on the server to which the client is connected.
 
EndPoint EndPoint [get]
 Gets the endpoint representing the server's address and port.
 
Socket Socket [get]
 Gets the underlying socket used for the connection.
 
TcpClientOptions Options [get]
 Gets the options used to configure the TCP client.
 
bool IsConnecting [get]
 Gets a value indicating whether the client is currently connecting.
 
bool IsConnected [get]
 Gets a value indicating whether the client is connected to the server.
 
bool IsDisposed [get]
 Gets a value indicating whether the client has been disposed.
 
bool IsSocketDisposed [get]
 Gets a value indicating whether the socket has been disposed.
 

Detailed Description

Implements a WebSocket client over an HTTP connection. Supports both synchronous and asynchronous operations.

Member Function Documentation

◆ Connect()

WsClient(string address, int port, TcpClientOptions options) override bool XmobiTea.ProtonNetClient.WsClient.Connect ( )
virtual

Initializes a new instance of the WsClient class.

Parameters
addressThe server address to connect to.
portThe port number on the server.
optionsTCP client options for configuring the connection.

Establishes a synchronous connection to the WebSocket server.

Returns
True if connected successfully, otherwise false.

Reimplemented from XmobiTea.ProtonNetClient.TcpClient.

◆ ConnectAsync()

override bool XmobiTea.ProtonNetClient.WsClient.ConnectAsync ( )
virtual

Establishes an asynchronous connection to the WebSocket server.

Returns
True if connection initiation is successful, otherwise false.

Reimplemented from XmobiTea.ProtonNetClient.TcpClient.

◆ OnConnected()

override void XmobiTea.ProtonNetClient.WsClient.OnConnected ( )
protectedvirtual

Called when the client has successfully connected to the server.

Reimplemented from XmobiTea.ProtonNetClient.TcpClient.

◆ OnDisconnected()

override void XmobiTea.ProtonNetClient.WsClient.OnDisconnected ( )
protectedvirtual

Called when the client has successfully disconnected from the server.

Reimplemented from XmobiTea.ProtonNetClient.HttpClient.

◆ OnDisconnecting()

override void XmobiTea.ProtonNetClient.WsClient.OnDisconnecting ( )
protectedvirtual

Called when the client is in the process of disconnecting.

Reimplemented from XmobiTea.ProtonNetClient.TcpClient.

◆ OnReceived()

override void XmobiTea.ProtonNetClient.WsClient.OnReceived ( byte[] buffer,
int position,
int length )
protectedvirtual

Processes the data received from the server.

Parameters
bufferThe buffer containing the received data.
positionThe starting position in the buffer.
lengthThe length of the received data.

Reimplemented from XmobiTea.ProtonNetClient.HttpClient.

◆ OnReceivedResponse()

override void XmobiTea.ProtonNetClient.WsClient.OnReceivedResponse ( HttpResponse response)
protectedvirtual

Processes the response body received from the server.

Parameters
responseThe HTTP response received.

Reimplemented from XmobiTea.ProtonNetClient.HttpClient.

◆ OnReceivedResponseError()

override void XmobiTea.ProtonNetClient.WsClient.OnReceivedResponseError ( HttpResponse response,
string error )
protectedvirtual

Handles errors in the response received from the server.

Parameters
responseThe HTTP response that caused the error.
errorThe error message.

Reimplemented from XmobiTea.ProtonNetClient.HttpClient.

◆ OnReceivedResponseHeader()

override void XmobiTea.ProtonNetClient.WsClient.OnReceivedResponseHeader ( HttpResponse response)
protectedvirtual

Processes the response header received from the server.

Parameters
responseThe HTTP response received.

Reimplemented from XmobiTea.ProtonNetClient.HttpClient.

◆ OnWsClose()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsClose ( byte[] buffer,
int position,
int length,
int status = 1000 )
virtual

Handles a close frame received from the server.

Parameters
bufferThe buffer containing the close frame.
positionThe starting position in the buffer.
lengthThe length of the close frame.
statusThe close status code.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsConnected() [1/2]

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsConnected ( HttpRequest request)
virtual

Called when the WebSocket connection is successfully established.

Parameters
requestThe HTTP request that initiated the WebSocket connection.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsConnected() [2/2]

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsConnected ( HttpResponse response)
virtual

Called when the WebSocket connection is successfully established.

Parameters
responseThe HTTP response that confirms the WebSocket upgrade.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

Reimplemented in XmobiTea.ProtonNet.Client.Socket.Clients.SocketWsClient.

◆ OnWsConnecting() [1/2]

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsConnecting ( HttpRequest request)
virtual

Prepares the WebSocket connection by setting the necessary headers.

Parameters
requestThe HTTP request being sent to initiate the WebSocket connection.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsConnecting() [2/2]

virtual bool XmobiTea.ProtonNetClient.WsClient.OnWsConnecting ( HttpRequest request,
HttpResponse response )
virtual

OnWsConnecting.

Parameters
requestThe HTTP request that initiated the WebSocket connection.
responseThe HTTP response that initiated the WebSocket connection.
Returns

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsDisconnected()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsDisconnected ( )
virtual

Called when the WebSocket connection is successfully disconnected.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsDisconnecting()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsDisconnecting ( )
virtual

Called when the WebSocket connection is in the process of disconnecting.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsError() [1/2]

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsError ( SocketError error)
virtual

Handles a WebSocket error by reporting it to the client.

Parameters
errorThe socket error.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsError() [2/2]

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsError ( string error)
virtual

Handles a WebSocket error by reporting it to the client.

Parameters
errorThe error message.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsPing()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsPing ( byte[] buffer,
int position,
int length )
virtual

Handles a ping frame received from the server by sending a pong frame.

Parameters
bufferThe buffer containing the ping frame.
positionThe starting position in the buffer.
lengthThe length of the ping frame.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsPong()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsPong ( byte[] buffer,
int position,
int length )
virtual

Handles a pong frame received from the server.

Parameters
bufferThe buffer containing the pong frame.
positionThe starting position in the buffer.
lengthThe length of the pong frame.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

◆ OnWsReceived()

virtual void XmobiTea.ProtonNetClient.WsClient.OnWsReceived ( byte[] buffer,
int position,
int length )
virtual

Processes the WebSocket frames received from the server.

Parameters
bufferThe buffer containing the received data.
positionThe starting position in the buffer.
lengthThe length of the received data.

Implements XmobiTea.ProtonNetCommon.IWebSocket.

Reimplemented in XmobiTea.ProtonNet.Client.Socket.Clients.SocketWsClient.

◆ SendBinary() [1/2]

int XmobiTea.ProtonNetClient.WsClient.SendBinary ( byte[] buffer)

Sends a binary message synchronously to the server.

Parameters
bufferThe byte array containing the binary message.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendBinary() [2/2]

int XmobiTea.ProtonNetClient.WsClient.SendBinary ( byte[] buffer,
int position,
int length )

Sends a binary message synchronously to the server.

Parameters
bufferThe byte array containing the binary message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendBinaryAsync() [1/2]

bool XmobiTea.ProtonNetClient.WsClient.SendBinaryAsync ( byte[] buffer)

Sends a binary message asynchronously to the server.

Parameters
bufferThe byte array containing the binary message.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendBinaryAsync() [2/2]

bool XmobiTea.ProtonNetClient.WsClient.SendBinaryAsync ( byte[] buffer,
int position,
int length )

Sends a binary message asynchronously to the server.

Parameters
bufferThe byte array containing the binary message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendClose() [1/2]

int XmobiTea.ProtonNetClient.WsClient.SendClose ( int status,
byte[] buffer )

Sends a close frame with a status code synchronously to the server.

Parameters
statusThe close status code.
bufferThe byte array containing the close message.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendClose() [2/2]

int XmobiTea.ProtonNetClient.WsClient.SendClose ( int status,
byte[] buffer,
int position,
int length )

Sends a close frame with a status code synchronously to the server.

Parameters
statusThe close status code.
bufferThe byte array containing the close message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendCloseAsync() [1/2]

bool XmobiTea.ProtonNetClient.WsClient.SendCloseAsync ( int status,
byte[] buffer )

Sends a close frame with a status code asynchronously to the server.

Parameters
statusThe close status code.
bufferThe byte array containing the close message.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendCloseAsync() [2/2]

bool XmobiTea.ProtonNetClient.WsClient.SendCloseAsync ( int status,
byte[] buffer,
int position,
int length )

Sends a close frame with a status code asynchronously to the server.

Parameters
statusThe close status code.
bufferThe byte array containing the close message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPing() [1/2]

int XmobiTea.ProtonNetClient.WsClient.SendPing ( byte[] buffer)

Sends a ping frame synchronously to the server.

Parameters
bufferThe byte array containing the ping message.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPing() [2/2]

int XmobiTea.ProtonNetClient.WsClient.SendPing ( byte[] buffer,
int position,
int length )

Sends a ping frame synchronously to the server.

Parameters
bufferThe byte array containing the ping message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPingAsync() [1/2]

bool XmobiTea.ProtonNetClient.WsClient.SendPingAsync ( byte[] buffer)

Sends a ping frame asynchronously to the server.

Parameters
bufferThe byte array containing the ping message.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPingAsync() [2/2]

bool XmobiTea.ProtonNetClient.WsClient.SendPingAsync ( byte[] buffer,
int position,
int length )

Sends a ping frame asynchronously to the server.

Parameters
bufferThe byte array containing the ping message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPong() [1/2]

int XmobiTea.ProtonNetClient.WsClient.SendPong ( byte[] buffer)

Sends a pong frame synchronously to the server.

Parameters
bufferThe byte array containing the pong message.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPong() [2/2]

int XmobiTea.ProtonNetClient.WsClient.SendPong ( byte[] buffer,
int position,
int length )

Sends a pong frame synchronously to the server.

Parameters
bufferThe byte array containing the pong message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPongAsync() [1/2]

bool XmobiTea.ProtonNetClient.WsClient.SendPongAsync ( byte[] buffer)

Sends a pong frame asynchronously to the server.

Parameters
bufferThe byte array containing the pong message.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendPongAsync() [2/2]

bool XmobiTea.ProtonNetClient.WsClient.SendPongAsync ( byte[] buffer,
int position,
int length )

Sends a pong frame asynchronously to the server.

Parameters
bufferThe byte array containing the pong message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendText() [1/2]

int XmobiTea.ProtonNetClient.WsClient.SendText ( byte[] buffer)

Sends a text message synchronously to the server.

Parameters
bufferThe byte array containing the text message.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendText() [2/2]

int XmobiTea.ProtonNetClient.WsClient.SendText ( byte[] buffer,
int position,
int length )

Sends a text message synchronously to the server.

Parameters
bufferThe byte array containing the text message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
The number of bytes sent.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendTextAsync() [1/2]

bool XmobiTea.ProtonNetClient.WsClient.SendTextAsync ( byte[] buffer)

Sends a text message asynchronously to the server.

Parameters
bufferThe byte array containing the text message.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendTextAsync() [2/2]

bool XmobiTea.ProtonNetClient.WsClient.SendTextAsync ( byte[] buffer,
int position,
int length )

Sends a text message asynchronously to the server.

Parameters
bufferThe byte array containing the text message.
positionThe starting position in the buffer.
lengthThe number of bytes to send.
Returns
True if the message was sent successfully, otherwise false.

Implements XmobiTea.ProtonNetClient.IWsClient.

◆ SendUpgrade()

void XmobiTea.ProtonNetClient.WsClient.SendUpgrade ( HttpResponse response)

Upgrades the HTTP connection to a WebSocket connection.

Parameters
responseThe HTTP response confirming the upgrade.

Implements XmobiTea.ProtonNetCommon.IWebSocket.


The documentation for this class was generated from the following file: