Universal Messaging Developer Guide : Universal Messaging Web Client APIs : Silverlight : Examples : Universal Messaging Silverlight (C#): Simple Chat Room
Universal Messaging Silverlight (C#): Simple Chat Room
This example demonstrates how to subscribe and publish to a Universal Messaging channel.
Application Source Code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;
namespace Silverlight_SimpleChatRoom
{
public partial class Page : UserControl,nEventListener,nReconnectHandler
{
private bool started;
public nSession mySession;
private ObservableCollection<ChatData> myChatDataListSource = new ObservableCollection<ChatData>();
public Thread sessionThread;
private nChannel myChatChannel;
private const string RNAME = "nhps://showcase.um.softwareag.com:443";
private const string CHAT_CHANNEL = "/showcase/simplechatroom";
public Page()
{
InitializeComponent();
StartupProgressDialog.IsOpen = true;
lstChat.ItemsSource = this.myChatDataListSource;
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 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 Chat...");
nChannelAttributes ncachat = new nChannelAttributes();
ncachat.setName(CHAT_CHANNEL);
myChatChannel = mySession.findChannel(ncachat);
myChatChannel.addSubscriber(this, 0);
StartupProgressDialog.Dispatcher.BeginInvoke(new setOverlayPanelVisibleDelegate(setOverlayPanelVisible), false);
}
catch (Exception e)
{
Console.WriteLine("Error starting subscribers: " + e.Message);
Console.WriteLine(e.StackTrace);
}
started = true;
}

}
public void go(nConsumeEvent evt)
{
if (evt.getChannelName().Equals(CHAT_CHANNEL))
{
nEventProperties nep = evt.getProperties();
String msg = nep.getString("message");
String sender = nep.getString("sender");
nEventAttributes nea = evt.getAttributes();
long tval = nea.getTimestamp();
DateTime ttime = ConvertJavaMiliSecondToDateTime(tval);
lstChat.Dispatcher.BeginInvoke(new ChatDataDelegate(updateChatList), sender, msg, ttime.ToString());
return;
}
}
private void Send_Button_Click(object sender, RoutedEventArgs e)
{
if (txtMessage.Text == null) return; //to handle enter key pressed in general
String senderuser = "SilverUser" + ("" + mySession.getSessionConnectionId()).Substring(13);
String message = txtMessage.Text;
nEventProperties props = new nEventProperties();
props.put("sender", senderuser);
props.put("message", message);
nConsumeEvent evt = new nConsumeEvent(props, "chatmsg");
myChatChannel.publish(evt);
txtMessage.Text = "";
}
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 ChatDataDelegate(String sender, String message, String timestamp);
public void updateChatList(String sender, String message, String timestamp)
{
ChatData somechatmessage = new ChatData() { Message = message, Sender = sender, TimeStamp = timestamp };
myChatDataListSource.Insert(0, somechatmessage);
}
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 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;
}
private void txtMessage_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && txtMessage.Text != null && txtMessage.Text.Trim().Length>0)
{
//Handle Enter Here.
e.Handled = true;
Send_Button_Click(sender, e);
}
else
{
e.Handled = false;
}
}
}
}
Copyright © 2013-2014 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback