Class WebSocketServer
Inheritance
Namespace: Mars.Common.Socket.Server
Assembly: Mars.Common.dll
Syntax
public class WebSocketServer : object
Remarks
Constructors
WebSocketServer()
Declaration
public WebSocketServer()
Remarks
WebSocketServer(IPAddress, Int32)
Declaration
public WebSocketServer(IPAddress address, int port)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address |
A |
System.Int32 | port |
An |
Remarks
The new instance listens for incoming handshake requests on
address
and port
.
It provides secure connections if port
is 443.
WebSocketServer(IPAddress, Int32, Boolean)
address
, port
,
and secure
.
Declaration
public WebSocketServer(IPAddress address, int port, bool secure)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address |
A |
System.Int32 | port |
An |
System.Boolean | secure |
A true if the new instance provides
secure connections; otherwise, false .
|
Remarks
address
and port
.
WebSocketServer(Int32)
port
.
Declaration
public WebSocketServer(int port)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | port |
An |
Remarks
The new instance listens for incoming handshake requests on
port
.
It provides secure connections if port
is 443.
WebSocketServer(Int32, Boolean)
Declaration
public WebSocketServer(int port, bool secure)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | port |
An |
System.Boolean | secure |
A true if the new instance provides
secure connections; otherwise, false .
|
Remarks
port
.
WebSocketServer(String)
url
.
Declaration
public WebSocketServer(string url)
Parameters
Type | Name | Description |
---|---|---|
System.String | url |
A |
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.
Properties
Address
Declaration
public IPAddress Address { get; }
Property Value
Type | Description |
---|---|
IPAddress |
A |
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 .
|
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 |
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 |
---|---|
TimeSpan |
A The default value is the same as 1 second. |
Remarks
WebSocketServices
Declaration
public WebSocketServiceManager WebSocketServices { get; }
Property Value
Type | Description |
---|---|
WebSocketServiceManager | A WebSocketServiceManager that manages the WebSocket services provided by the server. |
Methods
AddWebSocketService<TBehaviorWithNew>(String)
path
.
Declaration
public void AddWebSocketService<TBehaviorWithNew>(string path)
where TBehaviorWithNew : WebSocketBehavior, new()
Parameters
Type | Name | Description |
---|---|---|
System.String | path |
A |
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.
AddWebSocketService<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 |
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.
AddWebSocketService<TBehavior>(String, Func<TBehavior>)
path
, and creator
.
Declaration
public void AddWebSocketService<TBehavior>(string path, Func<TBehavior> creator)
where TBehavior : WebSocketBehavior
Parameters
Type | Name | Description |
---|---|---|
System.String | path |
A |
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.
RemoveWebSocketService(String)
path
.
Declaration
public bool RemoveWebSocketService(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path |
A |
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.
Start()
Declaration
public void Start()
Remarks
Stop()
Declaration
public void Stop()
Remarks
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 The size must be 123 bytes or less in UTF-8. |
Remarks
Stop(UInt16, String)
code
and
reason
.
Declaration
public void Stop(ushort code, string reason)
Parameters
Type | Name | Description |
---|---|---|
System.UInt16 | code |
A The status codes are defined in Section 7.4 of RFC 6455. |
System.String | reason |
A The size must be 123 bytes or less in UTF-8. |