void WyslijWiadomosc() { var receivedMessage = false; string message = ""; // create the client var client = new MQTTClient("xxx.xxx.xxx.xxx", 10883); // hook up the MessageReceived event with a handler //client.MessageReceived += (topic, qos, payload) => //{ // MessageBox.Show("Odebrano: " + topic); // receivedMessage = true; //}; // connect to the MQTT server message = Guid.NewGuid().ToString(); client.Connect("Klient1234"); // wait for the connection to complete // add a subscription client.Subscriptions.Add(new Subscription("test")); string User = ""; User = client.BrokerHostName; var data = DateTime.Now; client.Publish("test", data + " Wiadomosc: " + message, QoS.AcknowledgeDelivery, false); status.Content = "Wysłano"; if (client.IsConnected) { client.Disconnect(); } }
public void Disconnect() { if (!client.IsConnected) { client.Disconnect(); } }
/// <summary> /// 断开连接 /// </summary> public void DisConnect() { try { #region 先取消订阅 List <string> topics = new List <string>(); if (Client.Subscriptions.Count > 0) { foreach (Subscription subscription in Client.Subscriptions) { topics.Add(subscription.TopicName); } string errMsg = string.Empty; UnSubscribe(topics, ref errMsg); } #endregion Client.MessageReceived -= OnMessageReceived; Client.ReconnectedMqtt -= OnReconnectedMqtt; Client.Disconnect(); Client.Dispose(); LogHelper.WriteDebug("MQTT连接断开"); } catch (Exception e) { LogHelper.WriteError("MQTT连接断开失败" + e.Message); } }
/// <summary> /// 方法说明:MQTT断开连接 /// 完成时间:2016-04-20 /// </summary> public void DisConnect() { try { if (_client == null) { return; } //先取消主题的订阅 var topics = new List <string>(); if (_client.Subscriptions.Count > 0) { topics.AddRange(_client.Subscriptions.Select(subscription => subscription.TopicName)); var errMsg = string.Empty; UnSubscribe(topics, ref errMsg); } //再取消事件订阅 _client.MessageReceived -= OnMessageReceived; _client.ReconnectedMqtt -= OnReconnectedMqtt; //最后断开断开连接 _client.Disconnect(); //断开连接后,心跳计数需要释放 _heartbeattimer.Close(); _heartbeatnumber = 0; _client.Dispose(); LogHelper.WriteDebug(Resources.SdkMqttDisconnectSuccess); } catch (Exception ex) { LogHelper.WriteError($"{Resources.SdkMqttDisconnectFail}:{ex.Message}"); } }
//Cleans up after MQTT connections. First, it disconnects if it is still connected. Next it unsubsribes all events private static void CleanUpMQTT() { try { // deregister event listeners if (MQTTClient != null) { if (MQTTClient.IsConnected) { MQTTClient.Disconnect(); } MQTTClient.MqttMsgPublishReceived -= Client_MqttMsgPublishReceived; MQTTClient.MqttMsgSubscribed -= Client_MqttMsgSubscribed; MQTTClient.MqttMsgUnsubscribed -= Client_MqttMsgUnsubscribed; MQTTClient.MqttMsgPublished -= Client_MqttMsgPublished; MQTTClient.ConnectionClosed -= MQTTClient_ConnectionClosed; MQTTClient = null; } } catch (Exception e) { //Log.Logger.Error("Error during clean up: " + e); Console.WriteLine("Error during clean up: " + e); } }
public async Task DisconnectFromClient() { MQTTClient client = new MQTTClient(); bool isConnected = await client.Connect(this.IP, this.PORT, this.PASSWORD); Assert.IsTrue(isConnected); isConnected = await client.Disconnect(); Assert.IsFalse(isConnected); }
static void Main(string[] ass) { //// int count = 0; var ClientId = DateTime.Now.Ticks.ToString(); MQTTClient client = new MQTTClient("my.hnlyf.com"); client.Connect(new MQTTConnectInfo() { UserName = "******", Password = "******", ClientId = ClientId }); // client.Subscribe("iot/log/#"); ; System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += (o, e) => { Console.Title = ($"{ClientId}--{ass[0]}:当前每秒:{count}个,总个数:{TotalCount}"); count = 0; }; timer.Start(); while (true) { //Console.WriteLine("请随意输入"); var text = Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(text)) { client.UnSubscribe("iot/log/#");; client.Disconnect(); } PublishDataPackage applicationMessage = new PublishDataPackage() { Topic = $"iot/log/{ass[0]}", Text = text }; if (!client.Publish(applicationMessage)) { Console.WriteLine("断线了?"); Console.ReadLine(); } System.Threading.Interlocked.Increment(ref count); System.Threading.Interlocked.Increment(ref TotalCount); System.Threading.Thread.Sleep(10); } Console.ReadLine(); }
private void OnGUI() { scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(position.height), GUILayout.Width(position.width)); #region Logging. EditorGUILayout.BeginVertical(LayoutSettings.mainBox.style); Logger = new SerializedObject(FindObjectOfType <LoggerObject>().settings); outputPath = Logger.FindProperty("outputPath"); outputFile = Logger.FindProperty("outputFile"); EditorGUILayout.LabelField("Logging", LayoutSettings.sectionLabel); if (EditorApplication.isPlaying) { GUI.enabled = false; } // file/ EditorGUILayout.PropertyField(outputFile, new GUIContent("Session Name: "), false, GUILayout.Width(300)); EditorGUILayout.PropertyField(Logger.FindProperty("sessionId"), new GUIContent("Session Id: "), false, GUILayout.Width(300)); Logger.ApplyModifiedProperties(); EditorGUILayout.EndVertical(); #endregion #region MQTT // MQTT Settings. if (EditorApplication.isPlaying) { GUI.enabled = false; // disable on play. } EditorGUILayout.BeginVertical(LayoutSettings.mainBox.style); EditorGUILayout.LabelField("MQTT", LayoutSettings.sectionLabel); _client.ip = EditorGUILayout.TextField("ip: ", _client.ip, GUILayout.Width(300)); _client.port = int.Parse(EditorGUILayout.TextField("port: ", _client.port.ToString(), GUILayout.Width(300))); if (GUI.changed) { EditorPrefs.SetString("JaneliaVR_MQTT_IP", _client.ip); EditorPrefs.SetInt("JaneliaVR_MQTT_Port", _client.port); } if (GUILayout.Button("Test Connection")) { _client.Connect(true); _client.Disconnect(); } //Messaging settings. msgSettings.isFold = EditorGUILayout.Foldout(msgSettings.isFold, "Messaging"); if (msgSettings.isFold) { EditorGUILayout.BeginVertical(LayoutSettings.subBox.style); bool newSendFrame = EditorGUILayout.Toggle("Send Frame Messages", msgSettings.sendFrameMsg, LayoutSettings.editFieldOp); if (newSendFrame != msgSettings.sendFrameMsg) { msgSettings.sendFrameMsg = newSendFrame; EditorPrefs.SetBool("Gimbl_sendFrameMsg", newSendFrame); } EditorGUILayout.EndVertical(); } //Session settings. sessionSettings.isFold = EditorGUILayout.Foldout(sessionSettings.isFold, "External Control"); if (sessionSettings.isFold) { EditorGUILayout.BeginVertical(LayoutSettings.subBox.style); bool newExternalStart = EditorGUILayout.Toggle("External Start Trigger", sessionSettings.externalStart, LayoutSettings.editFieldOp); if (newExternalStart != sessionSettings.externalStart) { sessionSettings.externalStart = newExternalStart; EditorPrefs.SetBool("Gimbl_externalStart", newExternalStart); } bool newExternalLog = EditorGUILayout.Toggle("External Log Naming", sessionSettings.externalLog, LayoutSettings.editFieldOp); if (newExternalLog != sessionSettings.externalLog) { sessionSettings.externalLog = newExternalLog; EditorPrefs.SetBool("Gimbl_externalLog", newExternalLog); } EditorGUILayout.EndVertical(); } GUI.enabled = true; EditorGUILayout.EndVertical(); #endregion EditorGUILayout.BeginVertical(LayoutSettings.mainBox.style); EditorGUILayout.LabelField("General", LayoutSettings.sectionLabel); #region Teleport. foldBlink = EditorGUILayout.Foldout(foldBlink, "Teleport"); if (foldBlink) { EditorGUILayout.BeginVertical(LayoutSettings.subBox.style); EditorGUILayout.BeginHorizontal(LayoutSettings.editFieldOp); int newDuration = EditorGUILayout.IntField("Dark Duration: ", PlayerPrefs.GetInt("Gimbl_BlinkDuration", 2000)); EditorGUILayout.LabelField("(ms)", GUILayout.Width(50)); if (newDuration != duration) { PlayerPrefs.SetInt("Gimbl_BlinkDuration", newDuration); duration = newDuration; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(LayoutSettings.editFieldOp); int newFadeTime = EditorGUILayout.IntField("Fade Time: ", PlayerPrefs.GetInt("Gimbl_BlinkFadeTime", 3000)); EditorGUILayout.LabelField("(ms)", GUILayout.Width(50)); if (newFadeTime != fadeTime) { PlayerPrefs.SetInt("Gimbl_BlinkFadeTime", newFadeTime); fadeTime = newFadeTime; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } #endregion EditorGUILayout.EndVertical(); #region Export/Import. EditorGUILayout.BeginVertical(LayoutSettings.mainBox.style); EditorGUILayout.LabelField("Setup", LayoutSettings.sectionLabel); if (GUILayout.Button("Export Setup")) { ExportSetup(); } if (GUILayout.Button("Import Setup")) { ImportSetup(); } EditorGUILayout.EndVertical(); #endregion EditorGUILayout.EndScrollView(); }
// Use this to disconnect public void Disconnect() { client.Disconnect(); }