diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe
index fe86bd98..a87418bb 100755
Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ
diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb
index 106ca790..3c97fe1e 100644
Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ
diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll
index 2b866264..8b42adef 100755
Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ
diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
index 0df8e7f1..e5282e03 100644
Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ
diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe
index 1835a5dd..60a0f9b2 100755
Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ
diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb
index d2428513..bfa1d859 100644
Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ
diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll
index 2b866264..8b42adef 100755
Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ
diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
index 0df8e7f1..e5282e03 100644
Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ
diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe
index 7bfa9a1a..6714239a 100755
Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ
diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb
index ed291f11..21f8c675 100644
Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ
diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll
index 2b866264..8b42adef 100755
Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ
diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
index 0df8e7f1..e5282e03 100644
Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ
diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe
index b520c547..cfefe0a1 100755
Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ
diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb
index 2e6aa868..6027d46b 100644
Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ
diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll
index 2b866264..8b42adef 100755
Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ
diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
index 0df8e7f1..e5282e03 100644
Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ
diff --git a/websocket-sharp/ResponseHandshake.cs b/websocket-sharp/ResponseHandshake.cs
index 9d7c7eca..2aba71a1 100644
--- a/websocket-sharp/ResponseHandshake.cs
+++ b/websocket-sharp/ResponseHandshake.cs
@@ -4,7 +4,7 @@
*
* The MIT License
*
- * Copyright (c) 2012 sta.blockhead
+ * Copyright (c) 2012-2013 sta.blockhead
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -55,24 +55,16 @@ namespace WebSocketSharp {
#region Properties
public bool IsWebSocketResponse {
-
get {
- if (ProtocolVersion != HttpVersion.Version11)
- return false;
-
- if (StatusCode != "101")
- return false;
-
- if (!HeaderExists("Upgrade", "websocket"))
- return false;
-
- if (!HeaderExists("Connection", "Upgrade"))
- return false;
-
- if (!HeaderExists("Sec-WebSocket-Accept"))
- return false;
-
- return true;
+ return ProtocolVersion != HttpVersion.Version11
+ ? false
+ : StatusCode != "101"
+ ? false
+ : !HeaderExists("Upgrade", "websocket")
+ ? false
+ : !HeaderExists("Connection", "Upgrade")
+ ? false
+ : HeaderExists("Sec-WebSocket-Accept");
}
}
@@ -117,14 +109,11 @@ namespace WebSocketSharp {
public override string ToString()
{
var buffer = new StringBuilder();
-
buffer.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, StatusCode, Reason, _crlf);
-
foreach (string key in Headers.AllKeys)
buffer.AppendFormat("{0}: {1}{2}", key, Headers[key], _crlf);
buffer.Append(_crlf);
-
return buffer.ToString();
}
diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs
index f1c6b8d1..84cf04ef 100644
--- a/websocket-sharp/WebSocket.cs
+++ b/websocket-sharp/WebSocket.cs
@@ -308,13 +308,13 @@ namespace WebSocketSharp {
#region Private Methods
// As Server
- private void acceptHandshake()
+ private bool acceptHandshake()
{
if (!receiveOpeningHandshake())
- return;
+ return false;
sendResponseHandshake();
- onOpen();
+ return true;
}
private bool canSendAsCloseFrame(PayloadData data)
@@ -406,7 +406,9 @@ namespace WebSocketSharp {
{
var args = new CloseEventArgs(data);
var frame = createFrame(Fin.FINAL, Opcode.CLOSE, data);
- send(frame);
+ if (send(frame))
+ args.WasClean = true;
+
onClose(args);
}
@@ -473,7 +475,9 @@ namespace WebSocketSharp {
var host = _uri.DnsSafeHost;
var port = _uri.Port > 0
? _uri.Port
- : _isSecure ? 443 : 80;
+ : _isSecure
+ ? 443
+ : 80;
_tcpClient = new TcpClient(host, port);
_wsStream = WsStream.CreateClientStream(_tcpClient, host, _isSecure);
@@ -534,19 +538,10 @@ namespace WebSocketSharp {
}
// As Client
- private void doHandshake()
+ private bool doHandshake()
{
- var res = sendOpeningHandshake();
-
- string msg;
- if (!isValidResponse(res, out msg))
- {
- onError(msg);
- close(CloseStatusCode.ABNORMAL, msg);
- return;
- }
-
- onOpen();
+ createClientStream();
+ return sendOpeningHandshake();
}
// As Server
@@ -621,35 +616,13 @@ namespace WebSocketSharp {
}
// As Client
- private bool isValidResponse(ResponseHandshake response, out string message)
+ private bool isValidResponse(ResponseHandshake response)
{
- if (!response.IsWebSocketResponse)
- {
- message = "Invalid WebSocket response.";
- return false;
- }
-
- if (!response.HeaderExists("Sec-WebSocket-Accept", createResponseKey()))
- {
- message = "Invalid Sec-WebSocket-Accept.";
- return false;
- }
-
- if ( response.HeaderExists("Sec-WebSocket-Version") &&
- !response.HeaderExists("Sec-WebSocket-Version", _version))
- {
- message = "Unsupported Sec-WebSocket-Version.";
- return false;
- }
-
- if (response.HeaderExists("Sec-WebSocket-Protocol"))
- _protocol = response.Headers["Sec-WebSocket-Protocol"];
-
- if (response.HeaderExists("Sec-WebSocket-Extensions"))
- _extensions = response.Headers["Sec-WebSocket-Extensions"];
-
- message = String.Empty;
- return true;
+ return !response.IsWebSocketResponse
+ ? false
+ : !response.HeaderExists("Sec-WebSocket-Accept", createResponseKey())
+ ? false
+ : !response.HeaderExists("Sec-WebSocket-Version") || response.HeaderExists("Sec-WebSocket-Version", _version);
}
private void onClose(CloseEventArgs eventArgs)
@@ -658,8 +631,8 @@ namespace WebSocketSharp {
if (!_exitMessageLoop.IsNull())
_exitMessageLoop.WaitOne(5 * 1000);
- if (closeResources())
- eventArgs.WasClean = true;
+ if (!closeResources())
+ eventArgs.WasClean = false;
OnClose.Emit(this, eventArgs);
}
@@ -1011,19 +984,34 @@ namespace WebSocketSharp {
}
// As Client
- private ResponseHandshake sendOpeningHandshake()
+ private bool sendOpeningHandshake()
{
var req = createOpeningHandshake();
- sendOpeningHandshake(req);
+ sendRequestHandshake(req);
- return receiveResponseHandshake();
+ var res = receiveResponseHandshake();
+ if (!isValidResponse(res))
+ {
+ var msg = "Invalid response to the WebSocket connection request.";
+ onError(msg);
+ close(CloseStatusCode.ABNORMAL, msg);
+ return false;
+ }
+
+ if (res.HeaderExists("Sec-WebSocket-Protocol"))
+ _protocol = res.Headers["Sec-WebSocket-Protocol"];
+
+ if (res.HeaderExists("Sec-WebSocket-Extensions"))
+ _extensions = res.Headers["Sec-WebSocket-Extensions"];
+
+ return true;
}
// As Client
- private void sendOpeningHandshake(RequestHandshake request)
+ private void sendRequestHandshake(RequestHandshake request)
{
#if DEBUG
- Console.WriteLine("WS: Info@sendOpeningHandshake: Opening handshake from client:\n");
+ Console.WriteLine("WS: Info@sendRequestHandshake: Request handshake from client:\n");
Console.WriteLine(request.ToString());
#endif
writeHandshake(request);
@@ -1173,7 +1161,7 @@ namespace WebSocketSharp {
}
///
- /// Establishes a connection.
+ /// Establishes a WebSocket connection.
///
public void Connect()
{
@@ -1183,23 +1171,23 @@ namespace WebSocketSharp {
return;
}
+ Func connect = () =>
+ {
+ return _isClient
+ ? doHandshake()
+ : acceptHandshake();
+ };
+
try
{
- // As client
- if (_isClient)
- {
- createClientStream();
- doHandshake();
- return;
- }
-
- // As server
- acceptHandshake();
+ if (connect())
+ onOpen();
}
- catch (Exception ex)
+ catch (Exception)
{
- onError(ex.Message);
- close(CloseStatusCode.ABNORMAL, "An exception has occured.");
+ var msg = "An exception has occured.";
+ onError(msg);
+ close(CloseStatusCode.ABNORMAL, msg);
}
}
diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll
index 2b866264..8b42adef 100755
Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ
diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
index 0df8e7f1..e5282e03 100644
Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ
diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb
index 1541ae36..53a0a216 100644
Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ