C Sharp Examples: Difference between revisions

From Open Rail Data Wiki
m Tidy up code
m Add a note that Apache.NMS.Stomp does not work with C#/.NET on the new platform
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Apache NMS==
Sample code for C# .NET is available at [https://github.com/openraildata/stomp-client-dotnet on the openraildata organization on GitHub].


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].
Please be aware that the [https://activemq.apache.org/components/nms/providers/stomp/ Apache.NMS.Stomp client library] is was last updated around 2014 and does not work with the version of ActiveMQ used in the publicdatafeeds and approveddatafeeds platforms.
 
<code>
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);
      }
    }
  }
 
}
</code>


{{Navtable-DataFeeds}}
{{Navtable-DataFeeds}}

Latest revision as of 20:09, 25 January 2023