星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WSocketClient.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using SuperSocket.ClientEngine;
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using WebSocket4Net;
  6. namespace XHZB.Desktop.WebSocket
  7. {
  8. public class WSocketClient : IDisposable
  9. {
  10. public static NLog.Logger _Logger = NLog.LogManager.GetCurrentClassLogger();
  11. #region 向外传递数据事件
  12. public event Action<string> MessageReceived;
  13. #endregion
  14. WebSocket4Net.WebSocket _webSocket;
  15. /// <summary>
  16. /// 检查重连线程
  17. /// </summary>
  18. Thread _thread;
  19. bool _isRunning = true;
  20. /// <summary>
  21. /// WebSocket连接地址
  22. /// </summary>
  23. public string ServerPath { get; set; }
  24. public WSocketClient(string url)
  25. {
  26. ServerPath = url;
  27. this._webSocket = new WebSocket4Net.WebSocket(url);
  28. this._webSocket.Opened += WebSocket_Opened;
  29. this._webSocket.Error += WebSocket_Error;
  30. this._webSocket.Closed += WebSocket_Closed;
  31. this._webSocket.MessageReceived += WebSocket_MessageReceived;
  32. }
  33. #region "web socket "
  34. /// <summary>
  35. /// 连接方法
  36. /// <returns></returns>
  37. public bool Start()
  38. {
  39. bool result = true;
  40. try
  41. {
  42. this._webSocket.Open();
  43. this._isRunning = true;
  44. this._thread = new Thread(new ThreadStart(CheckConnection));
  45. this._thread.Start();
  46. }
  47. catch (Exception ex)
  48. {
  49. _Logger.Error(ex.ToString());
  50. result = false;
  51. }
  52. return result;
  53. }
  54. /// <summary>
  55. /// 消息收到事件
  56. /// </summary>
  57. /// <param name="sender"></param>
  58. /// <param name="e"></param>
  59. void WebSocket_MessageReceived(object sender, MessageReceivedEventArgs e)
  60. {
  61. _Logger.Info(" Received:" + e.Message);
  62. MessageReceived?.Invoke(e.Message);
  63. }
  64. /// <summary>
  65. /// Socket关闭事件
  66. /// </summary>
  67. /// <param name="sender"></param>
  68. /// <param name="e"></param>
  69. void WebSocket_Closed(object sender, EventArgs e)
  70. {
  71. _Logger.Info("websocket_Closed");
  72. }
  73. /// <summary>
  74. /// Socket报错事件
  75. /// </summary>
  76. /// <param name="sender"></param>
  77. /// <param name="e"></param>
  78. void WebSocket_Error(object sender, ErrorEventArgs e)
  79. {
  80. _Logger.Info("websocket_Error:" + e.Exception.ToString());
  81. }
  82. /// <summary>
  83. /// Socket打开事件
  84. /// </summary>
  85. /// <param name="sender"></param>
  86. /// <param name="e"></param>
  87. void WebSocket_Opened(object sender, EventArgs e)
  88. {
  89. _Logger.Info(" websocket_Opened");
  90. }
  91. /// <summary>
  92. /// 检查重连线程
  93. /// </summary>
  94. private void CheckConnection()
  95. {
  96. do
  97. {
  98. try
  99. {
  100. if (this._webSocket.State != WebSocket4Net.WebSocketState.Open && this._webSocket.State != WebSocket4Net.WebSocketState.Connecting)
  101. {
  102. _Logger.Info(" Reconnect websocket WebSocketState:" + this._webSocket.State);
  103. this._webSocket.Close();
  104. this._webSocket.Open();
  105. Console.WriteLine("正在重连");
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. _Logger.Error(ex.ToString());
  111. }
  112. System.Threading.Thread.Sleep(5000);
  113. } while (this._isRunning);
  114. }
  115. #endregion
  116. /// <summary>
  117. /// 发送消息
  118. /// </summary>
  119. /// <param name="Message"></param>
  120. public void SendMessage(string Message)
  121. {
  122. Task.Factory.StartNew(() =>
  123. {
  124. if (_webSocket != null && _webSocket.State == WebSocket4Net.WebSocketState.Open)
  125. {
  126. this._webSocket.Send(Message);
  127. }
  128. });
  129. }
  130. public void Dispose()
  131. {
  132. this._isRunning = false;
  133. try
  134. {
  135. _thread.Abort();
  136. }
  137. catch
  138. {
  139. }
  140. this._webSocket.Close();
  141. this._webSocket.Dispose();
  142. this._webSocket = null;
  143. }
  144. }
  145. }