private void notifyTaskSuccess(string userId, StockTaskStatus sts, List <RealTimeData> retList) { using (var scope = _serviceScopeFactory.CreateScope()) { var scopedServices = scope.ServiceProvider; var hubContext = scopedServices.GetRequiredService <IHubContext <NotificationsHub> >(); //hubContext.Clients.All.SendAsync("notification", "test"); //var test = hubContext.Clients.User(userId); //能够找到user,但是客户端接收不到 hubContext.Clients.User(userId).SendAsync( "notification", new Notification <TaskSuccessPayload> { NotificationType = NotificationType.TaskSuccess, Payload = new TaskSuccessPayload { Message = "task success", Result = retList, } } ); } }
protected void StockTaskStart() { string userId = _userId; //if (await this.isFetchingRealTimeData()) //{ // throw new Exception("服务器后台正在获取实时数据,请等会再尝试当前操作"); //} StockTaskStatus oldStatus; if (_stockTaskStatusDic.TryGetValue(userId, out oldStatus)) { int duration = _configuration.GetValue <int>("MaximumTaskDuration"); if (DateTime.Now.Subtract(oldStatus.StartTime).TotalSeconds > duration) { //有任务超时了,但是没有被清除 _stockTaskStatusDic.TryRemove(userId, out oldStatus); } } var tempSts = new StockTaskStatus() { Id = Guid.NewGuid(), StartTime = DateTime.Now, }; if (_stockTaskStatusDic.TryAdd(userId, tempSts) == false) { //throw new Exception("has another task running"); throw new Exception("任务失败,因为当前用户已经有一个任务在服务端上执行"); } else { //成功了才能更新_taskStatus _taskStatus = tempSts; notifyTaskStart(userId, _taskStatus); } System.Diagnostics.Debug.WriteLine($"_stock task is starting , add status {_taskStatus} "); }
private void notifyTaskFail(string userId, StockTaskStatus sts, string message, List <RealTimeData> retList) { using (var scope = _serviceScopeFactory.CreateScope()) { var scopedServices = scope.ServiceProvider; var hubContext = scopedServices.GetRequiredService <IHubContext <NotificationsHub> >(); hubContext.Clients.User(userId).SendAsync( "notification", new Notification <TaskFailPayload> { NotificationType = NotificationType.TaskFail, Payload = new TaskFailPayload { Message = message, ResultList = retList, } } ); } }
private void notifyTaskProgress(string userId, StockTaskStatus sts) { using (var scope = _serviceScopeFactory.CreateScope()) { var scopedServices = scope.ServiceProvider; var hubContext = scopedServices.GetRequiredService <IHubContext <NotificationsHub> >(); int progress = 100 * _progressedCnt / _allItemsCnt; hubContext.Clients.User(userId).SendAsync( "notification", new Notification <TaskProgressPayload> { NotificationType = NotificationType.TaskProgress, Payload = new TaskProgressPayload { Progress = progress, } } ); } }
private void notifyTaskStart(string userId, StockTaskStatus sts) { using (var scope = _serviceScopeFactory.CreateScope()) { var scopedServices = scope.ServiceProvider; var hubContext = scopedServices.GetRequiredService <IHubContext <NotificationsHub> >(); hubContext.Clients.User(userId).SendAsync( "notification", new Notification <TaskStartPayload> { NotificationType = NotificationType.TaskStart, Payload = new TaskStartPayload { Status = sts, } } ); } }