[en] Two way communication (solved)

Topics: Technical Support
Nov 1 at 1:16 PM
It looks like Zyan has two way communication between server and client because the server is able to trigger events, and the client's proxy can subscribe to those events. Does that mean that the only server-to-client communication can happen through events? Or can the server call a method (on the interface) as well?
Coordinator
Nov 12 at 8:54 PM
Edited Nov 12 at 9:14 PM
Terribly sorry for my late reply. The codeplex site stopped sending me notifications for the new threads, what a shame...
 
Does that mean that the only server-to-client communication can happen through events?
 
Yes, once the client subscribed to the server's event, the server is able to trigger the event at any time.
 
Or can the server call a method (on the interface) as well?
 
There are two options to achieve this.
  • First option: pass a client's delegate to the server. The server stores the delegate locally and then calls it as needed
  • Second option: start ZyanComponentHost on the client side and have server connect back to the client.
Please let me know if you need a code sample.
Nov 18 at 12:39 PM
Thanks for the reply. A code sample of the delegate approach would be helpful.
Coordinator
Nov 18 at 7:45 PM
Edited Nov 18 at 7:45 PM
Hello Mark,

Here is the complete sample: https://gist.github.com/yallie/d39a2e81106dc5572f1e2b5f519510eb

Below is the essential part:
// shared interface
public interface IService
{
    void RegisterCallback(Action callback);

    void DoCallback();
}

// server-side class code
public class Service : IService
{
    private static ConcurrentDictionary<Guid, Action> Callbacks { get; } =
        new ConcurrentDictionary<Guid, Action>();

    private Guid ClientID
    {
        get { return ServerSession.CurrentSession.SessionID; }
    }

    public void RegisterCallback(Action callback)
    {
        // register my delegate
        Callbacks[ClientID] = callback;
        Console.WriteLine("Client {0} registered the delegate.", ClientID);
    }

    public void DoCallback()
    {
        Action action;
        if (Callbacks.TryGetValue(ClientID, out action))
        {
            // call my delegate, if registered
            action();
            Console.WriteLine("Calling client {0}'s delegate.", ClientID);
        }
    }
}
Hope that helps.

Regards, Alex.