public void Close(String username) { Logger.LogDebug("Close Mqtt:" + username); TwainCloudServer tcs = null; if (!map.ContainsKey(username)) { return; } tcs = map[username]; if (null != tcs && tcs.isConnected()) { Task.Run(async() => { await tcs.Disconnect(); }).Wait(); tcs.Dispose(); } else if (tcs.isConnected()) { tcs.Dispose(); } map.Remove(username); }
private async Task <MqttResult> SendMqttMessageAsync(String id, String username, String sendMsg, String cmdId) { TwainCloudServer tcs = null; if (map.ContainsKey(username)) { tcs = map[username]; } if (null == tcs || !tcs.isConnected()) { tcs = new TwainCloudServer(StringUtil.GetTopic_RecieveMsgFromDevice(username)); map[username] = tcs; await tcs.Connect(); } if (cmdId == null || cmdId.Length == 0) { tcs.ClearLastMsg(); } DateTime startSend = DateTime.Now; TimeSpan timeoutSpan = new TimeSpan(0, 0, 30); // 30s if (sendMsg.IndexOf("x-privet-token:\\\"\\\"") > 0) { timeoutSpan = new TimeSpan(0, 0, 5); // 5s } await tcs.Send(StringUtil.GetTopic_SendMsgToDevice(id), sendMsg); MqttResult ret = new MqttResult(); ret.bTimeout = false; while (tcs.GetMessage(cmdId) == null) { if (DateTime.Now - startSend > timeoutSpan) { // timeout ret.bTimeout = true; ret.errorString = "timed out."; break; } await Task.Delay(100); } ret.result = tcs.GetMessage(cmdId); Logger.LogDebug("Get Message:" + ret); return(ret); }