MessageEventArgs.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Text;
  3. namespace SocketHttpListener
  4. {
  5. /// <summary>
  6. /// Contains the event data associated with a <see cref="WebSocket.OnMessage"/> event.
  7. /// </summary>
  8. /// <remarks>
  9. /// A <see cref="WebSocket.OnMessage"/> event occurs when the <see cref="WebSocket"/> receives
  10. /// a text or binary data frame.
  11. /// If you want to get the received data, you access the <see cref="MessageEventArgs.Data"/> or
  12. /// <see cref="MessageEventArgs.RawData"/> property.
  13. /// </remarks>
  14. public class MessageEventArgs : EventArgs
  15. {
  16. #region Private Fields
  17. private string _data;
  18. private Opcode _opcode;
  19. private byte[] _rawData;
  20. #endregion
  21. #region Internal Constructors
  22. internal MessageEventArgs (Opcode opcode, byte[] data)
  23. {
  24. _opcode = opcode;
  25. _rawData = data;
  26. _data = convertToString (opcode, data);
  27. }
  28. internal MessageEventArgs (Opcode opcode, PayloadData payload)
  29. {
  30. _opcode = opcode;
  31. _rawData = payload.ApplicationData;
  32. _data = convertToString (opcode, _rawData);
  33. }
  34. #endregion
  35. #region Public Properties
  36. /// <summary>
  37. /// Gets the received data as a <see cref="string"/>.
  38. /// </summary>
  39. /// <value>
  40. /// A <see cref="string"/> that contains the received data.
  41. /// </value>
  42. public string Data {
  43. get {
  44. return _data;
  45. }
  46. }
  47. /// <summary>
  48. /// Gets the received data as an array of <see cref="byte"/>.
  49. /// </summary>
  50. /// <value>
  51. /// An array of <see cref="byte"/> that contains the received data.
  52. /// </value>
  53. public byte [] RawData {
  54. get {
  55. return _rawData;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the type of the received data.
  60. /// </summary>
  61. /// <value>
  62. /// One of the <see cref="Opcode"/> values, indicates the type of the received data.
  63. /// </value>
  64. public Opcode Type {
  65. get {
  66. return _opcode;
  67. }
  68. }
  69. #endregion
  70. #region Private Methods
  71. private static string convertToString (Opcode opcode, byte [] data)
  72. {
  73. return data.Length == 0
  74. ? String.Empty
  75. : opcode == Opcode.Text
  76. ? Encoding.UTF8.GetString (data, 0, data.Length)
  77. : opcode.ToString ();
  78. }
  79. #endregion
  80. }
  81. }