Difference between revisions of "C Sharp Examples"

From Open Rail Data Wiki
Jump to navigation Jump to search
(Moved C# examples to new page)
 
m (Added GitHub link)
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
==Apache NMS==
 
==Apache NMS==
  
This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages.  For more details on this client, see [http://gavincoates.com/c-activemq-stomp Gavin's blog].
+
This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages.   
  
<pre>
+
Sample code for C# .NET is available at [https://github.com/openraildata/stomp-client-dotnet on the openraildata organization on GitHub].
 +
 
 +
<code>
 
  using System;
 
  using System;
 
  using System.Collections.Generic;
 
  using System.Collections.Generic;
Line 10: Line 12:
 
  using System.IO;
 
  using System.IO;
 
  using Apache.NMS;
 
  using Apache.NMS;
 
+
 
 
  namespace ApacheNMS
 
  namespace ApacheNMS
 
  {
 
  {
    class Program
+
  class Program
    {
+
  {
        static void Main(string[] args)
+
    static void Main(string[] args)
        {
+
    {
            IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.co.uk:61618"));
+
      IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.co.uk:61618"));
 
+
 
            IConnection connection = factory.CreateConnection("username", "password");
+
      IConnection connection = factory.CreateConnection("username", "password");
            ISession session = connection.CreateSession();
+
      ISession session = connection.CreateSession();
 +
 
 +
      IDestination destination = session.GetDestination("topic://" + "TRAIN_MVT_ALL_TOC");
 +
      IMessageConsumer consumer = session.CreateConsumer(destination);
 +
 
 +
      connection.Start();
 +
      consumer.Listener += new MessageListener(OnMessage);
 +
      Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");
 +
 
 +
      Console.ReadLine();
 +
      connection.Close();
 +
    }
 +
 
 +
    private static void OnMessage(IMessage message)
 +
    {
 +
      try
 +
      {
 +
          Console.WriteLine("Median-Server (.NET): Message received");
 +
 
 +
          ITextMessage msg = (ITextMessage)message;
 +
          message.Acknowledge();
 +
 
 +
          Console.WriteLine(msg.Text);
 +
      }
 +
      catch (Exception ex)
 +
      {
 +
          Console.WriteLine(ex.Message);
 +
          Console.WriteLine("---");
 +
          Console.WriteLine(ex.InnerException);
 +
          Console.WriteLine("---");
 +
          Console.WriteLine(ex.InnerException.Message);
 +
      }
 +
 
 +
    }
 +
 
 +
  }
 +
 +
}
 +
</code>
  
            IDestination destination = session.GetDestination("topic://" + "TRAIN_MVT_ALL_TOC");
+
{{Navtable-DataFeeds}}
            IMessageConsumer consumer = session.CreateConsumer(destination);
 
 
 
 
 
            connection.Start();
 
            consumer.Listener += new MessageListener(OnMessage);
 
            Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");
 
 
 
            Console.ReadLine();
 
            connection.Close();
 
        }
 
 
 
        private static void OnMessage(IMessage message)
 
        {
 
            try
 
            {
 
                Console.WriteLine("Median-Server (.NET): Message received");
 
 
 
                ITextMessage msg = (ITextMessage)message;
 
                message.Acknowledge();
 
 
 
                Console.WriteLine(msg.Text);
 
            }
 
            catch (Exception ex)
 
            {
 
                Console.WriteLine(ex.Message);
 
                Console.WriteLine("---");
 
                Console.WriteLine(ex.InnerException);
 
                Console.WriteLine("---");
 
                Console.WriteLine(ex.InnerException.Message);
 
            }
 
        }
 
    }
 
}
 
</pre>
 
  
 
[[Category:Network Rail Data Feeds]]
 
[[Category:Network Rail Data Feeds]]
 +
[[Category:Example Code]]

Revision as of 13:02, 3 October 2022

Apache NMS

This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages.

Sample code for C# .NET is available at on the openraildata organization on GitHub.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Apache.NMS;
 
namespace ApacheNMS
{
  class Program
  {
    static void Main(string[] args)
    {
      IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.co.uk:61618"));
 
      IConnection connection = factory.CreateConnection("username", "password");
      ISession session = connection.CreateSession();
 
      IDestination destination = session.GetDestination("topic://" + "TRAIN_MVT_ALL_TOC");
      IMessageConsumer consumer = session.CreateConsumer(destination);
 
      connection.Start();
      consumer.Listener += new MessageListener(OnMessage);
      Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");
 
      Console.ReadLine();
      connection.Close();
    }
 
    private static void OnMessage(IMessage message)
    {
      try
      {
          Console.WriteLine("Median-Server (.NET): Message received");
 
          ITextMessage msg = (ITextMessage)message;
          message.Acknowledge();
 
          Console.WriteLine(msg.Text);
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.Message);
          Console.WriteLine("---");
          Console.WriteLine(ex.InnerException);
          Console.WriteLine("---");
          Console.WriteLine(ex.InnerException.Message);
      }
 
    }
  
  }

}


Network Rail Open Data Feeds
Data Feeds About the Feeds Account States Durable Subscriptions Example Code ( PHP / C# / Java / Ruby / Node.js) • Advanced UsesFAQ Release Notes
RTPPM RTPPM Feed
Train Movements Train Movements Feed Train Activation Train Cancellation Train Movement Train Reinstatement Change of Origin Change of Identity Change of Location TSPEED Field Planned Cancellations Cancellation Codes
TD TD Feed C-Class Messages S-Class Messages Train Describers TD Berths
VSTP VSTP Feed
TSR TSR Feed Route Codes
SCHEDULE SCHEDULE Feed Schedule and Location Records Association Records CIF Codes How Scheduling Works Allowances
Reference Data Reference Data Feed TOC Codes CIF Codes Delay Attribution Codes Identifying Locations (STANOX, TIPLOC, NLC and 3-Alpha Codes) STANOX Geographical Areas Train Planning data