Class WebSocketServer
- Namespace
- System.Net.WebSockets.Server
- Assembly
- System.Net.WebSockets.dll
The WebSocketServer class is a WebSocket Server to which WebSocket Clients can connect.
public class WebSocketServer : IDisposable
- Inheritance
-
WebSocketServer
- Implements
- Inherited Members
- Extension Methods
Constructors
WebSocketServer(WebSocketServerOptions)
Creates an instance of the System.Net.WebSockets.WebSocketServer class.
public WebSocketServer(WebSocketServerOptions options = null)
Parameters
options
WebSocketServerOptionsOptional WebSocketServerOptions where extra options can be defined.
Properties
ClientsCount
The number of Clients connected to the WebSocketServer.
public int ClientsCount { get; }
Property Value
FragmentSize
Gets the maximum allowed byte length of a partial message send by the WebSocketServer. By default if a message that exceeds the size limit it will be broken up in smaller partial messages.
public int FragmentSize { get; }
Property Value
KeepAliveInterval
The WebSocketServer protocol keep-alive interval.
public TimeSpan KeepAliveInterval { get; }
Property Value
ListClients
Gets an array of all connected client IPEndPoints.
public string[] ListClients { get; }
Property Value
- string[]
MaxClients
The maximum number of clients that can connect to the server.
public int MaxClients { get; }
Property Value
MaxReceiveFrameSize
The maximum allowed byte length of messages received by the WebSocket.
public int MaxReceiveFrameSize { get; }
Property Value
Port
The local Port to listen on.
public int Port { get; }
Property Value
Prefix
The remote Prefix clients need to connect to.
public string Prefix { get; }
Property Value
ServerName
The server name that is presented to the client during the handshake.
public string ServerName { get; }
Property Value
ServerTimeout
The WebSocketServer timeout which specifies how long to wait for a message.
public TimeSpan ServerTimeout { get; }
Property Value
Started
True, server is started. False, means server is not active.
public bool Started { get; }
Property Value
Methods
Abort()
Aborts the server and all WebSocket connections and any pending IO operations.
public void Abort()
AddWebSocket(HttpListenerContext)
public bool AddWebSocket(HttpListenerContext context)
Parameters
context
HttpListenerContext
Returns
BroadCast(byte[], int)
Broadcast a binary message to all connected clients
public void BroadCast(byte[] buffer, int fragmentSize = -1)
Parameters
buffer
byte[]The data content of the message.
fragmentSize
intOverride the maxFragmentSize used.Default -1 will use the maxFragmentSize .
Remarks
Messages are buffered send synchronously using a single send thread. The send message is not awaited.
BroadCast(string, int)
Broadcast a text message to all connected clients
public void BroadCast(string message, int fragmentSize = -1)
Parameters
message
stringThe text message.
fragmentSize
intOverride the maxFragmentSize used.Default -1 will use the maxFragmentSize .
Remarks
Messages are buffered send synchronously using a single send thread. The send message is not awaited.
DisconnectClient(string, WebSocketCloseStatus, bool)
Will start closing the WebSocket connection to the client using handshake defined in the WebSocket protocol specification section 7. After connection is closed the client will be removed from the clientpool
public void DisconnectClient(string endPoint, WebSocketCloseStatus closeStatus, bool abort = false)
Parameters
endPoint
stringThe IP endpoint of the connected client.
closeStatus
WebSocketCloseStatusIndicates the reason for closing the WebSocket connection.
abort
boolOptional, set to true to direct abort client connection in contrary to close connection.
Remarks
WebSocketCloseStatus.EndpointUnavailable will close the WebSocket synchronous without awaiting response.
Dispose()
Releases the unmanaged resources used by the System.Net.WebSockets.WebSocketServer instance.
public void Dispose()
SendData(string, byte[], int)
Sends a binary message to the specified client
public void SendData(string endPoint, byte[] buffer, int fragmentSize = -1)
Parameters
endPoint
stringThe IP endpoint of the connected client.
buffer
byte[]The data content of the message.
fragmentSize
intOverride the maxFragmentSize used.Default -1 will use the maxFragmentSize.
Remarks
Messages are buffered send synchronously using a single send thread. The send message is not awaited.
SendText(string, string, int)
Sends a text message to the specified client
public void SendText(string endPoint, string message, int fragmentSize = -1)
Parameters
endPoint
stringThe IP endpoint of the connected client.Default -1 will use the maxFragmentSize.
message
stringThe text message.
fragmentSize
intOverride the maxFragmentSize used.
Remarks
Messages are buffered send synchronously using a single send thread. The send message is not awaited.
Start()
Starts the server.
public void Start()
Stop()
Stops the server and disconnects all connected clients with code WebSocketCloseStatus.EndpointUnavailable.
public void Stop()
Events
MessageReceived
Occurs when a message is received by the server. The argument contains the received message frame.
public event WebSocketServer.MessageReceivedEventhandler MessageReceived
Event Type
Remarks
The WebSocketServer will stop to receiving any incoming messages including controller messages until the included MessageStream is completely read till the end.
WebSocketClosed
Occurs when a Client is disconnected. The argument contains the disconnected clients endpoint.
public event WebSocketServer.WebSocketClosedEventhandler WebSocketClosed
Event Type
WebSocketOpened
Occurs when a new Client is connected. The argument contains the connected clients endpoint.
public event WebSocketServer.WebSocketOpenedEventhandler WebSocketOpened