Ejemplo n.º 1
0
 public void Connect()
 {
     _bayeuxClient.Handshake();
     _bayeuxClient.WaitFor(1000, new[] { BayeuxClient.State.CONNECTED });
     _bayeuxClient.GetChannel(_channel).Subscribe(_listener);
     Logger.Info($"Waiting event from salesforce for the push topic {_channel}");
 }
Ejemplo n.º 2
0
 public void Connect()
 {
     _bayeuxClient.Handshake();
     _bayeuxClient.WaitFor(1000, new[] { BayeuxClient.State.CONNECTED });
     _bayeuxClient.GetChannel(channel).Subscribe(new Listener());
     Console.WriteLine("Waiting event from salesforce for the push topic " + channel.ToString());
 }
        ///<inheritdoc/>
        public void Handshake(int timeout)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("Cannot connect when disposed");
            }

            _logger.LogDebug("Handshaking...");
            _bayeuxClient.Handshake();
            _bayeuxClient.WaitFor(timeout, new[] { BayeuxClient.State.CONNECTED });
            _logger.LogDebug("Connected");
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var longPollingTransport = new LongPollingTransport(null)
            {
                HeaderCollection = new WebHeaderCollection
                {
                    new NameValueCollection
                    {
                        {
                            "Content-Type",
                            "application/json"
                        },
                        {
                            "Authorization",
                            $"Bearer {accessToken}"
                        }
                    },
                },
                CookieCollection = new CookieCollection(),
            };

            var client = new BayeuxClient(url, new List <ClientTransport> {
                longPollingTransport
            });

            // Save the newReplayId in a reliable way. This is basically the last message processed
            // So when your application recovers a crash the next subscription should start from the last replay id processed
            var replayExtension = new ReplayExtension((changedChannel, newReplayId) => Console.WriteLine($"{changedChannel}: {newReplayId}"));

            replayExtension.SetReplayId(channelName, replayId);
            client.AddExtension(replayExtension);

            client.Handshake(new Dictionary <string, object>
            {
                { MessageFields.ReplayField, true }
            });

            var result = client.WaitFor(6000, new List <BayeuxClient.State> {
                BayeuxClient.State.Connected
            });

            // Subscription to channels
            IClientSessionChannel channel = client.GetChannel(channelName);
            var listener = new SimpleListener();

            channel.Subscribe(listener);
            //channel.Unsubscribe(listener);
            //replayExtension.SetReplayId(channelName, 100);
            //channel.Subscribe(listener);

            Thread.Sleep(Timeout.Infinite);
        }
        public void Initialize(StatisticsApi api)
        {
            WebHeaderCollection headers = new WebHeaderCollection();

            foreach (string key in api.Configuration.DefaultHeader.Keys)
            {
                switch (key)
                {
                case "x-api-key":
                case "Authorization":
                    headers.Add(key, api.Configuration.DefaultHeader[key]);
                    break;
                }
            }

            CookieCollection cookieCollection = CookieManager.Cookies.GetCookies(new Uri(api.GetBasePath()));

            /**
             * GWS currently only supports LongPolling as a method to receive events.
             * So tell the CometD library to negotiate a handshake with GWS and setup a LongPolling session.
             */
            LongPollingTransport transport = new LongPollingTransport(null);

            transport.CookieCollection = cookieCollection;
            transport.HeaderCollection = headers;

            bayeuxClient = new BayeuxClient(api.GetBasePath() + "/notifications", new List <CometD.NetCore.Client.Transport.ClientTransport>()
            {
                transport
            });

            bayeuxClient.Handshake();
            bayeuxClient.WaitFor(30000, new List <BayeuxClient.State>()
            {
                BayeuxClient.State.Connected
            });

            if (bayeuxClient.Connected)
            {
                foreach (Cookie cookie in cookieCollection)
                {
                    CookieManager.AddCookie(cookie);
                }

                foreach (string channelName in subscriptions.Keys)
                {
                    IClientSessionChannel channel = bayeuxClient.GetChannel(channelName);
                    channel.Subscribe(this);
                }
            }
        }
        ///<inheritdoc/>
        public void Disconnect(int timeout)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("Cannot disconnect when disposed");
            }

            _bayeuxClient?.ResetSubscriptions();

            _logger.LogDebug("Disconnecting...");
            _bayeuxClient?.Disconnect();
            _bayeuxClient?.WaitFor(timeout, new[] { BayeuxClient.State.DISCONNECTED });

            _errorExtension.ConnectionError     -= ErrorExtension_ConnectionError;
            _errorExtension.ConnectionException -= ErrorExtension_ConnectionException;
            _errorExtension.ConnectionMessage   -= ErrorExtension_ConnectionMessage;

            _logger.LogDebug("Disconnected...");
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

            Console.WriteLine("1 - subscribe to channel");
            Console.WriteLine("q - unsubscribe from channel");
            Console.WriteLine("2 - subscribe to channel");
            Console.WriteLine("w - unsubscribe from channel");
            Console.WriteLine("x - exit");
            Console.WriteLine("");
            Console.WriteLine("");

            ServerEndpoint = new Uri(args [0]);

            BayeuxClient client = new BayeuxClient(ServerEndpoint);

            client.DataReceived += (object sender, BayeuxClient.DataReceivedEventArgs e) => {
                if (e.Data.ContainsKey("color"))
                {
                    Console.WriteLine("Received color : " + e.Data ["color"]);
                }
                else if (e.Data.ContainsKey("status"))
                {
                    Console.WriteLine("Received status : " + e.Data ["status"]);
                }
                else
                {
                    Console.WriteLine("Received unknown message");
                }
            };

            client.Subscribe("/devices/51251481d988b43c45000002");
            client.Connect();

            ConsoleKeyInfo key = Console.ReadKey();

            while (key.KeyChar != 'x')
            {
                if (key.KeyChar == '1')
                {
                    client.Subscribe("/devices/51251481d988b43c45000002");
                }

                if (key.KeyChar == 'q')
                {
                    client.Unsubscribe("/devices/51251481d988b43c45000002");
                }

                if (key.KeyChar == '2')
                {
                    client.Subscribe("/devices/512518fcd988b43c45000003");
                }

                if (key.KeyChar == 'w')
                {
                    client.Unsubscribe("/devices/512518fcd988b43c45000003");
                }

                if (key.KeyChar == '*')
                {
                    client.Subscribe("/dees/*");
                }

                if (key.KeyChar == 'i')
                {
                    client.Unsubscribe("/devices/*");
                }

                key = Console.ReadKey();
            }

            client.Disconnect();
            Console.WriteLine("Waiting to disconnect");
            client.WaitFor(BayeuxClient.ClientStateEnum.Disconnected);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }