Exemplo n.º 1
0
        private void SubscribeStream(string symbol)
        {
            //if there is already subscribed stream - unsubscribe it and subscribe for new symbol
            _currentTicksStream?.Close();

            _glue.Interop.Subscribe(_ticksEndpoint,
                                    new ClientEventStreamHandler
            {
                // this lambda is invoked when the status of the stream has changed
                EventStreamStatusChanged = (info, status, cookie) => LogMessage($"{info.EventStreamingMethod.Name} to {info.Server} is {status}"),

                // this lambda is invoked when there is data published to the stream
                EventHandler = (info, data, cookie) =>
                               DispatchAction(() =>
                {
                    var isOOB = data.IsCallbackStream;
                    // if isOOB is true the data has been pushed only to this subscriber

                    var subscriptionItem = $"{data.ResultContext.AsString()}";
                    ListViewSubscriptions.Items.Add(subscriptionItem);
                    ListViewSubscriptions.ScrollIntoView(subscriptionItem);
                })
            },
                                    // these are the arguments sent in the subscription request
                                    mib => mib.SetContext(cb => cb.AddValue("reject", false).AddValue("symbol", symbol)),
                                    // additional settings - specify target await timeout
                                    new TargetSettings().WithTargetAwaitTimeout(TimeSpan.FromSeconds(5)),
                                    // stream settings, specifying that we accept 'personal' (out-of-band) stream pushes
                                    new ClientEventStreamSettings {
                AllowCallbacks = true, ReestablishStream = false
            }
                                    )
            .ContinueWith(eventStream =>
            {
                _currentTicksStream = eventStream.Status == TaskStatus.RanToCompletion ? eventStream.Result : null;
            });
        }
Exemplo n.º 2
0
        private void SubscriberHandler(IServerEventStream stream, IEventStreamSubscriber subscriber, IEventStreamBranch branch, object cookie)
        {
            IEventStreamSubscriptionRequest request = subscriber.Subscription;

            // log the subscriber
            LogMessage($"New subscriber {request.Caller} {request.SubscriptionContext.Arguments.AsString()}");

            // push an 'image' to that subscriber which is received *only* by it (last image pattern)

            // this will be received as an OOB data - see the demo subscriber code
            subscriber.Push(cb =>
                            cb.AddValue("SubscribersAsImage", _glue.AGMObjectSerializer.Serialize(stream.GetBranches()
                                                                                                  .SelectMany(b =>
                                                                                                              b.GetSubscribers().Select(sb => sb.Subscription.Caller)))));

            // e.g. keep the subscriptions in a list
            var subscriptionItem = $"{request.Caller.ApplicationName} {request.SubscriptionContext.Arguments.AsString()}";

            DispatchAction(() =>
            {
                ListViewSubscriptions.Items.Add(subscriptionItem);
                ListViewSubscriptions.ScrollIntoView(subscriptionItem);
            });
        }