/// <summary> /// 获取实例 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <returns></returns> public static UdpHost GetInstence(string ip = "127.0.0.1", int port = 12333) { if (udpHosts == null) { udpHosts = new System.Collections.Specialized.HybridDictionary(); } UdpHost udpHost = null; var key = ip + ":" + port; lock (udpHosts) { if (udpHosts.Contains(key)) { udpHost = udpHosts[key] as UdpHost; } } if (udpHost == null) { udpHost = new UdpHost(); udpHost.IP = ip; udpHost.Port = port; udpHost.Receive(); udpHost.Connection(); lock (udpHosts) { udpHosts.Add(key, udpHost); } } return(udpHost); }
/// <summary> /// 注册监听事件-客户端 /// </summary> /// <param name="value"></param> /// <param name="action"></param> /// <param name="ip"></param> /// <param name="port"></param> public static void UnRegisterUdpClientReceiveMessage(this object value, Action <Message> action, string ip = "127.0.0.1", int port = 12333) { UdpHost tcpHost = UdpHost.GetInstence(ip, port); tcpHost.ReceiveMessage -= action; }