Class WebSocketServer
Inheritance
Inherited Members
Namespace: Mars.Common.Socket.Server
Assembly: Mars.Common.dll
Syntax
public class WebSocketServer
Remarks
Constructors
| Improve this Doc View SourceWebSocketServer()
Declaration
public WebSocketServer()
Remarks
WebSocketServer(Int32, Boolean)
Declaration
public WebSocketServer(int port, bool secure)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | port | An System.Int32 that represents the number of the port on which to listen. |
System.Boolean | secure |
A System.Boolean: true if the new instance provides
secure connections; otherwise, false .
|
Remarks
port
.
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
port is less than 1 or greater than 65535.
|
WebSocketServer(Int32)
port
.
Declaration
public WebSocketServer(int port)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | port | An System.Int32 that represents the number of the port on which to listen. |
Remarks
The new instance listens for incoming handshake requests on
System.Net.IPAddress.Any and port
.
It provides secure connections if port
is 443.
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
port is less than 1 or greater than 65535.
|
WebSocketServer(IPAddress, Int32, Boolean)
address
, port
,
and secure
.
Declaration
public WebSocketServer(IPAddress address, int port, bool secure)
Parameters
Type | Name | Description |
---|---|---|
System.Net.IPAddress | address | A System.Net.IPAddress that represents the local IP address on which to listen. |
System.Int32 | port | An System.Int32 that represents the number of the port on which to listen. |
System.Boolean | secure |
A System.Boolean: true if the new instance provides
secure connections; otherwise, false .
|
Remarks
address
and port
.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
address is null.
|
System.ArgumentException |
address is not a local IP address.
|
System.ArgumentOutOfRangeException |
port is less than 1 or greater than 65535.
|
WebSocketServer(IPAddress, Int32)
Declaration
public WebSocketServer(IPAddress address, int port)
Parameters
Type | Name | Description |
---|---|---|
System.Net.IPAddress | address | A System.Net.IPAddress that represents the local IP address on which to listen. |
System.Int32 | port | An System.Int32 that represents the number of the port on which to listen. |
Remarks
The new instance listens for incoming handshake requests on
address
and port
.
It provides secure connections if port
is 443.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
address is null.
|
System.ArgumentException |
address is not a local IP address.
|
System.ArgumentOutOfRangeException |
port is less than 1 or greater than 65535.
|
WebSocketServer(String)
url
.
Declaration
public WebSocketServer(string url)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | A System.String that represents the WebSocket URL of the server. |
Remarks
The new instance listens for incoming handshake requests on
the IP address of the host of url
and
the port of url
.
Either port 80 or 443 is used if url
includes
no port. Port 443 is used if the scheme of url
is wss; otherwise, port 80 is used.
The new instance provides secure connections if the scheme of
url
is wss.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
url is null.
|
System.ArgumentException |
-or-
|
Properties
| Improve this Doc View SourceAddress
Declaration
public IPAddress Address { get; }
Property Value
Type | Description |
---|---|
System.Net.IPAddress | A System.Net.IPAddress that represents the local IP address on which to listen for incoming handshake requests. |
Remarks
AllowForwardedRequest
Declaration
public bool AllowForwardedRequest { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
The default value is |
Remarks
IsListening
Declaration
public bool IsListening { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
true if the server has started; otherwise, false .
|
Remarks
KeepClean
Declaration
public bool KeepClean { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
The default value is |
Remarks
Port
Declaration
public int Port { get; }
Property Value
Type | Description |
---|---|
System.Int32 | An System.Int32 that represents the number of the port on which to listen for incoming handshake requests. |
Remarks
ReuseAddress
Declaration
public bool ReuseAddress { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
The default value is |
Remarks
You should set this property to true
if you would
like to resolve to wait for socket in TIME_WAIT state.
The set operation does nothing if the server has already started or it is shutting down.
WaitTime
Declaration
public TimeSpan WaitTime { get; set; }
Property Value
Type | Description |
---|---|
System.TimeSpan |
A System.TimeSpan to wait for the response. The default value is the same as 1 second. |
Remarks
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException | The value specified for a set operation is zero or less. |
WebSocketServices
Declaration
public WebSocketServiceManager WebSocketServices { get; }
Property Value
Type | Description |
---|---|
WebSocketServiceManager | A WebSocketServiceManager that manages the WebSocket services provided by the server. |
Remarks
Methods
| Improve this Doc View SourceAddWebSocketService<TBehaviorWithNew>(String, Action<TBehaviorWithNew>)
path
, and initializer
.
Declaration
public void AddWebSocketService<TBehaviorWithNew>(string path, Action<TBehaviorWithNew> initializer)
where TBehaviorWithNew : WebSocketBehavior, new()
Parameters
Type | Name | Description |
---|---|---|
System.String | path | A System.String that represents an absolute path to the service to add. |
System.Action<TBehaviorWithNew> | initializer |
An That delegate invokes the method called for initializing a new session instance for the service. |
Type Parameters
Name | Description |
---|---|
TBehaviorWithNew |
The type of the behavior for the service. It must inherit the WebSocketBehavior class and have a public parameterless constructor. |
Remarks
path
is converted to a URL-decoded string and
'/' is trimmed from the end of the converted string if any.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
path is null.
|
System.ArgumentException |
-or-
-or-
-or-
|
AddWebSocketService<TBehavior>(String, Func<TBehavior>)
path
, and creator
.
Declaration
[Obsolete("This method will be removed. Use added one instead.")]
public void AddWebSocketService<TBehavior>(string path, Func<TBehavior> creator)
where TBehavior : WebSocketBehavior
Parameters
Type | Name | Description |
---|---|---|
System.String | path | A System.String that represents an absolute path to the service to add. |
System.Func<TBehavior> | creator |
A It invokes the method called for creating a new session instance for the service. The method must create a new instance of the specified behavior class and return it. |
Type Parameters
Name | Description |
---|---|
TBehavior |
The type of the behavior for the service. It must inherit the WebSocketBehavior class. |
Remarks
path
is converted to a URL-decoded string and
'/' is trimmed from the end of the converted string if any.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
-or-
|
System.ArgumentException |
-or-
-or-
-or-
|
AddWebSocketService<TBehaviorWithNew>(String)
path
.
Declaration
public void AddWebSocketService<TBehaviorWithNew>(string path)
where TBehaviorWithNew : WebSocketBehavior, new()
Parameters
Type | Name | Description |
---|---|---|
System.String | path | A System.String that represents an absolute path to the service to add. |
Type Parameters
Name | Description |
---|---|
TBehaviorWithNew |
The type of the behavior for the service. It must inherit the WebSocketBehavior class and have a public parameterless constructor. |
Remarks
path
is converted to a URL-decoded string and
'/' is trimmed from the end of the converted string if any.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
path is null.
|
System.ArgumentException |
-or-
-or-
-or-
|
RemoveWebSocketService(String)
path
.
Declaration
public bool RemoveWebSocketService(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | A System.String that represents an absolute path to the service to remove. |
Returns
Type | Description |
---|---|
System.Boolean |
true if the service is successfully found and removed;
otherwise, false .
|
Remarks
path
is converted to a URL-decoded string and
'/' is trimmed from the end of the converted string if any.
The service is stopped with close status 1001 (going away) if it has already started.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
path is null.
|
System.ArgumentException |
-or-
-or-
|
Start()
Declaration
public void Start()
Remarks
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException |
There is no server certificate for secure connections. -or- The underlying System.Net.Sockets.TcpListener has failed to start. |
Stop()
Declaration
public void Stop()
Remarks
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | The underlying System.Net.Sockets.TcpListener has failed to stop. |
Stop(CloseStatusCode, String)
code
and
reason
.
Declaration
public void Stop(CloseStatusCode code, string reason)
Parameters
Type | Name | Description |
---|---|---|
CloseStatusCode | code |
One of the CloseStatusCode enum values. It represents the status code indicating the reason for the close. |
System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
Exceptions
Type | Condition |
---|---|
System.ArgumentException |
-or-
-or-
|
System.ArgumentOutOfRangeException |
The size of reason is greater than 123 bytes.
|
System.InvalidOperationException | The underlying System.Net.Sockets.TcpListener has failed to stop. |
Stop(UInt16, String)
code
and
reason
.
Declaration
public void Stop(ushort code, string reason)
Parameters
Type | Name | Description |
---|---|---|
System.UInt16 | code |
A System.UInt16 that represents the status code indicating the reason for the close. The status codes are defined in Section 7.4 of RFC 6455. |
System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
-or-
The size of |
System.ArgumentException |
-or-
-or-
|
System.InvalidOperationException | The underlying System.Net.Sockets.TcpListener has failed to stop. |