RPC / RMI
The RemoteProcedureCall
pattern let you do exactly what is says, call procedures remotely. RPC together with XSockets fine grained control, let you send messages to specific clients in a very smooth way.
You will see more about this powerful feature combined with RPC below
How to call methods on the client
If you just want to send a message to the caller of the method, use Invoke
Server
//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
//Send a message to the caller
this.Invoke("Hello to caller from server", "chatmessage");
Client
Client - JavaScript
conn.controller('chat').chatmessage = function(data){console.log(data)};
Client - C#
conn.Controller("chat").On<string>("chatmessage", data => Console.WriteLine(data));
How to call methods on all clients
If you want to send a message to all clients connected to the controller, use InvokeToAll
Server
//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
this.InvokeToAll("Hello to all from server", "chatmessage");
Client
Client - JavaScript
conn.controller('chat').chatmessage = function(data){console.log(data)};
Client - C#
conn.Controller("chat").On<string>("chatmessage", data => Console.WriteLine(data));
How to target a subset of the connected clients
If you want to send a message to some of the clients connected to the controller, use InvokeTo
This is where you will see the power of state! You will actually get intellisense so that you can write lambda expressions to target the clients you want to send the message to.
If we have the properties City and Gender on the Controller
(for example) we can target exactly the ones we want to.
Below for example we send to all clients having the same gender and location as the caller
Server
//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
this.InvokeTo(p => p.Gender == this.Gender && p.City == this.City,"Hello to some from server", "chatmessage");
Client
Client - JavaScript
conn.controller('chat').chatmessage = function(data){console.log(data)};
Client - C#
conn.Controller("chat").On<string>("chatmessage", data => Console.WriteLine(data));
How to send messages to clients connected on another Controller class
It is pretty much the same as sending to the clients on the same Controller
since XSockets extension-methods are generic.
So, if you are on Controller
A and want to send to all clients on Controller
B you just use...
//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
//To all
this.InvokeToAll<B>("Hello to all from server", "target");
//And the powerful `InvokeTo<T>` would have a signature like:
this.InvokeTo<T>(Func<T,bool> expression, object obj, string target);
How to call client methods outside the Controller class
Just create a controller instance (or ask the plugin framework after the specific controller) and then use the extensions to send data
//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
//Create the instance your self
var chat = new Chat();
//Then just use one of the extensions to target, one, some, others or all...
chat.InvokeToAll<Chat>("Hello from manual instance","say");
Notice that you have to pass in <T>
in the extension regardless of what controller you want to use. So you can even do
new Chat().InvokeToAll<Stock>("Hello to stock clients from chat instance","hi");
Note: there is no point in using Invoke/Publish since the actual controller does not have a socket since there is no client connected to it (we created the instance manually).
GitHub RPC/RMI Sample
https://github.com/XSockets/XVA/tree/master/XVA-01-06-StateRPC