public class WebSocketClientHelper { public delegate void ActionEventHandler(); public delegate void MessageEventHandler(string message); public delegate void ErrorEventHandler(Exception ex); ///
/// 客户端打开连接时调用 ///
public event ActionEventHandler OnOpen; ///
/// 客户端关闭连接时调用 ///
public event ActionEventHandler OnClose; ///
/// 收到客户端信息时调用 ///
public event MessageEventHandler OnMessage; ///
/// 执行错误时调用 ///
public event ErrorEventHandler OnError; ///
/// 服务 ///
private WebSocket client; ///
/// 服务URL ///
public string ServerUtl { private set; get; } ///
/// 是否重连 ///
public bool IsReConnection { set; get; } ///
/// 重连间隔(毫秒) ///
public int ReConnectionTime { set; get; } ? public WebSocketClientHelper(string serverUtl) { this.ServerUtl = serverUtl; client = new WebSocket(this.ServerUtl); client.OnOpen += new EventHandler(client_OnOpen); client.OnClose += new EventHandler
(client_OnClose); client.OnMessage += new EventHandler(client_OnMessage); client.OnError += new EventHandler(client_OnError); } ? public WebSocketClientHelper(string serverUtl, bool isReConnection, int reConnectionTime = 1000) : this(serverUtl) { this.IsReConnection = isReConnection; this.ReConnectionTime = reConnectionTime; } ? public void client_OnOpen(object sender, EventArgs e) { try { Console.WriteLine("Open!"); if (OnOpen != null) OnOpen(); } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); else throw ex; } } ? public void client_OnClose(object sender, CloseEventArgs e) { try { Console.WriteLine("Close!"); if (OnClose != null) OnClose(); //掉线重连 if (IsReConnection) { Thread.Sleep(ReConnectionTime); Start(); } } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); else throw ex; } } ? public void client_OnMessage(object sender, MessageEventArgs e) { try { Console.WriteLine("socket收到信息:" + e.Data); if (OnMessage != null) OnMessage(e.Data); } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); //else // throw ex; } } ? public void client_OnError(object sender, ErrorEventArgs e) { if (OnError != null) OnError(e.Exception); //记录日志 //掉线重连 if (IsReConnection) { Thread.Sleep(ReConnectionTime); Start(); } } /// /// 启动服务 /// public void Start() { try { client.ConnectAsync(); } catch (Exception ex) { //日志 if (OnError != null) OnError(ex); else throw ex; } } /// /// 关闭服务 /// public void Close() { try { IsReConnection = false; client.CloseAsync(); } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); else throw ex; } } /// /// 发送信息 /// public void Send(string message) { try { client.Send(message); } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); else throw ex; } } /// /// 发送信息 /// public void Send(byte[] message) { try { client.Send(message); } catch (Exception ex) { //记录日志 if (OnError != null) OnError(ex); else throw ex; } } } 服务端帮助类 /// /// WebSocket服务辅助类 作者韩永健 /// public class WebSocketServerHelper { /// /// 客户端信息 /// public class ClientData { /// /// IP /// public string IP { get; set; } /// /// 端口号 /// public int Port { get; set; } } public delegate void ActionEventHandler(string ip, int port); public delegate void MessageEventHandler(string ip, int port, string message); public delegate void BinaryEventHandler(string ip, int port, byte[] message); public delegate void ErrorEventHandler(string ip, int port, Exception ex); /// /// 客户端打开连接时调用 /// public event ActionEventHandler OnOpen; /// /// 客户端关闭连接时调用 /// public event ActionEventHandler OnClose; /// /// 收到客户端信息时调用 /// public event MessageEventHandler OnMessage; /// /// 收到客户端信息时调用 /// public event BinaryEventHandler OnBinary; /// /// 执行错误时调用 /// public event ErrorEventHandler OnError; /// /// 服务 /// private Fleck.WebSocketServer server; /// /// socket列表 /// private Dictionary socketList = new Dictionary(); private List clientList = new List(); /// /// 客户端ip列表 /// public List ClientList { get { return clientList; } } /// /// 服务URL /// public string ServerUtl { private set; get; } ? public WebSocketServerHelper(string serverUtl) { this.ServerUtl = serverUtl; server = new Fleck.WebSocketServer(this.ServerUtl); } /// /// 启动服务 /// public void Start() { server.Start(socket => { try { socket.OnOpen = () => SocketOpen(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort); socket.OnClose = () => SocketClose(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort); socket.OnMessage = message => SocketMessage(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, message); socket.OnBinary = message => SocketBinary(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, message); socket.OnError = ex => SocketError(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, ex); socketList.Add(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort, socket); ClientList.Add(new ClientData() { IP = socket.ConnectionInfo.ClientIpAddress, Port = socket.ConnectionInfo.ClientPort }); } catch (Exception ex) { } }); } ? /// /// Socket错误 /// private void SocketError(string ip, int port, Exception ex) { Console.WriteLine("Error!" + ip + ":" + port + " " + ex); socketList.Remove(ip + ":" + port); ClientList.RemoveAll(m => m.IP == ip && m.Port == port); if (OnError != null) OnError(ip, port, ex); //else // throw ex; } ? /// /// Socket打开连接 /// private void SocketOpen(string ip, int port) { try { Console.WriteLine("Open!" + ip + ":" + port); if (OnOpen != null) OnOpen(ip, port); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } ? /// /// Socket关闭连接 /// private void SocketClose(string ip, int port) { try { Console.WriteLine("Close!" + ip + ":" + port); socketList.Remove(ip + ":" + port); ClientList.RemoveAll(m => m.IP == ip && m.Port == port); if (OnClose != null) OnClose(ip, port); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } /// /// 接收到的信息 /// private void SocketBinary(string ip, int port, byte[] message) { try { Console.WriteLine("socket收到信息:byte[]"); if (OnBinary != null) OnBinary(ip, port, message); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } /// /// 接收到的信息 /// private void SocketMessage(string ip, int port, string message) { try { Console.WriteLine("socket收到信息:" + message + " " + ip + ":" + port); if (OnMessage != null) OnMessage(ip, port, message); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } /// /// 发送信息(单客户端) /// public void Send(string ip, int port, string message) { try { socketList[ip + ":" + port].Send(message); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } /// /// 发送信息(单客户端) /// public void Send(string ip, int port, byte[] message) { try { socketList[ip + ":" + port].Send(message); } catch (Exception ex) { if (OnError != null) OnError(ip, port, ex); else throw ex; } } /// /// 发送信息(所有客户端) /// public void SendAll(string message) { for (int i = 0; i < socketList.Count; i++) { var item = socketList.ElementAt(i); try { item.Value.Send(message); } catch (Exception ex) { if (OnError != null) OnError(item.Value.ConnectionInfo.ClientIpAddress, item.Value.ConnectionInfo.ClientPort, ex); else throw ex; } } } /// /// 发送信息(所有客户端) /// public void SendAll(byte[] message) { for (int i = 0; i < socketList.Count; i++) { var item = socketList.ElementAt(i); try { item.Value.Send(message); } catch (Exception ex) { if (OnError != null) OnError(item.Value.ConnectionInfo.ClientIpAddress, item.Value.ConnectionInfo.ClientPort, ex); else throw ex; } } } }