Adding custom methods
In the introduction
section we took a look at how to use the OnMessage
method. We also described how the framework will decide what method to invoke.
So let's add a few custom methods.
public class MyController : XSocketController
{
public override async Task OnMessage(IMessage message)
{
await this.InvokeToAll(message);
}
public async Task Foo()
{
//Send a message to all clients with the topic 'say'
await this.InvokeToAll("Foo: Hello to all","foo");
}
public async Task Bar(string message)
{
//Send out the message that was sent in to all clients
await this.InvokeToAll(string.Format("Bar: {0}",message),"bar");
}
}
We now expect the OnMessage
to be hit whenever the topic
and parameter does not match the other methods. But if we call Foo
with no parameters or Bar
with a string we expect those method to be hit.
We try this in the easiest way by using Putty.
Connect
We connect to localhost on the port configured. In my case 9090.
Then I pass the handshake for the PuttyProtocol by just sending PuttyProtocol
Send a message that should hit the OnMessage
method
We send the topic Baz
since there is no method defined for that. Do note that I do not have to use the controller
suffix in the controller name.
Send a message to the Foo
method
We will just call the MyController
and the method Foo
, but not pass in any data since the Foo
method has no parameters
Send a message to the Bar
method
Similar to the previous call to Foo
, but this time we also pass in additional data since Bar
expects a string.
Parameter types?
The simple putty protocol will not allow us to send any complex object without changing the way serialization is done. That is out of topic for now. You can however change to other built-in types such as int
public async Task Bar(int message)
{
//Send out the message that was sent in to all clients
await this.InvokeToAll(string.Format("Bar: {0}", message), "bar");
}
and the call the method like