websocket-sharp/websocket-sharp/MessageEventArgs.cs

128 lines
3.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
*
2013-01-15 14:29:05 +08:00
* Copyright (c) 2012-2013 sta.blockhead
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.
*
* 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;
2012-07-31 09:36:52 +08:00
using System.Text;
2010-10-18 21:28:56 +08:00
2013-10-01 13:52:39 +08:00
namespace WebSocketSharp
{
2013-01-11 19:32:38 +08:00
/// <summary>
/// Contains the event data associated with a <see cref="WebSocket.OnMessage"/> event.
/// </summary>
/// <remarks>
2013-11-05 20:42:59 +08:00
/// A <see cref="WebSocket.OnMessage"/> event occurs when the <see cref="WebSocket"/> receives
/// a text or binary data frame.
/// If you want to get the received data, you access the <see cref="MessageEventArgs.Data"/> or
/// <see cref="MessageEventArgs.RawData"/> property.
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;
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
2013-11-05 20:42:59 +08:00
internal MessageEventArgs (Opcode opcode, byte[] data)
{
2013-11-05 20:42:59 +08:00
if ((ulong) data.LongLength > PayloadData.MaxLength)
2013-10-01 13:52:39 +08:00
throw new WebSocketException (CloseStatusCode.TOO_BIG);
2013-05-01 22:37:48 +08:00
2013-05-03 12:29:52 +08:00
_opcode = opcode;
2013-11-05 20:42:59 +08:00
_rawData = data;
_data = convertToString (opcode, data);
}
2013-11-05 20:42:59 +08:00
internal MessageEventArgs (Opcode opcode, PayloadData payload)
2012-07-31 09:36:52 +08:00
{
2013-05-03 12:29:52 +08:00
_opcode = opcode;
2013-11-05 20:42:59 +08:00
_rawData = payload.ApplicationData;
_data = convertToString (opcode, _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>
/// Gets the received data as a <see cref="string"/>.
/// </summary>
/// <value>
2013-03-11 15:28:34 +08:00
/// A <see cref="string"/> that contains the received data.
2013-01-11 19:32:38 +08:00
/// </value>
public string Data {
get {
2013-05-02 11:51:56 +08:00
return _data;
2012-07-31 09:36:52 +08:00
}
}
2013-01-11 19:32:38 +08:00
/// <summary>
/// Gets the received data as an array of <see cref="byte"/>.
/// </summary>
/// <value>
2013-03-11 15:28:34 +08:00
/// An array of <see cref="byte"/> that contains the received data.
2013-01-11 19:32:38 +08:00
/// </value>
2013-10-01 13:52:39 +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>
2013-03-11 15:28:34 +08:00
/// Gets the type of the received data.
2013-01-11 19:32:38 +08:00
/// </summary>
/// <value>
2013-10-01 13:52:39 +08:00
/// One of the <see cref="Opcode"/> values, indicates the type of the received data.
2013-01-11 19:32:38 +08:00
/// </value>
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
2013-11-05 20:42:59 +08:00
#region Private Methods
private static string convertToString (Opcode opcode, byte [] data)
{
return data.LongLength == 0
? String.Empty
: opcode == Opcode.TEXT
? Encoding.UTF8.GetString (data)
: opcode.ToString ();
}
#endregion
2010-10-18 21:28:56 +08:00
}
}