Method overloading
Having two methods with the same name (overloading) will not work our of the box.
If we declare two Foo
methods with different signatures.
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using System.Threading.Tasks;
namespace XSocketsTutorial
{
public class MyController : XSocketController
{
public async Task Foo()
{
//Send a message to all clients with the topic 'say'
await this.InvokeToAll("Hello to all from a foo","foo");
}
public async Task Foo(string message)
{
//Send out the message that was sent in to all clients
await this.InvokeToAll(string.Format("Foo: {0}", message), "foo");
}
}
}
The server will give us an error message at startup.
The ControllerEvent
attribute
By using the ControllerEvent
attribute we can allow method overloading. This is done by giving the methods another name in XSockets so that the routing will be able to know where to send messages.
This attribute is located under the namespace XSockets.Core.Common.Socket.Event.Attributes
.
Below we have decorated the Foo methods with unique names, so now method overloading will be allowed. Do note that we now have to use the names given in the ControllerEvent
attribute to be able to call the methods.
[ControllerEvent("F1")]
public async Task Foo()
{
//Send a message to all clients with the topic 'say'
await this.InvokeToAll("Hello to all from a foo","foo");
}
[ControllerEvent("F2")]
public async Task Foo(string message)
{
//Send out the message that was sent in to all clients
await this.InvokeToAll(string.Format("Foo: {0}", message), "foo");
}
This is not only useful for overloading, it is also good to use to save bytes in communication. Methods often have long named to describe what they do. By using the ControllerEvent
attribute you can give the methods shorter names and get even more efficient communication.