MessageEventArgs.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #region License
  2. /*
  3. * MessageEventArgs.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2022 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. namespace WebSocketSharp
  30. {
  31. /// <summary>
  32. /// Represents the event data for the <see cref="WebSocket.OnMessage"/> event.
  33. /// </summary>
  34. /// <remarks>
  35. /// <para>
  36. /// The message event occurs when the <see cref="WebSocket"/> interface
  37. /// receives a message or a ping if the <see cref="WebSocket.EmitOnPing"/>
  38. /// property is set to <c>true</c>.
  39. /// </para>
  40. /// <para>
  41. /// If you would like to get the message data, you should access
  42. /// the <see cref="Data"/> or <see cref="RawData"/> property.
  43. /// </para>
  44. /// </remarks>
  45. public class MessageEventArgs : EventArgs
  46. {
  47. #region Private Fields
  48. private string _data;
  49. private bool _dataSet;
  50. private Opcode _opcode;
  51. private byte[] _rawData;
  52. #endregion
  53. #region Internal Constructors
  54. internal MessageEventArgs (WebSocketFrame frame)
  55. {
  56. _opcode = frame.Opcode;
  57. _rawData = frame.PayloadData.ApplicationData;
  58. }
  59. internal MessageEventArgs (Opcode opcode, byte[] rawData)
  60. {
  61. if ((ulong) rawData.LongLength > PayloadData.MaxLength)
  62. throw new WebSocketException (CloseStatusCode.TooBig);
  63. _opcode = opcode;
  64. _rawData = rawData;
  65. }
  66. #endregion
  67. #region Internal Properties
  68. /// <summary>
  69. /// Gets the opcode for the message.
  70. /// </summary>
  71. /// <value>
  72. /// <see cref="Opcode.Text"/>, <see cref="Opcode.Binary"/>,
  73. /// or <see cref="Opcode.Ping"/>.
  74. /// </value>
  75. internal Opcode Opcode {
  76. get {
  77. return _opcode;
  78. }
  79. }
  80. #endregion
  81. #region Public Properties
  82. /// <summary>
  83. /// Gets the message data as a <see cref="string"/>.
  84. /// </summary>
  85. /// <value>
  86. /// <para>
  87. /// A <see cref="string"/> that represents the message data
  88. /// if the message type is text or ping.
  89. /// </para>
  90. /// <para>
  91. /// <see langword="null"/> if the message type is binary or
  92. /// the message data could not be UTF-8-decoded.
  93. /// </para>
  94. /// </value>
  95. public string Data {
  96. get {
  97. setData ();
  98. return _data;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets a value indicating whether the message type is binary.
  103. /// </summary>
  104. /// <value>
  105. /// <c>true</c> if the message type is binary; otherwise, <c>false</c>.
  106. /// </value>
  107. public bool IsBinary {
  108. get {
  109. return _opcode == Opcode.Binary;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets a value indicating whether the message type is ping.
  114. /// </summary>
  115. /// <value>
  116. /// <c>true</c> if the message type is ping; otherwise, <c>false</c>.
  117. /// </value>
  118. public bool IsPing {
  119. get {
  120. return _opcode == Opcode.Ping;
  121. }
  122. }
  123. /// <summary>
  124. /// Gets a value indicating whether the message type is text.
  125. /// </summary>
  126. /// <value>
  127. /// <c>true</c> if the message type is text; otherwise, <c>false</c>.
  128. /// </value>
  129. public bool IsText {
  130. get {
  131. return _opcode == Opcode.Text;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the message data as an array of <see cref="byte"/>.
  136. /// </summary>
  137. /// <value>
  138. /// An array of <see cref="byte"/> that represents the message data.
  139. /// </value>
  140. public byte[] RawData {
  141. get {
  142. setData ();
  143. return _rawData;
  144. }
  145. }
  146. #endregion
  147. #region Private Methods
  148. private void setData ()
  149. {
  150. if (_dataSet)
  151. return;
  152. if (_opcode == Opcode.Binary) {
  153. _dataSet = true;
  154. return;
  155. }
  156. string data;
  157. if (_rawData.TryGetUTF8DecodedString (out data))
  158. _data = data;
  159. _dataSet = true;
  160. }
  161. #endregion
  162. }
  163. }