Added ConnectAsync method to the WebSocket class
This commit is contained in:
parent
e7d3a2bb17
commit
58c392f37a
@ -124,6 +124,7 @@ namespace Example {
|
||||
//ws.SetCookie(new Cookie("dora", "tanuki"));
|
||||
//ws.SetCredentials ("nobita", "password", false);
|
||||
ws.Connect();
|
||||
//ws.ConnectAsync();
|
||||
//Console.WriteLine("Compression: {0}", ws.Compression);
|
||||
|
||||
Thread.Sleep(500);
|
||||
|
@ -200,14 +200,14 @@ namespace WebSocketSharp.Server
|
||||
{
|
||||
_context = context;
|
||||
_sessions = sessions;
|
||||
|
||||
_websocket = context.WebSocket;
|
||||
_websocket.CookiesValidation = ValidateCookies;
|
||||
_websocket.OnOpen += onOpen;
|
||||
_websocket.OnMessage += onMessage;
|
||||
_websocket.OnError += onError;
|
||||
_websocket.OnClose += onClose;
|
||||
|
||||
_websocket.Connect ();
|
||||
_websocket.ConnectAsServer ();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -83,6 +83,7 @@ namespace WebSocketSharp
|
||||
private string _extensions;
|
||||
private AutoResetEvent _exitReceiving;
|
||||
private object _forClose;
|
||||
private object _forConnect;
|
||||
private object _forSend;
|
||||
private volatile Logger _logger;
|
||||
private uint _nonceCount;
|
||||
@ -206,7 +207,7 @@ namespace WebSocketSharp
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// One of the <see cref="CompressionMethod"/> values that represents the
|
||||
/// compression method to use.
|
||||
/// compression method used to compress.
|
||||
/// The default value is <see cref="CompressionMethod.NONE"/>.
|
||||
/// </value>
|
||||
public CompressionMethod Compression {
|
||||
@ -215,6 +216,7 @@ namespace WebSocketSharp
|
||||
}
|
||||
|
||||
set {
|
||||
lock (_forConnect) {
|
||||
var msg = !_client
|
||||
? "Set operation of Compression isn't available as a server."
|
||||
: IsOpened
|
||||
@ -231,6 +233,7 @@ namespace WebSocketSharp
|
||||
_compression = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cookies used in the WebSocket connection request.
|
||||
@ -315,9 +318,6 @@ namespace WebSocketSharp
|
||||
}
|
||||
|
||||
internal set {
|
||||
if (value == null)
|
||||
return;
|
||||
|
||||
_logger = value;
|
||||
}
|
||||
}
|
||||
@ -347,6 +347,7 @@ namespace WebSocketSharp
|
||||
}
|
||||
|
||||
set {
|
||||
lock (_forConnect) {
|
||||
string msg = null;
|
||||
if (!_client)
|
||||
msg = "Set operation of Origin isn't available as a server.";
|
||||
@ -373,6 +374,7 @@ namespace WebSocketSharp
|
||||
_origin = value.TrimEnd ('/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the WebSocket subprotocol selected by the server.
|
||||
@ -419,6 +421,7 @@ namespace WebSocketSharp
|
||||
}
|
||||
|
||||
set {
|
||||
lock (_forConnect) {
|
||||
var msg = !_client
|
||||
? "Set operation of ServerCertificateValidationCallback isn't available as a server."
|
||||
: IsOpened
|
||||
@ -435,6 +438,7 @@ namespace WebSocketSharp
|
||||
_certValidationCallback = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the WebSocket URL to connect.
|
||||
@ -645,11 +649,26 @@ namespace WebSocketSharp
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool connect ()
|
||||
private void connect ()
|
||||
{
|
||||
return _client
|
||||
? doHandshake ()
|
||||
: acceptHandshake ();
|
||||
lock (_forConnect) {
|
||||
if (IsOpened) {
|
||||
var msg = "A WebSocket connection has already been established.";
|
||||
_logger.Error (msg);
|
||||
error (msg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (_client ? doHandshake () : acceptHandshake ())
|
||||
open ();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
processException (
|
||||
ex, "An exception has occurred while connecting or opening.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// As client
|
||||
@ -804,6 +823,7 @@ namespace WebSocketSharp
|
||||
_cookies = new CookieCollection ();
|
||||
_extensions = String.Empty;
|
||||
_forClose = new object ();
|
||||
_forConnect = new object ();
|
||||
_forSend = new object ();
|
||||
_protocol = String.Empty;
|
||||
_readyState = WebSocketState.CONNECTING;
|
||||
@ -1353,6 +1373,19 @@ namespace WebSocketSharp
|
||||
OnClose.Emit (this, args);
|
||||
}
|
||||
|
||||
// As server
|
||||
internal void ConnectAsServer ()
|
||||
{
|
||||
try {
|
||||
if (acceptHandshake ())
|
||||
open ();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
processException (
|
||||
ex, "An exception has occurred while connecting or opening.");
|
||||
}
|
||||
}
|
||||
|
||||
internal bool Ping (byte [] frameAsBytes, int timeOut)
|
||||
{
|
||||
return send (frameAsBytes) &&
|
||||
@ -1530,8 +1563,7 @@ namespace WebSocketSharp
|
||||
/// </summary>
|
||||
public void Connect ()
|
||||
{
|
||||
if (IsOpened)
|
||||
{
|
||||
if (IsOpened) {
|
||||
var msg = "A WebSocket connection has already been established.";
|
||||
_logger.Error (msg);
|
||||
error (msg);
|
||||
@ -1539,13 +1571,24 @@ namespace WebSocketSharp
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (connect ())
|
||||
open ();
|
||||
connect ();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
processException (ex, "An exception has occurred while connecting or opening.");
|
||||
|
||||
/// <summary>
|
||||
/// Establishes a WebSocket connection asynchronously.
|
||||
/// </summary>
|
||||
public void ConnectAsync ()
|
||||
{
|
||||
if (IsOpened) {
|
||||
var msg = "A WebSocket connection has already been established.";
|
||||
_logger.Error (msg);
|
||||
error (msg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Action connector = connect;
|
||||
connector.BeginInvoke (ar => connector.EndInvoke (ar), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1840,6 +1883,7 @@ namespace WebSocketSharp
|
||||
/// </param>
|
||||
public void SetCookie (Cookie cookie)
|
||||
{
|
||||
lock (_forConnect) {
|
||||
var msg = !_client
|
||||
? "SetCookie isn't available as a server."
|
||||
: IsOpened
|
||||
@ -1859,6 +1903,7 @@ namespace WebSocketSharp
|
||||
_cookies.SetOrRemove (cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a pair of the <paramref name="username"/> and
|
||||
@ -1877,6 +1922,7 @@ namespace WebSocketSharp
|
||||
/// </param>
|
||||
public void SetCredentials (string username, string password, bool preAuth)
|
||||
{
|
||||
lock (_forConnect) {
|
||||
string msg = null;
|
||||
if (!_client)
|
||||
msg = "SetCredentials isn't available as a server.";
|
||||
@ -1908,6 +1954,7 @@ namespace WebSocketSharp
|
||||
username, password, _uri.PathAndQuery);
|
||||
_preAuth = preAuth;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user