Example #1
0
        public async Task StartRecvListener(NBSTopic topic, Func <SubscribeResponse, T> func)
        {
            SubscribeRequest request = new SubscribeRequest {
                Topics = topic.ToString()
            };

            Logged("Topic [" + topic.ToString() + "] starting ....");

            try {
                using (var call = pubsubClient.Subscribe(request))
                {
                    while (await call.ResponseStream.MoveNext(CancellationToken.None))
                    {
                        SubscribeResponse response = call.ResponseStream.Current;
                        Console.WriteLine(">>>Recv Message:" + response.From);
                        T t = func(response);
                        if (response.From != selfID)
                        {
                            collection.Add(t);
                        }
                    }
                }
            } catch (Exception e) {
                Logged(e, "subscribe failure.");
            }
        }
Example #2
0
 public bool SendMessage(NBSTopic topic, string msg, Func <T, string, bool, T> func)
 {
     if (String.IsNullOrEmpty(msg))
     {
         return(false);
     }
     try
     {
         PublishRequest request = new PublishRequest {
             Topics = topic.ToString(), Message = msg
         };
         var back = pubsubClient.Publish(request);
         Logged(back.Result);
         T t = func(Self, msg, true);
         collection.Add(t);
         return(true);
     }
     catch (Exception e)
     {
         Logged(e, "Send [" + msg + "] failure.");
         T t = func(Self, msg, false);
         collection.Add(t);
     }
     return(false);
 }