void SendMessage(string strGroupName, string strText) { this.EnableControls(false); List <MessageRecord> messages = new List <MessageRecord>(); MessageRecord record = new MessageRecord(); record.groups = new string [1] { strGroupName }; record.data = strText; messages.Add(record); SetMessageRequest param = new SetMessageRequest("create", "", messages); SetMessageResult result = Program.MainForm.MessageHub.SetMessageAsync(param).Result; if (result.Value == -1) { this.Invoke((Action)(() => MessageBox.Show(this, result.ErrorInfo))); } else { // 调用成功后才把输入的文字清除 this.Invoke((Action)(() => this.textBox_input.Text = "" )); } this.EnableControls(true); }
public async Task <SetMessageResult> TrySetMessageAsync(SetMessageRequest request) { int max = 5; for (int i = 0; ; i++) { try { return(await HubProxy.Invoke <SetMessageResult>( "SetMessage", request).ConfigureAwait(false)); } catch (Exception ex) { if (i < max && ex.InnerException is InvalidOperationException) { Console.WriteLine("*** TrySetMessageAsync InvalidOperationException, retryCount=" + i.ToString()); Thread.Sleep(1000); continue; } else { throw; } } } }
/// <summary> /// 删除消息 /// </summary> /// <param name="records"></param> /// <param name="strGroupName"></param> /// <returns> /// true 成功 /// false 失败 /// </returns> bool DeleteMessage(List <MessageRecord> records) { List <MessageRecord> delete_records = new List <MessageRecord>(); foreach (MessageRecord source in records) { MessageRecord record = new MessageRecord(); // 2016-6-20 jane 不需要传group参数 record.groups = dp2WeiXinService.C_Group_PatronNotity.Split(new char[] { ',' }); record.id = source.id; delete_records.Add(record); } string strError = ""; // CancellationToken cancel_token = new CancellationToken(); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, dp2WeiXinService.C_ConnName_TraceMessage).Result; SetMessageRequest param = new SetMessageRequest("expire", "dontNotifyMe", delete_records);//records);这里应该用delete_records吧,用records好像也没错 CancellationToken cancel_token = new CancellationToken(); SetMessageResult result = connection.SetMessageTaskAsync(param, new TimeSpan(0, 1, 0), cancel_token).Result; if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return(true); ERROR1: dp2WeiXinService.Instance.WriteErrorLog("DeleteMessage() error : " + strError); //this.WriteLog("DeleteMessage() error : " + strError, dp2WeiXinService.C_LogLevel_1); return(false); }
// return: // false 出错 // true 成功 bool SendMessage( MessageConnection connection, string strGroupName, string[] texts) { string strError = ""; List <MessageRecord> records = new List <MessageRecord>(); foreach (string text in texts) { MessageRecord record = new MessageRecord(); record.groups = strGroupName.Split(new char[] { ',' }); record.creator = ""; // 服务器会自己填写 record.data = text; record.format = "text"; record.type = "message"; record.thread = ""; record.expireTime = new DateTime(0); // 表示永远不失效 records.Add(record); } try { SetMessageRequest param = new SetMessageRequest("create", "", records); SetMessageResult result = connection.SetMessageTaskAsync(param, new CancellationToken()).Result; if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } return(true); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: this.Invoke((Action)(() => MessageBox.Show(this, strError))); return(false); }
void DeleteMessage(List <MessageRecord> records, string strGroupName) { List <MessageRecord> delete_records = new List <MessageRecord>(); foreach (MessageRecord source in records) { MessageRecord record = new MessageRecord(); record.groups = strGroupName.Split(new char[] { ',' }); record.id = source.id; delete_records.Add(record); } string strError = ""; // CancellationToken cancel_token = new CancellationToken(); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, "").Result; SetMessageRequest param = new SetMessageRequest("expire", "dontNotifyMe", records); SetMessageResult result = connection.SetMessageTaskAsync(param, new CancellationToken()).Result; if (result.Value == -1) { goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return; ERROR1: WriteErrorLog("DeleteMessage() error : " + strError); }
static int GetLength(SetMessageRequest request) { if (request.Records == null) { return(0); } int length = 0; foreach (MessageRecord record in request.Records) { if (record.data != null) { length += record.data.Length; } } return(length); }
async Task ProcessAndReply(List <MessageRecord> records) { try { List <MessageRecord> new_messages = new List <MessageRecord>(); foreach (var record in records) { var result_text = ProcessCommand(record.data); if (result_text == null) { continue; } WriteInfoLog($"响应文字 '{result_text}'"); new_messages.Add(new MessageRecord { groups = record.groups, data = result_text }); } if (new_messages.Count > 0) { SetMessageRequest param = new SetMessageRequest("create", "dontNotifyMe", new_messages); var result = await _connection.SetMessageAsyncLite(param); if (result.Value == -1) { WriteErrorLog($"reply message error: {result.ErrorInfo}"); } } } catch (Exception ex) { WriteErrorLog($"ProcessAndReply() 出现异常: {ExceptionUtil.GetDebugText(ex)}"); } }
// 对于 .data 超过 chunk_size 的情况可以自动切割为多次发送请求 public async Task <SetMessageResult> SetMessageAsyncLite( SetMessageRequest request) { // 请求结构中如果具备了 TaskID 值,说明调主想自己控制拼接过程,那这里就直接发送出去 if (string.IsNullOrEmpty(request.TaskID) == false) { return(await TrySetMessageAsync(request).ConfigureAwait(false)); } int chunk_size = 4096; int length = GetLength(request); if (length < chunk_size) { return(await TrySetMessageAsync(request)); } SetMessageResult result = null; foreach (MessageRecord record in request.Records) { string taskID = Guid.NewGuid().ToString(); string data = record.data; int send = 0; for (; ;) { SetMessageRequest current_request = new SetMessageRequest(); current_request.TaskID = taskID; current_request.Style = request.Style; current_request.Action = request.Action; current_request.Records = new List <MessageRecord>(); MessageRecord current_record = new MessageRecord(); // TODO: 除了第一次请求外,其它的都只要 .data 成员具备即可 current_record.CopyFrom(record); current_record.data = data.Substring(send, Math.Min(chunk_size, data.Length - send)); current_request.Records.Add(current_record); // 这一次就是最后一次 if (send + current_record.data.Length >= data.Length) { MessageRecord tail_record = new MessageRecord(); tail_record.data = null; // 表示结束 current_request.Records.Add(tail_record); } // TODO: #if NO result = await HubProxy.Invoke <SetMessageResult>( "SetMessage", current_request); #endif result = await TrySetMessageAsync(current_request).ConfigureAwait(false); if (result.Value == -1) { return(result); // 中途出错了 } send += current_record.data.Length; if (send >= data.Length) { break; } } } return(result); // 返回最后一次请求的 result 值 }
void SendMessage(string strGroupName, string strText) { this.EnableControls(false); List<MessageRecord> messages = new List<MessageRecord>(); MessageRecord record = new MessageRecord(); record.groups = new string [1] {strGroupName}; record.data = strText; messages.Add(record); SetMessageRequest param = new SetMessageRequest("create", "", messages); SetMessageResult result = this.MainForm.MessageHub.SetMessageAsync(param).Result; if (result.Value == -1) { this.Invoke((Action)(() => MessageBox.Show(this, result.ErrorInfo))); } else { // 调用成功后才把输入的文字清除 this.Invoke((Action)(() => this.textBox_input.Text = "" )); } this.EnableControls(true); }