Parameters, Model binding & return values - XSockets/XSockets.NET-4.0 GitHub Wiki

##Parameters, Model binding & return values XSockets have had strongly typed model binding for generations, this is one of the things that makes it easy to use. Just declare methods on the controllers and specify the complex parameter on the methods. XSockets will transform the JSON sent in to the correct parameter type.

You can return any serializable object from the action methods. The value returned will end up at the client that initiated the call. As of 4.0 you can also return Task and Task<T> from the action methods and the client will get the data back when the Task is completed.

###How to use complex objects as parameters Above we looked at how you can send complex types to the clients, of course the client can send complex types to the server as well.

Server

A simple model for our chat sample

public class ChatModel
{
    public string UserName {get;set;}
    public string Text {get;set;}
}

Server code that accepts a complex object and sends it to all clients. Since we have the username (showed in How to use state on the server) we never pass that in, we only set it before sending data out.

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
public void ChatMessage(ChatModel message)
{
    message.UserName = this.UserName;
    this.InvokeToAll(message,"chatmessage");
}

Clients

Client - JavaScript

conn.controller('chat').invoke('chatmessage',{Text:'Hello from JS'});

Client - C#

conn.Controller("chat").Invoke("chatmessage", new ChatModel{Text="Hello from CS"});

###How to use IMessage as parameter When you do not process the data server-side there is no need serializing incoming messages. In these situations you can use IMessage as parameter since any message sent into XSockets.NET will be transformed into IMessage internally.

This is how the Generic controller used in the Getting started with real-time communication sample is built. The code for Generic is very simple and looks exactly like this.

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
public class Generic : XSocketController
{
    public override OnMessage(IMessage message)
    {
        this.InvokeToAll(message);
    }
}

In the Generic Controller we override the OnMessage method which means that any call to the Controller that does not match a method will end up in the OnMessage method.

You can of course use the IMessage parameter in regular methods on the Controller and not just the OnMessage method.

###Return data synchronously to caller When you call the server from a client and want to wait for the result you just replace void with the type you want to return and the client API's will wait for the result.

Server

public string Echo()
{
    return "Echo";
}

Note: You can also return Task<T>

You can of course send data before doing the return, for example.

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;
public string Echo()
{
    this.InvokeToAll("someData", "someMethod");
    //and so on...
    return "Echo";
}

Clients

Client - JavaScript

conn.controller('chat').invoke('echo').then(function(d){console.log(d)});

Client - C#

var echo = conn.Controller("chat").Invoke<string>("echo");
Console.WriteLine(echo.Result);
⚠️ **GitHub.com Fallback** ⚠️