/// <summary> /// Queries the given reportId from an SMS /// </summary> /// <param name="request">prepared request to use for the query</param> public SmsResponse subscribeNotifications(NotificationSubscribeRequest request) { EnsureRequestValid(request); string uri = ServiceBaseUrl + "/outbound/{0}/subscriptions"; uri = string.Format(uri, Uri.EscapeDataString(request.senderAddress)); return(CreateAuthenticatedJsonRequest <SmsResponse>(uri, request).Execute()); }
private void Connect() { channel = new Channel($"{ServiceAddress}:{ServicePort}", ChannelCredentials.Insecure); notificationClient = new NotificationService.NotificationServiceClient(channel); var request = new NotificationSubscribeRequest { Extension = extension }; var response = notificationClient.Subscribe(request); var watcher = new NotificationConnectionWatcher(channel, OnChanalStateChanged); var responseReaderTask = Task.Run(async() => { while (await response.ResponseStream.MoveNext(token)) { FailSince = null; var message = response.ResponseStream.Current; logger.Debug($"extension:{extension} Received:{message}"); OnAppearedMessage(message); } logger.Warn($"Соединение с NotificationService[{extension}] завершено."); }, token).ContinueWith(task => { if (task.IsCanceled || (task.Exception?.InnerException as RpcException)?.StatusCode == StatusCode.Cancelled) { logger.Info($"Соединение с NotificationService[{extension}] отменено."); } else if (task.IsFaulted) { if (FailSince == null) { FailSince = DateTime.Now; } var failedTime = (DateTime.Now - FailSince).Value; if (failedTime.Seconds < 10) { Thread.Sleep(1000); } else if (failedTime.Minutes < 10) { Thread.Sleep(4000); } else { Thread.Sleep(30000); } logger.Error(task.Exception); logger.Info($"Соединение с NotificationService[{extension}] разорвано... Пробуем соединиться."); Connect(); } }) ; }
public override async Task Subscribe(NotificationSubscribeRequest request, IServerStreamWriter <NotificationMessage> responseStream, ServerCallContext context) { var subscription = new Subscription(request.Extension); lock (Subscribers) { Subscribers.Add(subscription); } logger.Debug($"Добавочный {request.Extension} зарегистрировался."); try { var reader = subscription.Queue.Reader; while (!context.CancellationToken.IsCancellationRequested) { var message = await reader.ReadAsync(context.CancellationToken); logger.Debug("Сообщение в очереди"); if (message != null) { await responseStream.WriteAsync(message); } } } catch (Exception e) { logger.Debug(e); throw; } finally { lock (Subscribers) { Subscribers.Remove(subscription); } logger.Debug($"Добавочный {request.Extension} отвалился."); } }
private TelekomJsonWebRequest <SmsResponse> CreateAuthenticatedJsonRequest <ResponseType>(string uri, NotificationSubscribeRequest request) { TelekomJsonWebRequest <SmsResponse> webRequest = CreateAuthenticatedRequest <SmsResponse>(uri, HttpMethod.POST); Dictionary <string, Dictionary <string, object> > jsonRequest = new Dictionary <string, Dictionary <string, object> >(); //--------------------------------------------------------------------------------------------------------------- Dictionary <string, object> val = new Dictionary <string, object>(); Dictionary <string, string> helper = new Dictionary <string, string>(); helper.Add("notifyURL", request.notifyURL); if (request.callbackData != null) { helper.Add("callbackData", request.callbackData); } val.Add("callbackReference", helper); jsonRequest.Add("deliveryReceiptSubscription", val); JsonSerializer serializer = new JsonSerializer(); MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); JsonWriter writer = new JsonTextWriter(sw); serializer.Serialize(writer, jsonRequest); writer.Flush(); sw.Flush(); ms.Position = 0; ms.Flush(); webRequest.SetRawContent(ms, "application/json"); return(webRequest); }