示例#1
0
        /// <summary>
        /// 断开服务端
        /// </summary>
        public void Close()
        {
            try
            {
                if (_mqtt != null)
                {
                    string er = string.Empty;
                    _mqtt.OnMessageArgs.OnEvent -= new COnEvent <CMQTT.CMessageArgs> .OnEventHandler(OnMessageRecv);

                    UnSubscribeTopics(_subscribeTopics, out er);
                    _mqtt.Close();
                    _mqtt = null;
                }
            }
            catch (Exception)
            {
            }
        }
示例#2
0
        /// <summary>
        /// 连接服务端
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="er"></param>
        /// <returns></returns>
        public bool Connect(string ip, int port, out string er)
        {
            er = string.Empty;

            try
            {
                if (_mqtt != null)
                {
                    _mqtt.OnMessageArgs.OnEvent -= new COnEvent <CMQTT.CMessageArgs> .OnEventHandler(OnMessageRecv);

                    _mqtt.Close();
                    _mqtt = null;
                }

                _mqtt = new CMQTT(_idNo, _name);

                if (!_mqtt.Connect(ip, port, out er))
                {
                    _mqtt = null;
                    return(false);
                }

                if (!SubscribeTopics(_subscribeTopics, out er))
                {
                    _mqtt.Close();
                    _mqtt = null;
                    return(false);
                }

                _mqtt.OnMessageArgs.OnEvent += new COnEvent <CMQTT.CMessageArgs> .OnEventHandler(OnMessageRecv);

                return(true);
            }
            catch (Exception ex)
            {
                er = ex.ToString();
                return(false);
            }
        }