Exemplo n.º 1
0
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public static TcpHost GetInstence(string ip = "127.0.0.1", int port = 12333)
        {
            if (tcpHosts == null)
            {
                tcpHosts = new System.Collections.Specialized.HybridDictionary();
            }

            TcpHost tcpHost = null;
            var     key     = ip + ":" + port;

            lock (tcpHosts)
            {
                if (tcpHosts.Contains(key))
                {
                    tcpHost = tcpHosts[key] as TcpHost;
                }
            }

            if (tcpHost == null)
            {
                tcpHost      = new TcpHost();
                tcpHost.IP   = ip;
                tcpHost.Port = port;
                tcpHost.Connection();
                lock (tcpHosts)
                {
                    tcpHosts.Add(key, tcpHost);
                }
            }

            return(tcpHost);
        }
        /// <summary>
        /// 注册监听事件-客户端
        /// </summary>
        /// <param name="value"></param>
        /// <param name="action"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public static void UnRegisterTcpClientReceiveMessage(this object value, Action <Message> action, string ip = "127.0.0.1", int port = 12333)
        {
            TcpHost tcpHost = TcpHost.GetInstence(ip, port);

            tcpHost.ReceiveMessage -= action;
        }