Skip to content

Broadcasting from client to other clients etc.

Gerardo Sista edited this page Feb 15, 2019 · 6 revisions

SignalGo has a simple and well designed architecture to broadcast messages from client other client(s) or server to client(s). To achieve this you to manage broadcasting system server side. Clients don't have direct access to other clients. This is by design for security reason. That's why you must manage broadcasting system for clients server side.

In the section Server Provider Examples we can see that it's trivial to create broadcasting classes and services, Here all we need to do it's reverse service creation. For example, first in server services we first created interfaces and then classes implementing these interfaces and client just need these interfaces to call methods. Now, in broadcast service we first create interfaces and classes client side and server just need these interfaces to call client methods. Very easy! Let's see one example:

C# examples (shared interface service):

[SignalGo.Shared.DataTypes.ServiceContract("SignalGoTestClientService", ServiceType.ClientService, InstanceType = SignalGo.Shared.DataTypes.InstanceType.SingleInstance)]
    public interface ISignalGoClientMethods
    {
        void HelloSignalGo(string hello);
        Tuple<string> GetMeSignalGo(string value);
    }

Server side we just register this interface in connection start (full server connection sample):

static void Main(string[] args)
        {
            // create instance of server listener
            SignalGo.Server.ServiceManager.ServerProvider server = new SignalGo.Server.ServiceManager.ServerProvider();
            //register service class with implemented methods (not interfaces)
            server.RegisterServerService<TestServerModel>();
            //register client callback methods on server for broadcasting system
            server.RegisterClientService<ISignalGoClientMethods>();
            //start server provider (server address is vital for client connections)
            server.Start("http://localhost:1132/SignalGoTestService");
            //this code prevent windows closing.
            Console.ReadLine();
        }

Client side we need to implement the service interface and methods. For example:

    public class ClientCallback : ISignalGoClientMethods
    {
        public Tuple<string> GetMeSignalGo(string value)
        {
            Console.WriteLine("called GetMeSignalGo: " + value);
            return new Tuple<string>("GetMeSignalGo :" + value);
        }

        public void HelloSignalGo(string hello)
        {
            Console.WriteLine("called HelloSignalGo: " + hello);
        }
    }

The question now is: how we can call clients methods server side? We need to use OperationContext class to access client and its callbacks.

For example, server side:

            //get all clients and call interface methods
            foreach (var call in SignalGo.Server.Models.OperationContext.Current.GetAllClientCallbackList<ISignalGoClientMethods>())
            {
                //call GetMeSignalGo method
                var result = call.GetMeSignalGo("test");

                Console.WriteLine("GetMeSignalGo result: " + result.Item1);

                //call HelloSignalGo method
                call.HelloSignalGo("hello signalGo");

                Console.WriteLine("hello signalGo call Success");
            }

IMPORTANT: SignalGo.Server.Models.OperationContext.Current. MUST be in methods INSIDE service methods body to use it. If it's outside (in a class that is not a service, for example) it will return NULL value. In this case, if you want to access the client, for example, inside of new thread or outside service methods you can create instance manually like this:

SignalGo.Server.Models.OperationContext context = new SignalGo.Server.Models.OperationContext();
//server is your ServerProvider instance
context.ServerBase = server;

or:

//server is your ServerProvider instance
var allClients = server.GetAllClientServices<ISignalGoClientMethods>();

there are many ways to manage clients callbacks. For further informations please read OperationContext