Refactored MessageEventArgs.cs

This commit is contained in:
sta 2014-08-24 16:01:29 +09:00
parent 4e50200dcc
commit 7eb4e2ec61
2 changed files with 41 additions and 29 deletions

View File

@ -4,7 +4,7 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -35,10 +35,14 @@ namespace WebSocketSharp
/// Contains the event data associated with a <see cref="WebSocket.OnMessage"/> event. /// Contains the event data associated with a <see cref="WebSocket.OnMessage"/> event.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// A <see cref="WebSocket.OnMessage"/> event occurs when the <see cref="WebSocket"/> receives /// <para>
/// a text or binary data frame. /// A <see cref="WebSocket.OnMessage"/> event occurs when the <see cref="WebSocket"/> receives
/// If you want to get the received data, you access the <see cref="MessageEventArgs.Data"/> or /// a text or binary message.
/// <see cref="MessageEventArgs.RawData"/> property. /// </para>
/// <para>
/// If you would like to get the message data, you should access
/// the <see cref="MessageEventArgs.Data"/> or <see cref="MessageEventArgs.RawData"/> property.
/// </para>
/// </remarks> /// </remarks>
public class MessageEventArgs : EventArgs public class MessageEventArgs : EventArgs
{ {
@ -52,21 +56,21 @@ namespace WebSocketSharp
#region Internal Constructors #region Internal Constructors
internal MessageEventArgs (Opcode opcode, byte[] data) internal MessageEventArgs (WebSocketFrame frame)
{ {
if ((ulong) data.LongLength > PayloadData.MaxLength) _opcode = frame.Opcode;
_rawData = frame.PayloadData.ApplicationData;
_data = convertToString (_opcode, _rawData);
}
internal MessageEventArgs (Opcode opcode, byte[] rawData)
{
if ((ulong) rawData.LongLength > PayloadData.MaxLength)
throw new WebSocketException (CloseStatusCode.TooBig); throw new WebSocketException (CloseStatusCode.TooBig);
_opcode = opcode; _opcode = opcode;
_rawData = data; _rawData = rawData;
_data = convertToString (opcode, data); _data = convertToString (opcode, rawData);
}
internal MessageEventArgs (Opcode opcode, PayloadData payload)
{
_opcode = opcode;
_rawData = payload.ApplicationData;
_data = convertToString (opcode, _rawData);
} }
#endregion #endregion
@ -74,10 +78,18 @@ namespace WebSocketSharp
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets the received data as a <see cref="string"/>. /// Gets the message data as a <see cref="string"/>.
/// </summary> /// </summary>
/// <remarks>
/// <para>
/// If the message data is empty, this property returns <see cref="String.Empty"/>.
/// </para>
/// <para>
/// Or if the message is a binary message, this property returns <c>"Binary"</c>.
/// </para>
/// </remarks>
/// <value> /// <value>
/// A <see cref="string"/> that contains the received data. /// A <see cref="string"/> that represents the message data.
/// </value> /// </value>
public string Data { public string Data {
get { get {
@ -86,22 +98,22 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Gets the received data as an array of <see cref="byte"/>. /// Gets the message data as an array of <see cref="byte"/>.
/// </summary> /// </summary>
/// <value> /// <value>
/// An array of <see cref="byte"/> that contains the received data. /// An array of <see cref="byte"/> that represents the message data.
/// </value> /// </value>
public byte [] RawData { public byte[] RawData {
get { get {
return _rawData; return _rawData;
} }
} }
/// <summary> /// <summary>
/// Gets the type of the received data. /// Gets the type of the message.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="Opcode"/> values, indicates the type of the received data. /// <see cref="Opcode.Text"/> or <see cref="Opcode.Binary"/>.
/// </value> /// </value>
public Opcode Type { public Opcode Type {
get { get {
@ -113,12 +125,12 @@ namespace WebSocketSharp
#region Private Methods #region Private Methods
private static string convertToString (Opcode opcode, byte [] data) private static string convertToString (Opcode opcode, byte[] rawData)
{ {
return data.LongLength == 0 return rawData.LongLength == 0
? String.Empty ? String.Empty
: opcode == Opcode.Text : opcode == Opcode.Text
? Encoding.UTF8.GetString (data) ? Encoding.UTF8.GetString (rawData)
: opcode.ToString (); : opcode.ToString ();
} }

View File

@ -922,7 +922,7 @@ namespace WebSocketSharp
var e = frame.IsCompressed var e = frame.IsCompressed
? new MessageEventArgs ( ? new MessageEventArgs (
frame.Opcode, frame.PayloadData.ApplicationData.Decompress (_compression)) frame.Opcode, frame.PayloadData.ApplicationData.Decompress (_compression))
: new MessageEventArgs (frame.Opcode, frame.PayloadData); : new MessageEventArgs (frame);
enqueueToMessageEventQueue (e); enqueueToMessageEventQueue (e);
return true; return true;