private void initWsProcessor(IApplicationBuilder app) { // 初始化参数 initParam(); // 初始化连接 var options = new WebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(120), ReceiveBufferSize = 4 * 1024, }; app.UseWebSockets(options); app.Use(async(context, next) => { if (context.Request.Path == "/ws/testnet" && context.WebSockets.IsWebSocketRequest) { var ws = await context.WebSockets.AcceptWebSocketAsync(); await NotifyProcessor.initWsProcessor(context, ws, "testnet"); } if (context.Request.Path == "/ws/mainnet" && context.WebSockets.IsWebSocketRequest) { var ws = await context.WebSockets.AcceptWebSocketAsync(); await NotifyProcessor.initWsProcessor(context, ws, "mainnet"); } }); // 启动任务 Task.Run(() => NotifyProcessor.loop()); // 启动心跳 Task.Run(() => NotifyProcessor.ping()); }
public TransactionStatus Notify(ComplexNotification notification, NamedEndpointVisit visit) { Activity activity = null; string transactionId = null; try { NodeVisit nodeVisit; MakeEndpointActivity(visit, ActivityType.Audit, NodeMethod.Notify, out nodeVisit, out activity); if (notification == null) { throw FaultProvider.GetFault(visit.Version, ENExceptionCodeType.E_InvalidParameter, "Empty input notification"); } if (string.IsNullOrEmpty(notification.FlowName)) { throw FaultProvider.GetFault(visit.Version, ENExceptionCodeType.E_InvalidDataflow, "Empty dataflow name"); } bool isFlowProtected; string flowId = FlowManager.GetDataFlowIdByName(notification.FlowName, out isFlowProtected); if (flowId == null) { throw FaultProvider.GetFault(visit.Version, ENExceptionCodeType.E_InvalidDataflow, "Flow \"{0}\" was not found", notification.FlowName); } activity.FlowName = notification.FlowName; if (CollectionUtils.IsNullOrEmpty(notification.Notifications)) { throw FaultProvider.GetFault(visit.Version, ENExceptionCodeType.E_InvalidParameter, "Notifications array is empty"); } if (isFlowProtected) { ValidateUserPermissions(nodeVisit, notification.FlowName, null, NodeMethod.Notify, activity); } activity.AppendFormat("Notify request from {0} by {1}.", visit.IP, nodeVisit.Account.NaasAccount); foreach (Notification notifyElement in notification.Notifications) { activity.AppendFormat("Notify element name {0}, type {1}, status.", notifyElement.Name, notifyElement.Category.ToString(), notifyElement.Status.ToString()); } transactionId = TransactionManager.CreateNotifyTransaction(flowId, string.Empty, nodeVisit.Account.Id, CommonTransactionStatusCode.Received, null, notification, visit.Version, true); activity.TransactionId = transactionId; TransactionStatus rtnTransactionStatus = new TransactionStatus(transactionId, CommonTransactionStatusCode.Received); NotificationManager.DoNotifyNotifications(rtnTransactionStatus, flowId, notification.FlowName, nodeVisit.Account.NaasAccount); NotifyProcessor.Wakeup(); return(rtnTransactionStatus); } catch (Exception ex) { if (transactionId != null) { TransactionManager.SetTransactionStatusNoThrow(transactionId, CommonTransactionStatusCode.Failed, ex.Message, false); } if (activity != null) { activity.Append(ExceptionUtils.ToShortString(ex)); activity.Type = ActivityType.Error; } if (ex is SoapException) { throw; // Throw directly since already SoapException } else { throw FaultProvider.GetFault(visit.Version, ex); } } finally { if (activity != null) { ActivityManager.Log(activity); } } }