Universal Messaging Developer Guide : Universal Messaging Web Client APIs : Silverlight : Examples : Universal Messaging Silverlight (C#): Live Stock Indices
Universal Messaging Silverlight (C#): Live Stock Indices
This example demonstrates how to subscribe to a Universal Messaging channel and render prices received in real-time events.
Application Source Code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using com.pcbsys.nirvana.client;
namespace Silverlight_LiveStockIndices
{
public partial class Page : UserControl,nReconnectHandler,nEventListener
{
private bool started;
public nSession mySession;
private ObservableCollection<RatesData> myRatesDataListSource = new ObservableCollection<RatesData>();
public Thread sessionThread;
private const string RNAME = "nhps://showcase.um.softwareag.com:443";
private const string RATES_CHANNEL = "/showcase/stockindices";
public Page()
{
InitializeComponent();
StartupProgressDialog.IsOpen = true;
myIndexGrid.ItemsSource = this.myRatesDataListSource;
myIndexGrid.IsReadOnly = true;
App.Current.Host.Content.Resized += (s, e) =>
{
theBack.Width = App.Current.Host.Content.ActualWidth;
theBack.Height = App.Current.Host.Content.ActualHeight;
};
sessionThread = new Thread(new ThreadStart(startSubscribers));
sessionThread.IsBackground = true;
sessionThread.Start();
}
public void startSubscribers()
{
if (!started)
{
try
{
nSessionAttributes nsa = new nSessionAttributes(RNAME, 5);
mySession = nSessionFactory.create(nsa, this, "SilverDemoUser");
mySession.init();
StartupProgressDialog.Dispatcher.BeginInvoke(new setProgressBarMessage(updateStatusMessage), "Subscribing to Rates...");
nChannelAttributes ncaindices = new nChannelAttributes();
ncaindices.setName(RATES_CHANNEL);
nChannel myRatesChannel = mySession.findChannel(ncaindices);
myRatesChannel.addSubscriber(this, 0);
StartupProgressDialog.Dispatcher.BeginInvoke(new setOverlayPanelVisibleDelegate(setOverlayPanelVisible), false);
}
catch (Exception e)
{
Console.WriteLine("Error starting subscribers: " + e.Message);
Console.WriteLine(e.StackTrace);
}
started = true;
}

}
public delegate void setProgressBarMessage(String message);
public void updateStatusMessage(String message)
{
myStatusMessage.Text = message;
}
public delegate void setOverlayPanelVisibleDelegate(Boolean flag);
public void setOverlayPanelVisible(Boolean flag)
{
StartupProgressDialog.IsOpen = flag;
}
public void disconnected(nSession anSession)
{
StartupProgressDialog.Dispatcher.BeginInvoke(new setProgressBarMessage(updateStatusMessage), "Disconnected...");
StartupProgressDialog.Dispatcher.BeginInvoke(new setOverlayPanelVisibleDelegate(setOverlayPanelVisible), true);
Console.WriteLine("Disconnected");
}
public void reconnected(nSession anSession)
{
StartupProgressDialog.Dispatcher.BeginInvoke(new setOverlayPanelVisibleDelegate(setOverlayPanelVisible), false);
Console.WriteLine("Reconnected");
}
public bool tryAgain(nSession anSession)
{
return true;
}
public void go(nConsumeEvent evt)
{
if (evt.getChannelName().Equals(RATES_CHANNEL))
{
nEventProperties nep = evt.getProperties();
myIndexGrid.Dispatcher.BeginInvoke(new RatesDataDelegate(updateRatesGrid), nep.getString("name"), nep.get("value").ToString());
return;
}
}
public delegate void RatesDataDelegate(String index, String ival);

private void updateRatesGrid(String index, String ival)
{
try
{
Boolean found = false;
foreach (RatesData item in myRatesDataListSource)
{
if (item.Index.Equals(index))
{
if (item.Price != ival)
{
item.Price = ival;
int currentidx = myRatesDataListSource.IndexOf(item);
myRatesDataListSource.Remove(item);
myRatesDataListSource.Insert(currentidx, new RatesData() {Index = index, Price = ival});
myIndexGrid.SelectedIndex = currentidx;

}
found = true;
}
}
if (!found)
{
RatesData newratesd = new RatesData() { Index = index, Price = ival };
myRatesDataListSource.Insert(0, newratesd);
myIndexGrid.SelectedIndex = 0;
}
}
catch (Exception ex)
{
Console.WriteLine("Error updateing index grid");
Console.WriteLine(ex.Message);
}
}
}
}
Copyright © 2013-2014 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback