diff --git a/README.md b/README.md
index b365609a..7c455fde 100644
--- a/README.md
+++ b/README.md
@@ -147,7 +147,13 @@ ws.Send (data);
The `Send` method is overloaded.
-The types of `data` are `string`, `byte []` and `FileInfo` class.
+The types of `data` are `string`, `byte []` and `System.IO.FileInfo` class.
+
+In addition, the `Send (stream, length)` method exists, too.
+
+These methods don't wait for the send to be complete. This means that these methods behave asynchronously.
+
+If you want to do something when the send is complete, you use any of some `Send (data, completed)` methods.
#### Step 6 ####
@@ -159,9 +165,11 @@ ws.Close (code, reason);
If you want to close the WebSocket connection explicitly, you use the `Close` method.
-The `Close` method is overloaded. The types of `code` are `WebSocketSharp.CloseStatusCode` and `ushort`, the type of `reason` is `string`.
+The `Close` method is overloaded.
+
+The types of `code` are `WebSocketSharp.CloseStatusCode` and `ushort`, and the type of `reason` is `string`.
-In addition, the `Close ()` and `Close (code)` methods exist.
+In addition, the `Close ()` and `Close (code)` methods exist, too.
### WebSocket Server ###
diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs
index cd7c1692..9db34be2 100644
--- a/websocket-sharp/Ext.cs
+++ b/websocket-sharp/Ext.cs
@@ -140,22 +140,22 @@ namespace WebSocketSharp
private static byte [] readBytes (this Stream stream, byte [] buffer, int offset, int length)
{
- var readLen = stream.Read (buffer, offset, length);
- if (readLen < 1)
+ var len = stream.Read (buffer, offset, length);
+ if (len < 1)
return buffer.SubArray (0, offset);
- var tmpLen = 0;
- while (readLen < length)
+ var tmp = 0;
+ while (len < length)
{
- tmpLen = stream.Read (buffer, offset + readLen, length - readLen);
- if (tmpLen < 1)
+ tmp = stream.Read (buffer, offset + len, length - len);
+ if (tmp < 1)
break;
- readLen += tmpLen;
+ len += tmp;
}
- return readLen < length
- ? buffer.SubArray (0, offset + readLen)
+ return len < length
+ ? buffer.SubArray (0, offset + len)
: buffer;
}
@@ -536,15 +536,15 @@ namespace WebSocketSharp
AsyncCallback callback = ar =>
{
try {
- var readLen = stream.EndRead (ar);
- var result = readLen < 1
- ? new byte []{}
- : readLen < length
- ? stream.readBytes (buffer, readLen, length - readLen)
- : buffer;
+ var len = stream.EndRead (ar);
+ var bytes = len < 1
+ ? new byte []{}
+ : len < length
+ ? stream.readBytes (buffer, len, length - len)
+ : buffer;
if (completed != null)
- completed (result);
+ completed (bytes);
}
catch (Exception ex) {
if (error != null)
diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs
index 10f713da..b9623cf6 100644
--- a/websocket-sharp/Server/WebSocketService.cs
+++ b/websocket-sharp/Server/WebSocketService.cs
@@ -316,35 +316,35 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a text to the client of the current
- /// instance.
+ /// Sends a binary data from the specified to
+ /// the client of the current instance.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A that contains a text data to send.
+ ///
+ /// A from which contains a binary data to send.
///
- protected void Send (string data)
+ protected void Send (FileInfo file)
{
if (_websocket != null)
- _websocket.Send (data, null);
+ _websocket.Send (file, null);
}
///
- /// Sends a binary data from the specified to
- /// the client of the current instance.
+ /// Sends a text to the client of the current
+ /// instance.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A from which contains a binary data to send.
+ ///
+ /// A that contains a text data to send.
///
- protected void Send (FileInfo file)
+ protected void Send (string data)
{
if (_websocket != null)
- _websocket.Send (file, null);
+ _websocket.Send (data, null);
}
///
@@ -370,14 +370,14 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a text to the client of the current
- /// instance.
+ /// Sends a binary data from the specified to
+ /// the client of the current instance.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A that contains a text data to send.
+ ///
+ /// A from which contains a binary data to send.
///
///
/// An Action<bool> delegate that references the method(s) called when
@@ -385,21 +385,21 @@ namespace WebSocketSharp.Server
/// A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
- protected void Send (string data, Action completed)
+ protected void Send (FileInfo file, Action completed)
{
if (_websocket != null)
- _websocket.Send (data, completed);
+ _websocket.Send (file, completed);
}
///
- /// Sends a binary data from the specified to
- /// the client of the current instance.
+ /// Sends a text to the client of the current
+ /// instance.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A from which contains a binary data to send.
+ ///
+ /// A that contains a text data to send.
///
///
/// An Action<bool> delegate that references the method(s) called when
@@ -407,10 +407,10 @@ namespace WebSocketSharp.Server
/// A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
- protected void Send (FileInfo file, Action completed)
+ protected void Send (string data, Action completed)
{
if (_websocket != null)
- _websocket.Send (file, completed);
+ _websocket.Send (data, completed);
}
///
@@ -426,14 +426,10 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
- protected void Send (Stream stream, int length, bool dispose)
+ protected void Send (Stream stream, int length)
{
if (_websocket != null)
- _websocket.Send (stream, length, dispose, null);
+ _websocket.Send (stream, length, null);
}
///
@@ -449,20 +445,16 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
///
/// An Action<bool> delegate that references the method(s) called when
/// the send is complete.
/// A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
- protected void Send (Stream stream, int length, bool dispose, Action completed)
+ protected void Send (Stream stream, int length, Action completed)
{
if (_websocket != null)
- _websocket.Send (stream, length, dispose, completed);
+ _websocket.Send (stream, length, completed);
}
///
diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs
index 49e3adfa..11c6966d 100644
--- a/websocket-sharp/Server/WebSocketServiceHostManager.cs
+++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs
@@ -470,13 +470,9 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
- public void Broadcast (Stream stream, int length, bool dispose)
+ public void Broadcast (Stream stream, int length)
{
- Broadcast (stream, length, dispose, null);
+ Broadcast (stream, length, null);
}
///
@@ -492,15 +488,11 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
///
/// A delegate that references the method(s) called when
/// the broadcast is complete.
///
- public void Broadcast (Stream stream, int length, bool dispose, Action completed)
+ public void Broadcast (Stream stream, int length, Action completed)
{
var msg = _state.CheckIfStarted () ??
stream.CheckIfCanRead () ??
@@ -512,36 +504,32 @@ namespace WebSocketSharp.Server
return;
}
- Action result = data =>
- {
- var readLen = data.Length;
- if (readLen == 0)
+ stream.ReadBytesAsync (
+ length,
+ data =>
{
- _logger.Error ("A data cannot be read from 'stream'.");
- return;
- }
-
- if (readLen != length)
- _logger.Warn (String.Format (
- "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
- length,
- readLen));
-
- if (dispose)
- stream.Dispose ();
-
- if (readLen <= WebSocket.FragmentLength)
- broadcast (Opcode.BINARY, data, completed);
- else
- broadcast (Opcode.BINARY, new MemoryStream (data), completed);
- };
-
- Action exception = ex =>
- {
- _logger.Fatal (ex.ToString ());
- };
-
- stream.ReadBytesAsync (length, result, exception);
+ var len = data.Length;
+ if (len == 0)
+ {
+ _logger.Error ("A data cannot be read from 'stream'.");
+ return;
+ }
+
+ if (len < length)
+ _logger.Warn (String.Format (
+ "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
+ length,
+ len));
+
+ if (len <= WebSocket.FragmentLength)
+ broadcast (Opcode.BINARY, data, completed);
+ else
+ broadcast (Opcode.BINARY, new MemoryStream (data), completed);
+ },
+ ex =>
+ {
+ _logger.Fatal (ex.ToString ());
+ });
}
///
@@ -644,13 +632,9 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
- public void BroadcastTo (string servicePath, Stream stream, int length, bool dispose)
+ public void BroadcastTo (string servicePath, Stream stream, int length)
{
- BroadcastTo (servicePath, stream, length, dispose, null);
+ BroadcastTo (servicePath, stream, length, null);
}
///
@@ -669,20 +653,16 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
///
/// A delegate that references the method(s) called when
/// the broadcast is complete.
///
public void BroadcastTo (
- string servicePath, Stream stream, int length, bool dispose, Action completed)
+ string servicePath, Stream stream, int length, Action completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
- host.Sessions.Broadcast (stream, length, dispose, completed);
+ host.Sessions.Broadcast (stream, length, completed);
}
///
@@ -1011,13 +991,9 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
- public void SendTo (string servicePath, string id, Stream stream, int length, bool dispose)
+ public void SendTo (string servicePath, string id, Stream stream, int length)
{
- SendTo (servicePath, id, stream, length, dispose, null);
+ SendTo (servicePath, id, stream, length, null);
}
///
@@ -1040,10 +1016,6 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
///
/// An Action<bool> delegate that references the method(s) called when
/// the send is complete.
@@ -1051,11 +1023,11 @@ namespace WebSocketSharp.Server
/// successfully; otherwise, false.
///
public void SendTo (
- string servicePath, string id, Stream stream, int length, bool dispose, Action completed)
+ string servicePath, string id, Stream stream, int length, Action completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
- host.Sessions.SendTo (id, stream, length, dispose, completed);
+ host.Sessions.SendTo (id, stream, length, completed);
}
///
diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs
index 831364ca..b5da20cb 100644
--- a/websocket-sharp/Server/WebSocketSessionManager.cs
+++ b/websocket-sharp/Server/WebSocketSessionManager.cs
@@ -504,13 +504,9 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
- public void Broadcast (Stream stream, int length, bool dispose)
+ public void Broadcast (Stream stream, int length)
{
- Broadcast (stream, length, dispose, null);
+ Broadcast (stream, length, null);
}
///
@@ -526,15 +522,11 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to broadcast.
///
- ///
- /// true if is disposed after a binary data broadcasted;
- /// otherwise, false.
- ///
///
/// A delegate that references the method(s) called when
/// the broadcast is complete.
///
- public void Broadcast (Stream stream, int length, bool dispose, Action completed)
+ public void Broadcast (Stream stream, int length, Action completed)
{
var msg = _state.CheckIfStarted () ??
stream.CheckIfCanRead () ??
@@ -546,36 +538,32 @@ namespace WebSocketSharp.Server
return;
}
- Action result = data =>
- {
- var readLen = data.Length;
- if (readLen == 0)
+ stream.ReadBytesAsync (
+ length,
+ data =>
{
- _logger.Error ("A data cannot be read from 'stream'.");
- return;
- }
-
- if (readLen != length)
- _logger.Warn (String.Format (
- "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
- length,
- readLen));
-
- if (dispose)
- stream.Dispose ();
-
- if (readLen <= WebSocket.FragmentLength)
- Broadcast (Opcode.BINARY, data, completed);
- else
- Broadcast (Opcode.BINARY, new MemoryStream (data), completed);
- };
-
- Action exception = ex =>
- {
- _logger.Fatal (ex.ToString ());
- };
+ var len = data.Length;
+ if (len == 0)
+ {
+ _logger.Error ("A data cannot be read from 'stream'.");
+ return;
+ }
- stream.ReadBytesAsync (length, result, exception);
+ if (len < length)
+ _logger.Warn (String.Format (
+ "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
+ length,
+ len));
+
+ if (len <= WebSocket.FragmentLength)
+ Broadcast (Opcode.BINARY, data, completed);
+ else
+ Broadcast (Opcode.BINARY, new MemoryStream (data), completed);
+ },
+ ex =>
+ {
+ _logger.Fatal (ex.ToString ());
+ });
}
///
@@ -826,13 +814,9 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
- public void SendTo (string id, Stream stream, int length, bool dispose)
+ public void SendTo (string id, Stream stream, int length)
{
- SendTo (id, stream, length, dispose, null);
+ SendTo (id, stream, length, null);
}
///
@@ -852,10 +836,6 @@ namespace WebSocketSharp.Server
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
///
/// An Action<bool> delegate that references the method(s) called when
/// the send is complete.
@@ -863,11 +843,11 @@ namespace WebSocketSharp.Server
/// successfully; otherwise, false.
///
public void SendTo (
- string id, Stream stream, int length, bool dispose, Action completed)
+ string id, Stream stream, int length, Action completed)
{
IWebSocketSession session;
if (TryGetSession (id, out session))
- session.Context.WebSocket.Send (stream, length, dispose, completed);
+ session.Context.WebSocket.Send (stream, length, completed);
}
///
diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs
index c5b0d0ff..20ecf918 100644
--- a/websocket-sharp/WebSocket.cs
+++ b/websocket-sharp/WebSocket.cs
@@ -202,7 +202,7 @@ namespace WebSocketSharp
/// is .
///
///
- /// is not valid WebSocket URL.
+ /// is invalid.
///
public WebSocket (
string url,
@@ -1603,32 +1603,32 @@ namespace WebSocketSharp
}
///
- /// Sends a text using the WebSocket connection.
+ /// Sends a binary data from the specified
+ /// using the WebSocket connection.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A that contains a text data to send.
+ ///
+ /// A from which contains a binary data to send.
///
- public void Send (string data)
+ public void Send (FileInfo file)
{
- Send (data, null);
+ Send (file, null);
}
///
- /// Sends a binary data from the specified
- /// using the WebSocket connection.
+ /// Sends a text using the WebSocket connection.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A from which contains a binary data to send.
+ ///
+ /// A that contains a text data to send.
///
- public void Send (FileInfo file)
+ public void Send (string data)
{
- Send (file, null);
+ Send (data, null);
}
///
@@ -1664,13 +1664,14 @@ namespace WebSocketSharp
}
///
- /// Sends a text using the WebSocket connection.
+ /// Sends a binary data from the specified
+ /// using the WebSocket connection.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A that contains a text data to send.
+ ///
+ /// A from which contains a binary data to send.
///
///
/// An Action<bool> delegate that references the method(s) called when
@@ -1678,9 +1679,11 @@ namespace WebSocketSharp
/// A passed to this delegate is true if the send is complete
/// successfully; otherwise, false.
///
- public void Send (string data, Action completed)
+ public void Send (FileInfo file, Action completed)
{
- var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
+ var msg = _readyState.CheckIfOpen () ??
+ (file == null ? "'file' must not be null." : null);
+
if (msg != null)
{
_logger.Error (msg);
@@ -1689,22 +1692,17 @@ namespace WebSocketSharp
return;
}
- var rawData = Encoding.UTF8.GetBytes (data);
- if (rawData.LongLength <= FragmentLength)
- send (Opcode.TEXT, rawData, completed);
- else
- send (Opcode.TEXT, new MemoryStream (rawData), completed);
+ send (Opcode.BINARY, file.OpenRead (), completed);
}
///
- /// Sends a binary data from the specified
- /// using the WebSocket connection.
+ /// Sends a text using the WebSocket connection.
///
///
/// This method does not wait for the send to be complete.
///
- ///
- /// A from which contains a binary data to send.
+ ///
+ /// A that contains a text data to send.
///
///
/// An Action<bool> delegate that references the method(s) called when
@@ -1712,11 +1710,9 @@ namespace WebSocketSharp
/// A passed to this delegate is true if the send is complete
/// successfully; otherwise, false.
///
- public void Send (FileInfo file, Action completed)
+ public void Send (string data, Action completed)
{
- var msg = _readyState.CheckIfOpen () ??
- (file == null ? "'file' must not be null." : null);
-
+ var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null)
{
_logger.Error (msg);
@@ -1725,7 +1721,11 @@ namespace WebSocketSharp
return;
}
- send (Opcode.BINARY, file.OpenRead (), completed);
+ var rawData = Encoding.UTF8.GetBytes (data);
+ if (rawData.LongLength <= FragmentLength)
+ send (Opcode.TEXT, rawData, completed);
+ else
+ send (Opcode.TEXT, new MemoryStream (rawData), completed);
}
///
@@ -1741,13 +1741,9 @@ namespace WebSocketSharp
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
- public void Send (Stream stream, int length, bool dispose)
+ public void Send (Stream stream, int length)
{
- Send (stream, length, dispose, null);
+ Send (stream, length, null);
}
///
@@ -1763,17 +1759,13 @@ namespace WebSocketSharp
///
/// An that contains the number of bytes to send.
///
- ///
- /// true if is disposed after a binary data read;
- /// otherwise, false.
- ///
///
/// An Action<bool> delegate that references the method(s) called when
/// the send is complete.
- /// A passed to this delegate is true if the send is complete
- /// successfully; otherwise, false.
+ /// A passed to this delegate is true if the send is
+ /// complete successfully; otherwise, false.
///
- public void Send (Stream stream, int length, bool dispose, Action completed)
+ public void Send (Stream stream, int length, Action completed)
{
var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ??
@@ -1787,42 +1779,38 @@ namespace WebSocketSharp
return;
}
- Action result = data =>
- {
- var readLen = data.Length;
- if (readLen == 0)
+ stream.ReadBytesAsync (
+ length,
+ data =>
{
- var err = "A data cannot be read from 'stream'.";
- _logger.Error (err);
- error (err);
-
- return;
- }
-
- if (readLen != length)
- _logger.Warn (String.Format (
- "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
- length,
- readLen));
-
- if (dispose)
- stream.Dispose ();
+ var len = data.Length;
+ if (len == 0)
+ {
+ var err = "A data cannot be read from 'stream'.";
+ _logger.Error (err);
+ error (err);
- var sent = readLen <= FragmentLength
- ? send (Opcode.BINARY, data)
- : send (Opcode.BINARY, new MemoryStream (data));
+ return;
+ }
- if (completed != null)
- completed (sent);
- };
+ if (len < length)
+ _logger.Warn (String.Format (
+ "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
+ length,
+ len));
- Action exception = ex =>
- {
- _logger.Fatal (ex.ToString ());
- error ("An exception has occured.");
- };
+ var sent = len <= FragmentLength
+ ? send (Opcode.BINARY, data)
+ : send (Opcode.BINARY, new MemoryStream (data));
- stream.ReadBytesAsync (length, result, exception);
+ if (completed != null)
+ completed (sent);
+ },
+ ex =>
+ {
+ _logger.Fatal (ex.ToString ());
+ error ("An exception has occured.");
+ });
}
///