Exemplo n.º 1
0
        /// <summary>
        /// Connect to broker
        /// </summary>
        /// <param name="clientId">Client identifier</param>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <param name="willRetain">Will retain flag</param>
        /// <param name="willQosLevel">Will QOS level</param>
        /// <param name="willFlag">Will flag</param>
        /// <param name="willTopic">Will topic</param>
        /// <param name="willMessage">Will message</param>
        /// <param name="cleanSession">Clean sessione flag</param>
        /// <param name="keepAlivePeriod">Keep alive period</param>
        /// <returns>Return code of CONNACK message from broker</returns>
        public byte Connect(string clientId,
                            string username        = null,
                            string password        = null,
                            bool willRetain        = false,
                            byte willQosLevel      = MqttMsgConnect.QOS_LEVEL_AT_LEAST_ONCE,
                            bool willFlag          = false,
                            string willTopic       = null,
                            string willMessage     = null,
                            bool cleanSession      = true,
                            ushort keepAlivePeriod = MqttMsgConnect.KEEP_ALIVE_PERIOD_DEFAULT)
        {
            // create CONNECT message
            MqttMsgConnect connect = new MqttMsgConnect(clientId,
                                                        username,
                                                        password,
                                                        willRetain,
                                                        willQosLevel,
                                                        willFlag,
                                                        willTopic,
                                                        willMessage,
                                                        cleanSession,
                                                        keepAlivePeriod);

            try
            {
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                // try connection to the broker
                this.socket.Connect(new IPEndPoint(this.brokerIpAddress, this.brokerPort));
            }
            catch
            {
                throw new MqttConnectionException();
            }

            this.lastSend  = 0;
            this.isRunning = true;
            // start thread for receiving messages from broker
            this.receiveThread = new Thread(this.ReceiveThread);
            this.receiveThread.Start();

            MqttMsgConnack connack = (MqttMsgConnack)this.SendReceive(connect.GetBytes());

            // if connection accepted, start keep alive timer
            if (connack.ReturnCode == MqttMsgConnack.CONN_ACCEPTED)
            {
                this.IsConnected = true;

                this.keepAlivePeriod = keepAlivePeriod * 1000; // convert in ms

                // start thread for sending keep alive message to the broker
                this.keepAliveThread = new Thread(this.KeepAliveThread);
                this.keepAliveThread.Start();
            }
            return(connack.ReturnCode);
        }