Vue project often ignored tips Rainbow

2023-01-02   ES  

mqtt first use

The

project involves TCP communication, and the encapsulated MQTT package is implemented under other people’s suggestions.

In the VS NUGET package management, you can find a variety of packets of MQTT. I use version 2.8.2 Mqttnet here
2.8.2版本的MQTTnet
Directly apply code

public async void mqttclient () 
     {{ 
         if (! MQTTISCONNECTED) 
         {{ 
             try 
             {{ 
            // Configuration 
                 Var options = new mqttclientoptions () {clientid = guid.newguid (). Tostring ("d")}; 
                 options.ChannelOptions = New MQTTClienttcpopTIONS () 
                 {{ 
                     Server = "***. ***. ***. ***", 
                     Port = ***** 
                 }; 
                 options.credentials = New MQTTClientCREDENTIALS () 
                 {{ 
                     Username = "admin", 
                     Password = "password" 
                 }; 
                
                 options.CleAnsulation = TRUE; 
                 options.keepaliveperiod = Timespan.fromseconds (100.5); 
                 options.keepaliveSendinterval = Timespan.fromseconds (20000); 

                 if (null! = _mqttclient) 
                 {{ 
                     await_mqttclient.disconnectasync (); 
                     _mqttclient = null; 
                 } 
                 _mqttclient = new mqttfactory (). CreatemqttClient (); 

                 _mqttclient.applicationMessageRecated += (Sender, ARGS) => 
                 {{ 
                 | Retain: {args.applicationmessage.retain} "); 
                 }; 

                 _mqttclient.connect += (Sender, ARGS) => 
                 {{ 
                     Debug.log ($ "client is connected: issessionPreres: {args.issessionPreSent}");); 
                 }; 

                 _mqttclient.disconnect += (Sender, ARGS) => 
                 {{ 
                     Debug.log ($ "Client is disconnected clientwasconnected: {args.clientWasconnect}"); 
                 }; 

                 await_mqttclient.connectasync (options); 
                 mqttisconnect = true; 

                 try 
                 {{ 
                     await task.factory.startnew (async () => 
                     {{ 
                         await_mqttclient.subscribeasync ( 
                             New list <TopicFilter> 
                             {{ 
                             New TopicFilter ( 
                                 "test", // Subscribe theme 
                                 (MQTTQUALITYOFSERVILEVEL) 
                                      Enum.parse (Typeof (MQTTQUALITYOFSERVILEVEL), "0")) 
                             }); 
                     }); 
                 } 
                 catch (Exception EX) 
                 {{ 
                     Debug.log (ex.MESSAGE); 
                 } 
             } 
             catch (Exception EX) 
             {{ 
                 Debug.log (ex.MESSAGE); 
             } 
         } 
     } 

     Public async void clientstop () 
     {{ 
         if (MQTTISCONNECTED) 
         {{ 
             if (null! = _mqttclient && _mqttclient.isconnect) 
             {{ 
                 await_mqttclient.disconnectasync (); 
                 _mqttclient.dispose (); 
                 _mqttclient = null; 
                 mqttisconnect = false; 
 Debug.log ("Disconnected"); 
             } 
         } 
     }

source

Related Posts

Writing the program of the order N. Requirement: Write a function to achieve a grade of a given value of N. The main function is responsible for determining the value of N, calling a custom function, and outputting the calculation result. Write a step -by -line function of N and call

CSP-S 2019 preliminary knowledge point summary computer cultural basis

Cobbler Multi -System Automation Installation

Linux/CentOS system hangs a new hard disk simple tutorial (detailed)

Vue project often ignored tips Rainbow

Random Posts

Oracle’s large data volume migration, and inlet import samples (fetch … bulk collect) and Forall combined

Front -end HTML framework (frameset) usage detailed analysis

HDU4405 (Probability DP seeks expectations)

Android fast implementation file download (only 4 lines of code) laer

docker set up registry (built with the new version of Docker-CE)