Sample web socket service for event driven communication


Software Architecture


The web socket service provided for you to test your plugin code is written in ASP.NET core 2.0 and can be contained within Docker.  It is comprised of two main classes: the manager class and the middleware class.

StupidFunSocketManager


This class currently holds a collection of web socket connections so the service can broadcast if necessary.  Note this will only work for demonstration purposes; for production purposes you should leverage a centralized store or leverage an existing cloud based push notification system.

StupidFunSocketMiddleware


This class contains the code for reading messages sent from the plugin and assigning them to an appropriate adapter for processing.  The adapters would contain the business logic and are designed to work asynchronously, not in the typical request response RESTful fashion that the majority of the internet behaves like.

  

Activity Flow






Step #
Description
Software
1
Web socket connection opened and message received.
StupidFun.Online.StupidFunSocketMiddleware.Invoke()
2
New web socket connection received and added to the pool of open connections.
StupidFun.Online.StupidFunSocketManager.AddSocket()
3
Service confirms the web socket is open.
StupidFun.Online.StupidFunSocketMiddleware.Receive()
4
Service waits until all the bytes of the incoming message have been received.
StupidFun.Online.StupidFunSocketMiddleware.Receive()
5
Service delegates handling of the message to an adapter with appropriate business logic.
StupidFun.Business.Adapters.TheNerdAdapter.ProcessMessage()
6
Adapter fires event indicating the message has been processed.  (In the demo, this is a short process.  It could be a long run asynchronous process if required).
StupidFun.Business.Adapters.TheNerdAdapter.ProcessMessage()
7
The service handles the message processed event (if required).
StupidFun.Online.StupidFunSocketMiddleware.TheNerdAdapter_MessageProcessed
8
If necessary, the adapter can continue doing business specific logic.
StupidFun.Business.Adapters.TheNerdAdapter.ProcessMessage()
9
For the purposes of this demo, the adapter fires the message ready to send event.  (This could be after a period of time if required, as the adapter can run asynchronously).
StupidFun.Business.Adapters.TheNerdAdapter.ProcessMessage()
10
The service broadcasts the message to the clients specified by the adapter.
StupidFun.Online.StupidFunSocketMiddleware.TheNerdAdapter_MessageReadyToSend()
11
The service confirms the message is not a close connection and returns to step 3.  If it is a close then remove the connection from the pool and close it.
StupidFun.Online.StupidFunSocketManager.RemoveSocket()

The flow ends.


  

Deployment


For debugging purposes, just set this Visual Studio project as the startup one in the solution, and choose to run in IIS Express.  It will usually run on port 55770, but check the IIS icon in the bottom right side of the screen (right end of Windows task bar) by right clicking on it.  Then you can enter ws://localhost:55770 (or whatever port) into the Communication Manager's Base URL field:









References



A video of running both the plugin and service in debug mode in Visual Studio Community 2019:





Comments

Popular posts from this blog

Unity web socket manager plugin for event driven communication with a web socket service