Testament service

The Testament feature is supported.

This feature allows a client to request the router to publish some predefined events when the client disconnects.

Enabling the testament service on the router-side

In order to enable it from router-side, call HostTestamentService extension method of the relevant realm (this is similiar to the Router-side meta-api usage):

DefaultWampHost host = new DefaultWampHost("ws://127.0.0.1:8080/ws");

IWampHostedRealm realm = host.RealmContainer.GetRealmByName("realm1");

IDisposable disposable = realm.HostTestamentService();

// Uncomment to unregister the Testament service. 
// disposable.Dispose(); 

host.Open();

Consuming the testament service

In order to consume the Testament service, one can define its contract and consume it using the CalleeProxy method. Another option is to use the GetTestamentServiceProxy extension method of IWampRealmProxy which returns such a proxy:

private static async Task Main()
{
    WampChannelFactory channelFactory = new WampChannelFactory();

    IWampChannel channel =
        channelFactory.ConnectToRealm("realm1")
                      .WebSocketTransport(new Uri("ws://127.0.0.1:8080/ws"))
                      .JsonSerialization()
                      .Build();

    TaskCompletionSource<long> sessionIdTask = new TaskCompletionSource<long>();

    channel.RealmProxy.Monitor.ConnectionEstablished += (sender, args) => sessionIdTask.SetResult(args.SessionId);

    await channel.Open().ConfigureAwait(false);

    long sessionId = await sessionIdTask.Task.ConfigureAwait(false);

    IWampTestamentServiceProxy proxy = channel.RealmProxy.GetTestamentServiceProxy();

    string testamentTopic = "com.example.testament";

    for (int i = 0; i < 5; ++i)
    {
        object[] testamentArgs = new object[] {"this is my testament!"};

        Dictionary<string, object> testamentKwArgs =
            new Dictionary<string, object> {{"foo", i}, {"bar", "baz"}, {"sess", sessionId}};

        try
        {
            await proxy.AddTestamentAsync(testamentTopic, testamentArgs, testamentKwArgs)
                       .ConfigureAwait(false);

            Console.WriteLine("testament added");
        }
        catch (Exception ex)
        {
            Console.WriteLine("adding testament failed! " + ex);
        }
    }

    Console.WriteLine(@"Press any key to close channel. 5 events should be published to ""com.example.testament""");

    // This line is required in order to release the WebSocket thread, otherwise it will be blocked by the following Console.ReadKey() line.
    await Task.Yield();

    Console.ReadKey();
    channel.Close();
}

This sample is based on this Crossbar example.