public void SubscribeToNewsHeadlineStream() { string searchKeywords = SearchTextBox.Text; App.ctx.BeginLogIn(App.USERNAME, App.PASSWORD, a => { App.ctx.EndLogIn(a); //Next we create a connection to the streaming api, using the authenticated session streamingClient = App.StreamingClient; streamingClient.Connect(); //And instantiate a listener for news headlines on the appropriate topic //You can have multiple listeners on one connection newsListener = streamingClient.BuildNewsHeadlinesListener(searchKeywords); newsListener.Start(); //The MessageRecieved event will be triggered every time a new News headline is available, //so attach a handler for that event, and wait until something comes through NewsDTO recievedNewsHeadline = null; newsListener.MessageReceived += (s, e) => { recievedNewsHeadline = e.Data; //Add this new news headline to the main items collection. ItemViewModel item = new ItemViewModel(); item.Headline = recievedNewsHeadline.Headline; item.PublishDate = recievedNewsHeadline.PublishDate.ToString(); recievedNewsHeadline.StoryId = recievedNewsHeadline.StoryId; App.ViewModel.LoadData(item); }; }, null); }
private void ToggleSubscribeButton_Click(object sender, RoutedEventArgs e) { if (ToggleSubscribeButtonlabel.Text == "Subscribe") { _rcpClient = new Rpc.Client(new Uri(RpcUriTextbox.Text)); var userName = UserNameTextbox.Text; var streamingUri = new Uri(StreamingUriTextbox.Text); var topic = TopicTextbox.Text; Log("Creating session..."); _rcpClient.BeginLogIn(userName, PasswordTextbox.Text, loginResult => { try { _rcpClient.EndLogIn(loginResult); _logger.DebugFormat("Session is: {0}", _rcpClient.Session); Log("Creating streaming client..."); _streamingClient = StreamingClientFactory.CreateStreamingClient(streamingUri, userName, _rcpClient.Session); _streamingClient.StatusChanged += (s, message) => Log(string.Format("Status update: {0}", message.Status)); _streamingClient.Connect(); Log("Creating listener..."); _newsListener = _streamingClient.BuildNewsHeadlinesListener(topic); Log("Starting listener..."); _newsListener.Start(); Log("Listening to news stream..."); _newsListener.MessageReceived += (s, message) => { try { NewsDTO receivedNewsHeadline = message.Data; Log( string.Format( "Received: NewsDTO: StoryId {0}, Headline {1}, PublishDate = {2}", receivedNewsHeadline.StoryId, receivedNewsHeadline.Headline, //receivedNewsHeadline.PublishDate.ToString("u"))); // dates are currently strings receivedNewsHeadline.PublishDate)); } catch (Exception exception) { _logger.Error("Exception occured:", exception); } }; } catch (Exception exception) { _logger.Error("Exception occured:", exception); } }, null); ToggleSubscribeButtonlabel.Text = "Stop"; } else { try { Log("Stopping listening to news stream..."); // D: abbreviating conditionals makes it hard to step // and is also a good way to get bugs. you may notice that I always use // blocks. if (_newsListener != null) { _newsListener.Stop(); } Log("Disconnecting from streaming server..."); if (_streamingClient != null) { _streamingClient.Disconnect(); } Log("Deleting session..."); if (_rcpClient != null ) { // REMOVEME: i commented this out and still getting the ObjectDisposed exception // so it is not in the RpcClient _rcpClient.BeginLogOut(logoutResult => { // FIXME: id/session invalid - getting back LoggedOut: false /*do nothing*/ var breakTarget = 0; }, null); } } catch (Exception exception) { _logger.Error("Exception occured:", exception); } ToggleSubscribeButtonlabel.Text = "Subscribe"; } }