Added ConnectAsync method to the WebSocket class

This commit is contained in:
sta 2014-01-05 16:02:54 +09:00
parent e7d3a2bb17
commit 58c392f37a
3 changed files with 151 additions and 103 deletions

View File

@ -124,6 +124,7 @@ namespace Example {
//ws.SetCookie(new Cookie("dora", "tanuki")); //ws.SetCookie(new Cookie("dora", "tanuki"));
//ws.SetCredentials ("nobita", "password", false); //ws.SetCredentials ("nobita", "password", false);
ws.Connect(); ws.Connect();
//ws.ConnectAsync();
//Console.WriteLine("Compression: {0}", ws.Compression); //Console.WriteLine("Compression: {0}", ws.Compression);
Thread.Sleep(500); Thread.Sleep(500);

View File

@ -200,14 +200,14 @@ namespace WebSocketSharp.Server
{ {
_context = context; _context = context;
_sessions = sessions; _sessions = sessions;
_websocket = context.WebSocket; _websocket = context.WebSocket;
_websocket.CookiesValidation = ValidateCookies; _websocket.CookiesValidation = ValidateCookies;
_websocket.OnOpen += onOpen; _websocket.OnOpen += onOpen;
_websocket.OnMessage += onMessage; _websocket.OnMessage += onMessage;
_websocket.OnError += onError; _websocket.OnError += onError;
_websocket.OnClose += onClose; _websocket.OnClose += onClose;
_websocket.ConnectAsServer ();
_websocket.Connect ();
} }
#endregion #endregion

View File

@ -83,6 +83,7 @@ namespace WebSocketSharp
private string _extensions; private string _extensions;
private AutoResetEvent _exitReceiving; private AutoResetEvent _exitReceiving;
private object _forClose; private object _forClose;
private object _forConnect;
private object _forSend; private object _forSend;
private volatile Logger _logger; private volatile Logger _logger;
private uint _nonceCount; private uint _nonceCount;
@ -206,7 +207,7 @@ namespace WebSocketSharp
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="CompressionMethod"/> values that represents the /// 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"/>. /// The default value is <see cref="CompressionMethod.NONE"/>.
/// </value> /// </value>
public CompressionMethod Compression { public CompressionMethod Compression {
@ -215,6 +216,7 @@ namespace WebSocketSharp
} }
set { set {
lock (_forConnect) {
var msg = !_client var msg = !_client
? "Set operation of Compression isn't available as a server." ? "Set operation of Compression isn't available as a server."
: IsOpened : IsOpened
@ -231,6 +233,7 @@ namespace WebSocketSharp
_compression = value; _compression = value;
} }
} }
}
/// <summary> /// <summary>
/// Gets the cookies used in the WebSocket connection request. /// Gets the cookies used in the WebSocket connection request.
@ -315,9 +318,6 @@ namespace WebSocketSharp
} }
internal set { internal set {
if (value == null)
return;
_logger = value; _logger = value;
} }
} }
@ -347,6 +347,7 @@ namespace WebSocketSharp
} }
set { set {
lock (_forConnect) {
string msg = null; string msg = null;
if (!_client) if (!_client)
msg = "Set operation of Origin isn't available as a server."; msg = "Set operation of Origin isn't available as a server.";
@ -373,6 +374,7 @@ namespace WebSocketSharp
_origin = value.TrimEnd ('/'); _origin = value.TrimEnd ('/');
} }
} }
}
/// <summary> /// <summary>
/// Gets the WebSocket subprotocol selected by the server. /// Gets the WebSocket subprotocol selected by the server.
@ -419,6 +421,7 @@ namespace WebSocketSharp
} }
set { set {
lock (_forConnect) {
var msg = !_client var msg = !_client
? "Set operation of ServerCertificateValidationCallback isn't available as a server." ? "Set operation of ServerCertificateValidationCallback isn't available as a server."
: IsOpened : IsOpened
@ -435,6 +438,7 @@ namespace WebSocketSharp
_certValidationCallback = value; _certValidationCallback = value;
} }
} }
}
/// <summary> /// <summary>
/// Gets the WebSocket URL to connect. /// Gets the WebSocket URL to connect.
@ -645,11 +649,26 @@ namespace WebSocketSharp
return true; return true;
} }
private bool connect () private void connect ()
{ {
return _client lock (_forConnect) {
? doHandshake () if (IsOpened) {
: acceptHandshake (); 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 // As client
@ -804,6 +823,7 @@ namespace WebSocketSharp
_cookies = new CookieCollection (); _cookies = new CookieCollection ();
_extensions = String.Empty; _extensions = String.Empty;
_forClose = new object (); _forClose = new object ();
_forConnect = new object ();
_forSend = new object (); _forSend = new object ();
_protocol = String.Empty; _protocol = String.Empty;
_readyState = WebSocketState.CONNECTING; _readyState = WebSocketState.CONNECTING;
@ -1353,6 +1373,19 @@ namespace WebSocketSharp
OnClose.Emit (this, args); 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) internal bool Ping (byte [] frameAsBytes, int timeOut)
{ {
return send (frameAsBytes) && return send (frameAsBytes) &&
@ -1530,8 +1563,7 @@ namespace WebSocketSharp
/// </summary> /// </summary>
public void Connect () public void Connect ()
{ {
if (IsOpened) if (IsOpened) {
{
var msg = "A WebSocket connection has already been established."; var msg = "A WebSocket connection has already been established.";
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1539,13 +1571,24 @@ namespace WebSocketSharp
return; return;
} }
try { connect ();
if (connect ())
open ();
} }
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> /// <summary>
@ -1840,6 +1883,7 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SetCookie (Cookie cookie) public void SetCookie (Cookie cookie)
{ {
lock (_forConnect) {
var msg = !_client var msg = !_client
? "SetCookie isn't available as a server." ? "SetCookie isn't available as a server."
: IsOpened : IsOpened
@ -1859,6 +1903,7 @@ namespace WebSocketSharp
_cookies.SetOrRemove (cookie); _cookies.SetOrRemove (cookie);
} }
} }
}
/// <summary> /// <summary>
/// Sets a pair of the <paramref name="username"/> and /// Sets a pair of the <paramref name="username"/> and
@ -1877,6 +1922,7 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SetCredentials (string username, string password, bool preAuth) public void SetCredentials (string username, string password, bool preAuth)
{ {
lock (_forConnect) {
string msg = null; string msg = null;
if (!_client) if (!_client)
msg = "SetCredentials isn't available as a server."; msg = "SetCredentials isn't available as a server.";
@ -1908,6 +1954,7 @@ namespace WebSocketSharp
username, password, _uri.PathAndQuery); username, password, _uri.PathAndQuery);
_preAuth = preAuth; _preAuth = preAuth;
} }
}
#endregion #endregion
} }