Universal Messaging 9.12 | Developer Guide | Web Client APIs | Web Developer's Guide for Silverlight | Examples | Live Stock Chart
 
Live Stock Chart
This example demonstrates how to subscribe to a Universal Messaging channel and chart prices received in real-time events.
Application Source Code
using System;
using System.Collections.Generic;
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;
using Visifire.Charts;
namespace Silverlight_LiveStockChart
{
public partial class Page : UserControl,nEventListener,nReconnectHandler
{

private bool started;
public nSession mySession;
public Thread sessionThread;
private long myEventCount=0;
private const string RNAME = "nhps://showcase.um.softwareag.com:443";
private const string RATES_CHANNEL = "/showcase/stockhistory";
public Page()
{
InitializeComponent();
CreateChart();
StartupProgressDialog.IsOpen = true;
sessionThread = new Thread(new ThreadStart(startSubscribers));
sessionThread.IsBackground = true;
sessionThread.Start();
App.Current.Host.Content.Resized += (s, e) =>
{
theBack.Width = App.Current.Host.Content.ActualWidth;
theBack.Height = App.Current.Host.Content.ActualHeight;
};
}

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))
{
myEventCount++;
if (myEventCount>=100)
{
StartupProgressDialog.Dispatcher.BeginInvoke(new
setOverlayPanelVisibleDelegate(setOverlayPanelVisible),
false);
}
nEventProperties nep = evt.getProperties();
nEventAttributes nea = evt.getAttributes();
long tval = nea.getTimestamp();
DateTime ttime = ConvertJavaMiliSecondToDateTime(tval);
myChart.Dispatcher.BeginInvoke(new
RatesDataDelegate(updateRatesGrid), ttime.ToShortTimeString(),
nep.get("value").ToString());
return;
}
}
public DateTime ConvertJavaMiliSecondToDateTime(long javaMS)
{
DateTime UTCBaseTime = new DateTime(1970, 1, 1, 0, 0, 0,
DateTimeKind.Utc);
DateTime dt = UTCBaseTime.Add(new TimeSpan(javaMS *
TimeSpan.TicksPerMillisecond)).ToLocalTime();
return dt;
}
public delegate void RatesDataDelegate(String index, String ival);

private void updateRatesGrid(String time, String ival)
{
DataPoint dataPoint = new DataPoint();
// Set YValue for a DataPoint
dataPoint.YValue = Double.Parse(ival);
dataPoint.AxisXLabel = time;

// Add dataPoint to DataPoints collection.
myChart.Series[0].DataPoints.Add(dataPoint);
}
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 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
setProgressBarMessage(updateStatusMessage),
"Waiting for 100 events to create chart...");

}
catch (Exception e)
{
Console.WriteLine("Error starting subscribers: " + e.Message);
Console.WriteLine(e.StackTrace);
}
started = true;
}

}
/// <summary>
/// Function to create a Visifire Chart
/// </summary>
public void CreateChart()
{

// Create a new instance of Title
Title title = new Title();
// Set title property
title.Text = "NSL PLC.";
// Add title to Titles collection
myChart.Titles.Add(title);

}
}
}

Copyright © 2013-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.