VWG To WIseJ Conversion / Custom Socket and Static Instance Query

0
0

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 :-

  1. Is there anything technically wrong with this approach (from a WiseJ Perspective)?
  2. Is there anything that I need to worry about / be aware of when doing this?
  3. Anything else that may be relevant to this approach that may come back and haunt me in the future 🙂

Thanks,

Daniel.

 

  • You must to post comments
0
0

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

 

  • Daniel Kelly
    Hi Paul, Thanks for the response. Im a bit confused about your last comment and making sure that I disconnect the clients. That was an issue in our VWG instance as each client would have their own instance of the MessageBus and we would need to try to call CloseDown() on our bus to disconnect them (which was a challenge to get it to work reliably). My understanding of the static instance is that their will only be a single Message Bus so it will be irrespective of how many clients I have (our service will only show as a single socket connection) so when a session expires there should be nothing that I need to do specific to the Message Bus. I will use the Sessions Time out event to clean up other resources that may be in need of it. Am I missing or misunderstanding the concept? Thanks, Daniel.
  • Paul
    • Paul
    • May 28, 2021 - 4:27 am
    Hello Daniel You are right about static patterns, my suggestion was just to be careful in the way you are handling it and when all clients have finished their sessions. You’re already doing it for what you describe Greetings
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.