Beispiel #1
0
        /// <summary>
        /// Performs a synchronous connect to the message broker with an optional username and password
        /// for the purposes of authentication.
        /// </summary>
        /// <param name="username">Optionally the username to authenticate as.</param>
        /// <param name="password">Optionally the password to authenticate with.</param>
        public ConnectionState Connect(string username = null, string password = null)
        {
            Log.Debug(m => m("Initiating connection to broker '{0}', port '{1}' using Client Identifier '{2}'",
                             server, port, clientIdentifier));

            if (username != null)
            {
                Log.Info(m => m("Authenticating with username '{0}' and password '{0}'", username, password));
                if (username.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Username length ({0}) exceeds the max recommended in the MQTT spec. ", username.Trim().Length));
                }
                if (password != null && password.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Password length ({0}) exceeds the max recommended in the MQTT spec. ", password.Trim().Length));
                }
            }

            var connectMessage = GetConnectMessage(username, password);

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            publishingManager    = new PublishingManager(connectionHandler);
            subscriptionsManager = new SubscriptionsManager(connectionHandler, publishingManager);
            messageLogger        = new MessageLogger(connectionHandler);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
Beispiel #2
0
        /// <summary>
        /// Performs a synchronous connect to the message broker.
        /// </summary>
        public ConnectionState Connect()
        {
            MqttConnectMessage connectMessage = GetConnectMessage();

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            subscriptionsManager = new SubscriptionsManager(connectionHandler);
            messageLogger        = new MessageLogger(connectionHandler);
            publishingManager    = new PublishingManager(connectionHandler, HandlePublishMessage);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }