Esempio n. 1
0
    static async Task Async(IRtmClient client)
    {
        RtmPublishReply reply = null;

        try
        {
            var message = new Animal
            {
                Who   = "zebra",
                Where = new float[] { 34.134358f, -118.321506f }
            };

            reply = await client.Publish("animals", message, Ack.Yes);

            Console.WriteLine("Publish confirmed");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Failed to publish: " + ex.Message);
        }

        var observer = new SubscriptionObserver();

        observer.OnEnterSubscribed += (ISubscription sub) =>
                                      Console.WriteLine("Subscribed to: " + sub.SubscriptionId);

        observer.OnLeaveSubscribed += (ISubscription sub) =>
                                      Console.WriteLine("Unsubscribed from: " + sub.SubscriptionId);

        observer.OnSubscriptionData += (ISubscription sub, RtmSubscriptionData data) =>
        {
            foreach (JToken jToken in data.Messages)
            {
                Console.WriteLine("Got message: " + jToken);
            }
        };

        observer.OnSubscribeError += (ISubscription sub, Exception err) =>
                                     Console.WriteLine("Failed to subscribe: " + err.Message);

        observer.OnSubscriptionError += (ISubscription sub, RtmSubscriptionError err) =>
                                        Console.WriteLine("Subscription failed. RTM sent the unsolicited error {0}: {1}", err.Code, err.Reason);

        var cfg = new SubscriptionConfig(SubscriptionModes.Simple, observer)
        {
            Position = reply?.Position
        };

        client.CreateSubscription("animals", cfg);
    }
Esempio n. 2
0
    private static async Task PublishLoop(IRtmClient client)
    {
        // Publish messages every 2 seconds

        var random = new Random();

        while (true)
        {
            try
            {
                var message = new Animal
                {
                    Who   = "zebra",
                    Where = new float[] {
                        34.134358f + (float)random.NextDouble() / 100,
                        -118.321506f + (float)random.NextDouble() / 100
                    }
                };

                // At this point, the client may not yet be connected to Satori RTM.
                // If the client is not connected, the SDK internally queues the publish request and
                // will send it once the client connects
                RtmPublishReply reply = await client.Publish(channel, message, Ack.Yes);

                Console.WriteLine("Animal is published: {0}", message);
            }
            catch (PduException ex)
            {
                Console.WriteLine("Failed to publish. RTM replied with the error {0}: {1}", ex.Error.Code, ex.Error.Reason);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to publish: " + ex.Message);
            }

            await Task.Delay(millisecondsDelay : 2000);
        }
    }
Esempio n. 3
0
    static async Task Async(IRtmClient client)
    {
        try
        {
            var message = new Animal
            {
                Who   = "zebra",
                Where = new float[] { 34.134358f, -118.321506f }
            };

            RtmPublishReply reply = await client.Publish("animals", message, Ack.Yes);

            Console.WriteLine("Publish confirmed");
        }
        catch (PduException ex)
        {
            Console.WriteLine("Failed to publish. RTM replied with the error {0}: {1}", ex.Error.Code, ex.Error.Reason);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Failed to publish: " + ex.Message);
        }
    }