From 87ce74010e22cab50bc4090ded6693378a1c08c9 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 20 Jul 2015 16:23:40 +0900 Subject: [PATCH] Fix for pull request #116 --- websocket-sharp/MessageEventArgs.cs | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/websocket-sharp/MessageEventArgs.cs b/websocket-sharp/MessageEventArgs.cs index ec257c39..05be0c57 100644 --- a/websocket-sharp/MessageEventArgs.cs +++ b/websocket-sharp/MessageEventArgs.cs @@ -49,6 +49,7 @@ namespace WebSocketSharp #region Private Fields private string _data; + private bool _dataSet; private Opcode _opcode; private byte[] _rawData; @@ -60,7 +61,6 @@ namespace WebSocketSharp { _opcode = frame.Opcode; _rawData = frame.PayloadData.ApplicationData; - _data = convertToString (_opcode, _rawData); } internal MessageEventArgs (Opcode opcode, byte[] rawData) @@ -70,7 +70,6 @@ namespace WebSocketSharp _opcode = opcode; _rawData = rawData; - _data = convertToString (opcode, rawData); } #endregion @@ -80,19 +79,17 @@ namespace WebSocketSharp /// /// Gets the message data as a . /// - /// - /// - /// If the message data is empty, this property returns . - /// - /// - /// Or if the message is a binary message, this property returns "Binary". - /// - /// /// - /// A that represents the message data. + /// A that represents the message data, + /// or if the message data cannot be decoded to a string. /// public string Data { get { + if (!_dataSet) { + _data = convertToString (_rawData, _opcode); + _dataSet = true; + } + return _data; } } @@ -125,13 +122,17 @@ namespace WebSocketSharp #region Private Methods - private static string convertToString (Opcode opcode, byte[] rawData) + private static string convertToString (byte[] rawData, Opcode opcode) { - return rawData.LongLength == 0 - ? String.Empty - : opcode == Opcode.Text - ? Encoding.UTF8.GetString (rawData) - : opcode.ToString (); + if (opcode == Opcode.Binary) + return BitConverter.ToString (rawData); + + try { + return Encoding.UTF8.GetString (rawData); + } + catch { + return null; + } } #endregion