123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using SuperSocket.ClientEngine;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using WebSocket4Net;
-
- namespace XHZB.Desktop.WebSocket
- {
- public class WSocketClient : IDisposable
- {
- public static NLog.Logger _Logger = NLog.LogManager.GetCurrentClassLogger();
-
- #region 向外传递数据事件
- public event Action<string> MessageReceived;
- #endregion
-
- WebSocket4Net.WebSocket _webSocket;
- /// <summary>
- /// 检查重连线程
- /// </summary>
- Thread _thread;
- bool _isRunning = true;
- /// <summary>
- /// WebSocket连接地址
- /// </summary>
- public string ServerPath { get; set; }
-
- public WSocketClient(string url)
- {
- ServerPath = url;
- this._webSocket = new WebSocket4Net.WebSocket(url);
- this._webSocket.Opened += WebSocket_Opened;
- this._webSocket.Error += WebSocket_Error;
- this._webSocket.Closed += WebSocket_Closed;
- this._webSocket.MessageReceived += WebSocket_MessageReceived;
- }
-
- #region "web socket "
- /// <summary>
- /// 连接方法
- /// <returns></returns>
- public bool Start()
- {
- bool result = true;
- try
- {
- this._webSocket.Open();
-
- this._isRunning = true;
- this._thread = new Thread(new ThreadStart(CheckConnection));
- this._thread.Start();
- }
- catch (Exception ex)
- {
- _Logger.Error(ex.ToString());
- result = false;
- }
- return result;
- }
- /// <summary>
- /// 消息收到事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void WebSocket_MessageReceived(object sender, MessageReceivedEventArgs e)
- {
- _Logger.Info(" Received:" + e.Message);
- MessageReceived?.Invoke(e.Message);
-
-
-
-
-
-
-
-
- }
- /// <summary>
- /// Socket关闭事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void WebSocket_Closed(object sender, EventArgs e)
- {
- _Logger.Info("websocket_Closed");
- }
- /// <summary>
- /// Socket报错事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void WebSocket_Error(object sender, ErrorEventArgs e)
- {
- _Logger.Info("websocket_Error:" + e.Exception.ToString());
- }
- /// <summary>
- /// Socket打开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void WebSocket_Opened(object sender, EventArgs e)
- {
- _Logger.Info(" websocket_Opened");
- }
- /// <summary>
- /// 检查重连线程
- /// </summary>
- private void CheckConnection()
- {
- do
- {
- try
- {
- if (this._webSocket.State != WebSocket4Net.WebSocketState.Open && this._webSocket.State != WebSocket4Net.WebSocketState.Connecting)
- {
- _Logger.Info(" Reconnect websocket WebSocketState:" + this._webSocket.State);
- this._webSocket.Close();
- this._webSocket.Open();
- Console.WriteLine("正在重连");
- }
- }
- catch (Exception ex)
- {
- _Logger.Error(ex.ToString());
- }
- System.Threading.Thread.Sleep(5000);
- } while (this._isRunning);
- }
- #endregion
-
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="Message"></param>
- public void SendMessage(string Message)
- {
- Task.Factory.StartNew(() =>
- {
- if (_webSocket != null && _webSocket.State == WebSocket4Net.WebSocketState.Open)
- {
- this._webSocket.Send(Message);
- }
- });
- }
-
- public void Dispose()
- {
- this._isRunning = false;
- try
- {
- _thread.Abort();
- }
- catch
- {
-
- }
- this._webSocket.Close();
- this._webSocket.Dispose();
- this._webSocket = null;
- }
- }
- }
|