using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Web; using SuperWebSocket; using HZQR.Common; namespace TestDemo.Helper { public class WebSocketDemo { } public static class WebSocketManager { static Dictionary sessionDics = new Dictionary(); static WebSocketServer webSocketServer; public static void Init() { var ip = "172.16.155.172"; //GetLocalIP(); webSocketServer = new WebSocketServer(); var isSetup = webSocketServer.Setup(ip, 7119); LogUtil.WriteLog("WebSocketManager: 服务器端启动" + (isSetup ? "成功" : "失败")); if (!isSetup) return; webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected; webSocketServer.SessionClosed += WebSocketServer_SessionClosed; webSocketServer.NewMessageReceived += WebSocketServer_NewMessageReceived; var isStart = webSocketServer.Start(); LogUtil.WriteLog("WebSocketManager: 服务器端侦听" + (isStart ? "成功" : "失败")); } /// /// 接收消息 /// /// /// private static void WebSocketServer_NewMessageReceived(WebSocketSession session, string value) { LogUtil.WriteLog("WebSocketManager: 接收消息 \r\n" + value); if (!string.IsNullOrWhiteSpace(value)) { if (!sessionDics.ContainsKey(value)) sessionDics.Add(value, session); else sessionDics[value] = session; } SendMsgToRemotePoint("888888042002", DateTime.Now.ToString()); } /// /// 下线 /// /// /// private static void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) { LogUtil.WriteLog(string.Format("WebSocketManager:{0} {1}下线", value, session.RemoteEndPoint)); } /// /// 上线 /// /// private static void WebSocketServer_NewSessionConnected(WebSocketSession session) { LogUtil.WriteLog(string.Format("WebSocketManager:{0}上线", session.RemoteEndPoint)); session.Send(DateTime.Now.ToString()); } /// /// 发送消息到客户端 /// /// /// public static void SendMsgToRemotePoint(string value, string msg) { if (sessionDics.ContainsKey(value)) { var webSocketSession = sessionDics[value]; if (webSocketSession != null) { webSocketSession.Send(msg); } } } /// /// 获取本地IP等信息 /// /// public static string GetLocalIP() { //本机IP地址 string strLocalIP = ""; //得到计算机名 string strPcName = Dns.GetHostName(); //得到本机IP地址数组 IPHostEntry ipEntry = Dns.GetHostEntry(strPcName); //遍历数组 foreach (var IPadd in ipEntry.AddressList) { //判断当前字符串是否为正确IP地址 if (IsRightIP(IPadd.ToString())) { //得到本地IP地址 strLocalIP = IPadd.ToString(); //结束循环 break; } } //返回本地IP地址 return strLocalIP; } /// /// 判断是否为正确的IP地址 /// /// 需要判断的字符串 /// true = 是 false = 否 private static bool IsRightIP(string strIPadd) { //利用正则表达式判断字符串是否符合IPv4格式 if (Regex.IsMatch(strIPadd, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")) { //根据小数点分拆字符串 string[] ips = strIPadd.Split('.'); if (ips.Length == 4 || ips.Length == 6) { //如果符合IPv4规则 if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256) //正确 return true; //如果不符合 else //错误 return false; } else //错误 return false; } else //错误 return false; } } }