Fix for pull request #116

This commit is contained in:
sta 2015-07-20 16:23:40 +09:00
parent 8cd2248b2b
commit 87ce74010e

View File

@ -49,6 +49,7 @@ namespace WebSocketSharp
#region Private Fields #region Private Fields
private string _data; private string _data;
private bool _dataSet;
private Opcode _opcode; private Opcode _opcode;
private byte[] _rawData; private byte[] _rawData;
@ -60,7 +61,6 @@ namespace WebSocketSharp
{ {
_opcode = frame.Opcode; _opcode = frame.Opcode;
_rawData = frame.PayloadData.ApplicationData; _rawData = frame.PayloadData.ApplicationData;
_data = convertToString (_opcode, _rawData);
} }
internal MessageEventArgs (Opcode opcode, byte[] rawData) internal MessageEventArgs (Opcode opcode, byte[] rawData)
@ -70,7 +70,6 @@ namespace WebSocketSharp
_opcode = opcode; _opcode = opcode;
_rawData = rawData; _rawData = rawData;
_data = convertToString (opcode, rawData);
} }
#endregion #endregion
@ -80,19 +79,17 @@ namespace WebSocketSharp
/// <summary> /// <summary>
/// Gets the message data as a <see cref="string"/>. /// Gets the message data as a <see cref="string"/>.
/// </summary> /// </summary>
/// <remarks>
/// <para>
/// If the message data is empty, this property returns <see cref="String.Empty"/>.
/// </para>
/// <para>
/// Or if the message is a binary message, this property returns <c>"Binary"</c>.
/// </para>
/// </remarks>
/// <value> /// <value>
/// A <see cref="string"/> that represents the message data. /// A <see cref="string"/> that represents the message data,
/// or <see langword="null"/> if the message data cannot be decoded to a string.
/// </value> /// </value>
public string Data { public string Data {
get { get {
if (!_dataSet) {
_data = convertToString (_rawData, _opcode);
_dataSet = true;
}
return _data; return _data;
} }
} }
@ -125,13 +122,17 @@ namespace WebSocketSharp
#region Private Methods #region Private Methods
private static string convertToString (Opcode opcode, byte[] rawData) private static string convertToString (byte[] rawData, Opcode opcode)
{ {
return rawData.LongLength == 0 if (opcode == Opcode.Binary)
? String.Empty return BitConverter.ToString (rawData);
: opcode == Opcode.Text
? Encoding.UTF8.GetString (rawData) try {
: opcode.ToString (); return Encoding.UTF8.GetString (rawData);
}
catch {
return null;
}
} }
#endregion #endregion