async void Process(string message) { try { if (string.IsNullOrEmpty(message)) { return; } CQResponse response = await GetResponse(message); if (response is EmptyResponse == false) { if (caller != null) { await caller.SendRequestAsync( new ApiCall.Requests.HandleQuickOperationRequest( context: message, operation: response.content )); } else { Log.Warn("WS无法快速响应(caller为null)"); } } } catch (System.Exception e) { Log.Error( $"处理事件时发生未处理的异常{e},错误信息为{e.Message}" ); } }
protected async Task <CQResponse> GetResponse(string message) { CQResponse response = null; foreach (var handler in handlers) { try { response = await handler( JsonConvert.DeserializeObject <CQEvent> (message) ); } catch (Exception e) { Log.Error("处理事件时出现异常:"); Log.Error(e.ToString()); } } return(response); }
public ReverseWSListener(int bind_port, string event_path, string access_token) { event_path = event_path.Trim('/'); server = new WebsocketDaemon.WebsocketServerInstance( bind_port, event_path, access_token, message => { Task.Run(async() => { try { if (string.IsNullOrEmpty(message)) { return; } CQResponse response = await GetResponse(message); if (response is EmptyResponse == false) { if (caller != null) { await caller.SendRequestAsync( new ApiCall.Requests.HandleQuickOperationRequest( context: message, operation: response.content )); } else { Log.Warn("反向WS无法快速响应(caller为null)"); } } } catch (System.Exception e) { Log.Error( $"处理事件时发生未处理的异常{e},错误信息为{e.Message}" ); } }); } ); }
private async Task ProcessContext(HttpListenerContext context) { try { var request = context.Request; if (!request.ContentType.StartsWith( "application/json", StringComparison.Ordinal )) { return; } string message = GetContent(secret, request); if (string.IsNullOrEmpty(message)) { return; } CQResponse response = await GetResponse(message); context.Response.ContentType = "application/json"; if (response != null) { byte[] output = Encoding.UTF8.GetBytes(response.ToString()); context.Response.ContentLength64 = output.Length; await context.Response.OutputStream.WriteAsync( output, 0, output.Length ); } else { context.Response.StatusCode = 204; } } catch (Exception e) { Log.Error($"网络出现未知错误{e}\n{e.Message}"); } }