websocket-sharp/websocket-sharp/MessageEventArgs.cs

164 lines
4.5 KiB
C#
Raw Normal View History

2013-04-08 14:11:57 +08:00
#region License
2013-01-11 19:32:38 +08:00
/*
2012-07-31 09:36:52 +08:00
* MessageEventArgs.cs
2010-10-18 21:28:56 +08:00
*
* The MIT License
*
* Copyright (c) 2012-2015 sta.blockhead
2014-08-24 15:01:29 +08:00
*
2010-10-18 21:28:56 +08:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2014-08-24 15:01:29 +08:00
*
2010-10-18 21:28:56 +08:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
using System;
2013-10-01 13:52:39 +08:00
namespace WebSocketSharp
{
2013-01-11 19:32:38 +08:00
/// <summary>
2015-11-12 14:45:57 +08:00
/// Represents the event data for the <see cref="WebSocket.OnMessage"/> event.
2013-01-11 19:32:38 +08:00
/// </summary>
/// <remarks>
2014-08-24 15:01:29 +08:00
/// <para>
/// A <see cref="WebSocket.OnMessage"/> event occurs when the <see cref="WebSocket"/> receives
/// a text or binary message, or a ping if the <see cref="WebSocket.EmitOnPing"/> property is
2015-07-26 16:25:08 +08:00
/// set to <c>true</c>.
2014-08-24 15:01:29 +08:00
/// </para>
/// <para>
2015-07-26 16:25:08 +08:00
/// If you would like to get the message data, you should access the <see cref="Data"/> or
/// <see cref="RawData"/> property.
2014-08-24 15:01:29 +08:00
/// </para>
2013-01-11 19:32:38 +08:00
/// </remarks>
2012-07-31 09:36:52 +08:00
public class MessageEventArgs : EventArgs
2010-10-18 21:28:56 +08:00
{
2013-05-01 22:37:48 +08:00
#region Private Fields
2013-01-11 19:32:38 +08:00
2013-05-02 11:51:56 +08:00
private string _data;
2015-07-20 15:23:40 +08:00
private bool _dataSet;
2013-05-01 22:37:48 +08:00
private Opcode _opcode;
2013-05-02 11:51:56 +08:00
private byte[] _rawData;
2013-01-11 19:32:38 +08:00
#endregion
2012-07-31 09:36:52 +08:00
2013-05-01 22:37:48 +08:00
#region Internal Constructors
2013-01-11 19:32:38 +08:00
2014-08-24 15:01:29 +08:00
internal MessageEventArgs (WebSocketFrame frame)
{
2014-08-24 15:01:29 +08:00
_opcode = frame.Opcode;
_rawData = frame.PayloadData.ApplicationData;
}
2014-08-24 15:01:29 +08:00
internal MessageEventArgs (Opcode opcode, byte[] rawData)
2012-07-31 09:36:52 +08:00
{
2014-08-24 15:01:29 +08:00
if ((ulong) rawData.LongLength > PayloadData.MaxLength)
throw new WebSocketException (CloseStatusCode.TooBig);
2013-05-03 12:29:52 +08:00
_opcode = opcode;
2014-08-24 15:01:29 +08:00
_rawData = rawData;
2012-07-31 09:36:52 +08:00
}
2013-01-11 19:32:38 +08:00
#endregion
2013-05-01 22:37:48 +08:00
#region Public Properties
2012-07-31 09:36:52 +08:00
2013-01-11 19:32:38 +08:00
/// <summary>
2014-08-24 15:01:29 +08:00
/// Gets the message data as a <see cref="string"/>.
2013-01-11 19:32:38 +08:00
/// </summary>
/// <value>
2015-11-12 14:45:57 +08:00
/// A <see cref="string"/> that represents the message data or
/// <see langword="null"/> if the message data cannot be decoded to a string.
2013-01-11 19:32:38 +08:00
/// </value>
public string Data {
get {
2015-07-20 15:23:40 +08:00
if (!_dataSet) {
_data = _opcode != Opcode.Binary
2015-08-22 10:50:19 +08:00
? _rawData.UTF8Decode ()
: BitConverter.ToString (_rawData);
2015-07-20 15:23:40 +08:00
_dataSet = true;
}
2013-05-02 11:51:56 +08:00
return _data;
2012-07-31 09:36:52 +08:00
}
}
/// <summary>
/// Gets a value indicating whether the message type is binary.
/// </summary>
/// <value>
/// <c>true</c> if the message type is binary; otherwise, <c>false</c>.
/// </value>
public bool IsBinary {
get {
return _opcode == Opcode.Binary;
}
}
/// <summary>
/// Gets a value indicating whether the message type is ping.
/// </summary>
/// <value>
/// <c>true</c> if the message type is ping; otherwise, <c>false</c>.
/// </value>
public bool IsPing {
get {
return _opcode == Opcode.Ping;
}
}
/// <summary>
/// Gets a value indicating whether the message type is text.
/// </summary>
/// <value>
/// <c>true</c> if the message type is text; otherwise, <c>false</c>.
/// </value>
public bool IsText {
get {
return _opcode == Opcode.Text;
}
}
2013-01-11 19:32:38 +08:00
/// <summary>
2014-08-24 15:01:29 +08:00
/// Gets the message data as an array of <see cref="byte"/>.
2013-01-11 19:32:38 +08:00
/// </summary>
/// <value>
2014-08-24 15:01:29 +08:00
/// An array of <see cref="byte"/> that represents the message data.
2013-01-11 19:32:38 +08:00
/// </value>
2014-08-24 15:01:29 +08:00
public byte[] RawData {
2013-01-11 19:32:38 +08:00
get {
2013-05-02 11:51:56 +08:00
return _rawData;
2012-07-31 09:36:52 +08:00
}
}
2013-01-11 19:32:38 +08:00
/// <summary>
/// Gets the message type.
2013-01-11 19:32:38 +08:00
/// </summary>
/// <value>
2015-07-26 16:25:08 +08:00
/// <see cref="Opcode.Text"/>, <see cref="Opcode.Binary"/>, or <see cref="Opcode.Ping"/>.
2013-01-11 19:32:38 +08:00
/// </value>
[Obsolete ("This property will be removed. Use any of the Is properties instead.")]
2013-01-11 19:32:38 +08:00
public Opcode Type {
get {
2013-05-01 22:37:48 +08:00
return _opcode;
2013-01-11 19:32:38 +08:00
}
2012-07-31 09:36:52 +08:00
}
2013-01-11 19:32:38 +08:00
#endregion
2010-10-18 21:28:56 +08:00
}
}