Simple messaging
The built-in generic
controller uses a simple messaging pattern. Basically it is just a controller with a OnMessage
method. In that method you may have some simple logic to decide what to do with the message.
This pattern will quickly become a nightmare if your application complexity increases.
Basic Sample
On the controller there is a OnMessage
method that you can override. The OnMessage will be invoked if there is no other method matching the incoming data. In the Simple Messaging
pattern there is probably on the OnMessage
method.
public class Foo : XSocketController
{
public override async Task OnMessage(IMessage m)
{
//Do something clever with the message, maybe just broadcast it to all clients?
this.InvokeToAll(m);
}
}
Bad Sample
This pattern will (as already mentioned) become a nitghmare if you add to much logic into it. When that happens you should take a look at PUB/SUB
and RPC/RMI
.
As soon as you start to get a lot of conditions in your OnMessage
method you are on thin ice :)
public class Foo : XSocketController
{
public override async Task OnMessage(IMessage m)
{
switch(m.Topic)
{
case "a":
//business logic
break;
case "b":
//business logic
break;
case "c":
//business logic
break;
case "d":
//business logic
break;
case "e":
//business logic
break;
case "f":
//business logic
break;
}
}
}
I think you get the picture. This would be much easier to maintain if each "a" - "f" would have been isolated action methods.
GitHub Samples
There is 2 samples for you covering this on our XVA repository on github.