StreamLink for Silverlight
Caplin.StreamLink.Subscription.Chat Namespace
StreamLink for SilverlightCaplin.StreamLink.Subscription.Chat
Contains interfaces, classes, and enums related to the Chat SubjectType. Defines the key interfaces concerning the subscription to a chat subject (IChatSubscription, IChatSubscriptionListener, the interface that provides access to the parameters used to request chat objects, (IChatSubscriptionParameters), and the interface for handling chat events (IChatEvent).
Declaration Syntax
C#Visual BasicVisual C++
namespace Caplin.StreamLink.Subscription.Chat
Namespace Caplin.StreamLink.Subscription.Chat
namespace Caplin.StreamLink.Subscription.Chat
Types
All TypesInterfaces
IconTypeDescription
IChatEvent
Provides information when a chat message is received. (Chat).

IChatSubscription
Represents a subscription to a chat subject.

IChatSubscriptionListener
The IChatSubscriptionListener interface allows applications to receive events raised for a chat subscription.

IChatSubscriptionParameters
Provides access to the parameters used for a subscription to a Chat room on the Liberator.

Remarks

Chat subscriptions are created through the IStreamLinkProvider interface using one of the CreateChatSubscription(IChatSubscriptionListener, String) methods. In all cases an IChatSubscriptionListener must be passed along with the name of the chat object being subscribed to.

CopyC#
using System;
using System.Collections.Generic;
using System.Text;
using Caplin.StreamLink.Subscription;
using Caplin.StreamLink;
using Caplin.StreamLink.Configuration;
using Caplin.StreamLink.Authentication;
using System.Threading;
using Caplin.StreamLink.Logging;
using Caplin.StreamLink.Subscription.Chat;
using Caplin.StreamLink.Command;

namespace StreamLinkExamples.Chat
{
    public class CreateChatSubscription
    {
        // Subscription Listener class to be informed about all Subscription information
        class MyChatListener : IChatSubscriptionListener, ICommandListener
        {
            #region ISubscriptionListener Members

            public void SubscriptionErrorReceived(ISubscription subscription, ISubscriptionErrorEvent ev)
            {
                // Process Subscription error
                Console.WriteLine("Subscription Status Update " + ev.Subject + " Error: " + ev.Error.ToString());
            }

            public void SubscriptionStatusUpdated(ISubscription subscription, ISubscriptionStatusEvent status)
            {
                // Process Subscription status callback
                Console.WriteLine("Subscription Status Update " + status.Subject + " Status: " + status.Status.ToString() + " " + status.Message);
            }

            #endregion

            #region IChatSubscriptionListener Members

            void IChatSubscriptionListener.ChatMessageReceived(ISubscription subscription, IChatEvent ev)
            {
                String msg = String.Format("{0}: chat message received from {1}: {2}", ev.Time, ev.User, ev.Message);
                Console.WriteLine(msg);
            }

            void IChatSubscriptionListener.ChatUserLoggedIn(ISubscription subscription, IChatEvent ev)
            {
                String msg = String.Format("{0}: user {1} joins the chat", ev.Time, ev.User);
                Console.WriteLine(msg);
            }

            void IChatSubscriptionListener.ChatUserLoggedOut(ISubscription subscription, IChatEvent ev)
            {
                String msg = String.Format("{0}: user {1} leaves the chat", ev.Time, ev.User);
                Console.WriteLine(msg);
            }

            void IChatSubscriptionListener.ChatUsernameNotification(ISubscription subscription, IChatEvent ev)
            {
                String msg = String.Format("{0}: successfully logged into chat. Username is {0}", ev.Time, ev.User);
                Console.WriteLine(msg);
            }

            #endregion

            #region ICommandListener Members

            void ICommandListener.CommandSuccess(ICommandResultEvent ev)
            {
                String msg = String.Format("Successfully published chat message to chat object {0}", ev.Subject);
                Console.WriteLine(msg);
            }

            void ICommandListener.CommandError(ICommandResultEvent ev)
            {
                String msg = String.Format("Was not able to publish chat message to chat object {0}. Result was {1}", ev.Subject, ev.Result.ToString());
                Console.WriteLine(msg);
            }

            #endregion
        }

        // Main execution method
        static void Example()
        {
            // Create basic streamlink
            IStreamLinkConfiguration myConfiguration = SimpleConfiguration.CreateType2HttpConnection("localvm", 45260);
            IStreamLink myStreamLink = new StreamLink(myConfiguration);
            myStreamLink.CredentialsProvider = new PasswordCredentialsProvider("admin", "admin");
            myStreamLink.StreamLinkProvider.Connect();

            //uncomment to include logging
            myStreamLink.Logger = new ConsoleLogger();

            // Create a listener object for chat updates and command callbacks
            IChatSubscriptionListener chatListener = new MyChatListener();

            // Create a chat subscription and subscribe to it
            IChatSubscription subscription = myStreamLink.StreamLinkProvider.CreateChatSubscription(chatListener, "/MyChat");
            subscription.Subscribe();

            // Send a few chat messages
            for (int i = 1; i < 4; i++)
            {
                IPublishParameters parameters = myStreamLink.StreamLinkProvider.ParametersFactory.CreateChatPublishParameters("This is chat message number " + i);
                myStreamLink.StreamLinkProvider.Publish(chatListener as ICommandListener, "/MyChat", parameters);

                Thread.Sleep(1500);
            }

            Thread.Sleep(30000);

            // Discard chat object (other users in the chat room will receive a logout notification)
            subscription.Unsubscribe();

            // Log out of StreamLink
            myStreamLink.StreamLinkProvider.Disconnect();
        }
    }
}

The object implementing IChatSubscriptionListener will then be informed of all subscription status, error, and update events. (Also see ISubscriptionListener.)

There are four possible callbacks that the object implementing IChatSubscriptionListener can receive: When you first subscribe to the chat object you will receive a call to the ChatUsernameNotification(ISubscription, IChatEvent) method informing you of your chat username.When any chat user publishes a chat message you will receive a call to the ChatMessageReceived(ISubscription, IChatEvent) method.When a StreamLink user subscribes to the chat object you will receive a call to the ChatUserLoggedIn(ISubscription, IChatEvent) method.When a StreamLink user discards the chat object you will receive a call to the ChatUserLoggedOut(ISubscription, IChatEvent) method.