Hi,
We are in the process of porting a VWG application to WiseJ and all is going great so far, however I have a conceptual question as the best way to handle something.
We have a custom Socket Implementation which we call a MessageBus. Essentially it is a wrapper around the c# Socket class which manages everything such as Connection / Health Monitoring / Retries / Message Payload / Subscription etc. The are 3 main events which the client subscribes to which are Connect / Disconnect / MessageArrival. The wrapper is cross platform and we use it in a lot of other applications but the main purpose of it in this instance, is to push messages out from our Windows Service to the Web Applications (and other applications) and to also tell clients when things have changed.
So all web clients (and other clients) connect to our Windows Service and as an example if someone on a Web Client (or Windows Client or Mobile Client) update something, a message is sent to the Windows Service which will know which clients to send a message to that a specific event has happened.
In VWG, on the Form load event we create a new instance of the Message Bus and attach to the 3 events. So if we have 10 users on their Web Applications, we would have 10 instances of the Message Bus.
Based on a previous sample of a BroadcastTimer using a Static Instance for all clients. I’m wondering if I could do a similar thing with our MessageBus. It would save us lots of resources and our application flow / logic would work fine with a single instance of a message coming through.
Here is the Static class.
public static class ServiceBus {
private static ConnectMessageBus m_messageBus;
public static event EventHandler Connected;
public static event EventHandler DisConnected;
public static event EventHandler MessageArrived;
static ServiceBus() {
//Initialise The Message Bus
m_messageBus = new ConnectMessageBus(Convert.ToInt32(ConfigurationManager.AppSettings[“MessageBusPort”]), false);
m_messageBus.onConnected += m_messageBus_onConnected;
m_messageBus.onDisconnected += m_messageBus_onDisconnected;
m_messageBus.onMessageArrival += m_messageBus_onMessageArrival;
m_messageBus.ConnectToServer(ConfigurationManager.AppSettings[“MessageBusServer”], Convert.ToInt32(ConfigurationManager.AppSettings[“MessageBusPort”]),
new int[] { (int)MessageBusEvents.CallWasCancelled, (int)MessageBusEvents.CallWasChanged, (int)MessageBusEvents.ClientOnline,
(int)MessageBusEvents.LiveReportData, (int) MessageBusEvents.CallWasRestored, (int)MessageBusEvents.UpdateEMS, (int)MessageBusEvents.UnallocateCallFromAttendant,
(int) MessageBusEvents.CallWasAccepted, (int)MessageBusEvents.UpdateWebHeaders, (int)MessageBusEvents.RefreshShifts,
(int)MessageBusEvents.StartShift, (int)MessageBusEvents.EndShift, (int)MessageBusEvents.UpdateShift, (int)MessageBusEvents.UpdateGroupTree,
(int)MessageBusEvents.RequestActiveSystemCalls, (int)MessageBusEvents.UpdateLiveViewRefreshInterval, (int)MessageBusEvents.RefreshApplicationHeaders,
(int)MessageBusEvents.AssignCallToAttendant, (int)MessageBusEvents.UpdateGroupTree, (int)MessageBusEvents.UpdateEMS,
(int)MessageBusEvents.RefreshLiveViewDesigner, (int)MessageBusEvents.RefreshGamingLiveView, (int)MessageBusEvents.RequestCallCounts,
(int)MessageBusEvents.UpdateLiveViewAttendantNameFormat, (int)MessageBusEvents.UpdateWebApplicationHeaders,
(int)MessageBusEvents.ClientGUIALiveCheck, (int)MessageBusEvents.ServiceTimerStarted, (int)MessageBusEvents.LiveViewPeriodicRefresh });
}
private static void m_messageBus_onMessageArrival(BaseMessages.Message message) {
if (MessageArrived != null) MessageArrived(message, EventArgs.Empty);
}
private static void m_messageBus_onDisconnected() {
if (DisConnected != null) DisConnected(null, EventArgs.Empty);
}
private static void m_messageBus_onConnected() {
if (Connected != null) Connected(null,EventArgs.Empty);
}
}
In the WiseJ application, I simply have the following :-
ServiceBus.Connected += ServiceBus_Connected;
ServiceBus.DisConnected += ServiceBus_DisConnected;
ServiceBus.MessageArrived += ServiceBus_MessageArrived;
And For Testing :-
private void ServiceBus_MessageArrived(object sender, EventArgs e) {
var message = (BaseMessages.Message)sender;
var messageType = (MessageBusEvents)message.MessageType;
listStatus.Items.Add($”New Message – {messageType}”);
Wisej.Web.Application.Update(this);
}
From all the testing I have done, it works perfectly but I just wanted to ask the following :-
Thanks,
Daniel.
Hi Daniel
Your code looks fine
The only thing to worry about when using static patterns is to make sure to disconnect clients when their session expires to avoid leaks.
Regards and happy coding
Please login first to submit.