using System.Net.WebSockets;
namespace SafeMobileLib.WebsocketClient
{
///
/// Received message, could be Text or Binary
///
public class ResponseMessage
{
private ResponseMessage(byte[] binary, string text, WebSocketMessageType messageType)
{
Binary = binary;
Text = text;
MessageType = messageType;
}
///
/// Received text message (only if type = WebSocketMessageType.Text)
///
public string Text { get; }
///
/// Received text message (only if type = WebSocketMessageType.Binary)
///
public byte[] Binary { get; }
///
/// Current message type (Text or Binary)
///
public WebSocketMessageType MessageType { get; }
///
/// Return string info about the message
///
public override string ToString()
{
if (MessageType == WebSocketMessageType.Text)
{
return Text;
}
return $"Type binary, length: {Binary?.Length}";
}
///
/// Create text response message
///
public static ResponseMessage TextMessage(string data)
{
return new ResponseMessage(null, data, WebSocketMessageType.Text);
}
///
/// Create binary response message
///
public static ResponseMessage BinaryMessage(byte[] data)
{
return new ResponseMessage(data, null, WebSocketMessageType.Binary);
}
}
}