/// <summary>
        /// Handles all Bayeux client errors.
        /// </summary>
        protected virtual void HandleBayeuxErrors()
        {
            IClientSessionChannel channel = _bayeuxClient.GetChannel("/**");

            if (null != channel)
            {
                channel.AddListener(new CallbackMessageListener <IStreamingAPIClient>(OnBayeuxClientFailure, this));
            }
        }
        ///<inheritdoc/>
        public void SubscribeTopic(string topicName, IMessageListener listener, long replayId = -1)
        {
            if (topicName == null || (topicName = topicName.Trim()).Length == 0)
            {
                throw new ArgumentNullException(nameof(topicName));
            }

            if (listener == null)
            {
                throw new ArgumentNullException(nameof(listener));
            }

            var channel = _bayeuxClient.GetChannel(topicName, replayId);

            channel?.Subscribe(listener);
        }
示例#3
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());
 }
示例#4
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}");
 }
示例#5
0
        private void tryToDelete(string mychannel)
        {
            //如果发哥的程序没有跟新就用下面这个调试
            //string url = "http://112.74.22.182:8080/BubbleServer/cometd";
            string url     = "http://112.74.22.182/cometd";
            var    options = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase)
            {
                // CometD server socket timeout during connection: 2 minutes
                { ClientTransport.MaxNetworkDelayOption, 120000 }
            };

            using (client = new BayeuxClient(url, new LongPollingTransport(options)))
            {
                if (client.Handshake(null, 3000))  // Handshake timeout: 30 seconds
                {
                    IClientSessionChannel channel = client.GetChannel("/service/deleteRoom");
                    channel.Subscribe(new CallbackMessageListener <BayeuxClient>(ReplyForDelete, client));
                    var data = new Dictionary <string, string>()
                    {
                        { "roomId", mychannel }
                    };
                    channel.Publish(data);
                }
            }
        }
示例#6
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);
                }
            }
        }
示例#8
0
        private void connect(String mychannel)
        {
            // Initializes a new BayeuxClient
            //string url = "http://112.74.22.182:8080/BubbleServer/cometd";
            string url     = "http://112.74.22.182/cometd";
            var    options = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase)
            {
                // CometD server socket timeout during connection: 2 minutes
                { ClientTransport.MaxNetworkDelayOption, 120000 }
            };

            using (client = new BayeuxClient(url, new LongPollingTransport(options)))
            {
                // Connects to the Bayeux server
                if (client.Handshake(null, 10000))  // Handshake timeout: 30 seconds
                {
                    // Subscribes to channels
                    IClientSessionChannel channel = client.GetChannel("/" + mychannel);
                    channel.Subscribe(new CallbackMessageListener <BayeuxClient>(OnMessageReceived, client));


                    /*
                     * // Publishes to channels
                     * var data = new Dictionary<string, string>()
                     * {
                     *  { "sender", "PC" },
                     *  { "msg","pc text!"}
                     * };
                     * channel.Publish(data);
                     */
                }

                /*else {
                 *  MessageBox.Show("Connect failed!");
                 *  statics = 1;
                 * }*/
            }
        }