コード例 #1
0
        public static async Task setup_template()
        {
            MqttClientLocal localClient   = new MqttClientLocal();
            var             configuration = new MqttConfiguration()
            {
                Port            = 12393,
                KeepAliveSecs   = 0,
                WaitTimeoutSecs = 5,
                BufferSize      = 128 * 1024,
                AllowWildcardsInTopicFilters = true,
                MaximumQualityOfService      = MqttQualityOfService.AtLeastOnce
            };
            string topicSubscribe = "my/topic";
            string topicPost      = "my/other/topic";
            var    remoteClient   = await MqttClient.CreateAsync("localhost", configuration);

            await remoteClient.ConnectAsync(new MqttClientCredentials(clientId : "Frontend", "front", "frontpass"));

            await remoteClient.SubscribeAsync(topicSubscribe, MqttQualityOfService.AtLeastOnce);

            localClient.Subscribe(remoteClient.MessageStream);
            string payloadToSend = "MY MESSAGE TO BE SENT";
            var    mymessage     = new MqttApplicationMessage(topicPost, Encoding.UTF8.GetBytes(payloadToSend));

            remoteClient.PublishAsync(mymessage, MqttQualityOfService.AtLeastOnce).Wait();
        }
コード例 #2
0
        public async Task <IMqttClient> SetupClient(int port, string basepath, string username, string password, string clientname)
        {
            _basepath   = basepath;
            _username   = username;
            _password   = password;
            _clientName = clientname;

            var configuration = new MqttConfiguration()
            {
                Port            = port,
                KeepAliveSecs   = 60,
                WaitTimeoutSecs = 5,
                BufferSize      = 128 * 1024,
                AllowWildcardsInTopicFilters = true,
                MaximumQualityOfService      = MqttQualityOfService.AtLeastOnce
            };
            var rpc = await MqttClient.CreateAsync(basepath, configuration);

            await rpc.ConnectAsync(new MqttClientCredentials(clientname, username, password));

            client = rpc;
            return(rpc);
        }