using System;
using System.Text;
namespace UnityWebSocket
{
public class MessageEventArgs : EventArgs
{
private byte[] _rawData;
private string _data;
internal MessageEventArgs(Opcode opcode, byte[] rawData)
{
Opcode = opcode;
_rawData = rawData;
}
internal MessageEventArgs(Opcode opcode, string data)
{
Opcode = opcode;
_data = data;
}
///
/// Gets the opcode for the message.
///
///
/// , .
///
internal Opcode Opcode { get; private set; }
///
/// Gets the message data as a .
///
///
/// A that represents the message data if its type is
/// text and if decoding it to a string has successfully done;
/// otherwise, .
///
public string Data
{
get
{
SetData();
return _data;
}
}
///
/// Gets the message data as an array of .
///
///
/// An array of that represents the message data.
///
public byte[] RawData
{
get
{
SetRawData();
return _rawData;
}
}
///
/// Gets a value indicating whether the message type is binary.
///
///
/// true if the message type is binary; otherwise, false.
///
public bool IsBinary
{
get
{
return Opcode == Opcode.Binary;
}
}
///
/// Gets a value indicating whether the message type is text.
///
///
/// true if the message type is text; otherwise, false.
///
public bool IsText
{
get
{
return Opcode == Opcode.Text;
}
}
private void SetData()
{
if (_data != null) return;
if (RawData == null)
{
return;
}
_data = Encoding.UTF8.GetString(RawData);
}
private void SetRawData()
{
if (_rawData != null) return;
if (_data == null)
{
return;
}
_rawData = Encoding.UTF8.GetBytes(_data);
}
}
}