Team XSockets.NET

Introduction to controllers

A controller is (as most things in XSockets) a module. The controller is the place where you write your logic. Since the core purpose of XSockets is to dispatch messages the logic will mostly be about that. Depending on your solution you might also use the controller in a more classic way where you call a service layer to perform CRUD operations etc.

How do I create a controller?

All it takes to create a controller is to inherit the XSocketController class. This class is located under XSockets.Core.XSocket.

public class Sample : XSocketController

This "sample" controller will now be found by the server at startup. It really does not do anything, but it will be in there.

OnMessage explained

In "the basics" section we created our first controller from a item template. This template had an override of the OnMessage method.

It looked like this.

using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using XSockets.Core.Common.Socket.Event.Interface;
using System.Threading.Tasks;

namespace XSocketsTutorial

    public class MyController : XSocketController
        public override async Task OnMessage(IMessage message)
            //Send the message to all clients connected to this controller
            await this.InvokeToAll(message);

The way this work is that when a message arrives at a controller it will look for a method on the controller that matches the topic name. So if there is a method Foo and the topic is Foo the framework will invoke that method and serialize the data into the expected parameter of Foo.

If there is no method matching the topic the framework will instead look for an override of the OnMessage method. If no override is found the message has no where to go and will not be processed.

The generic controller

There is a built-in controller that looks exactly like the one above, but it's name is generic. That controller is located in the assembly XSockets.Controllers, so if you remove that assembly the generic functionality goes away.

The generic controller is great when you quickly want to prototype without writing server side code. In combination with various client API's you can do a lot of advanced stuff with the generic controller.

results matching ""

    No results matching ""