A message could be any class representing the data you want to bradcast. When someone publishes a message on the bus all subscribers of that message type will be notified.

Dummy class used in examples below
class TestMessage
{
    public TestMessage(int value)
    {
        Value = value;
    }

    public int Value
    {
        get;
        private set;
    }
}

How to subscribe to a message type
A subscriber/listener must implement the ISubscriber interface. The message type is a generic type argument on this interface.

class TestMessageSubscriber : ISubscriber<TestMessage>
{
        public TestMessageSubscriber()
        {
              // subscribe to the TestMessage
              PubSubLite.Bus.Subscribe<TestMessage>(this);
        }

        void ISubscriber<TestMessage>.HandleMessage(IMessage<TestMessage> message)
        {
            // this method will be invoked when someone publishes a TestMessage on the bus
            // you will find the actual message (TestMessage in this case) in the Data property of the message.
            Console.WriteLine(string.Format("Subscriber - Receive TestMessage1, value={0}", message.Data.Value));
        }
}

How to publish a message
PubSubLite.Bus.Publish(new TestMessage(3));

Last edited Nov 4, 2010 at 1:58 PM by swmal, version 5

Comments

No comments yet.