WampSharp ConnectionEstablished callback not being called

992 views Asked by At

I'm currently trying out the WampSharp implementation of the WAMP protocol.

I wanted the code to print a message on the consol when a client connects to it. So I created a router and a client. But the message doesn't appear in the console. Here's my code:

Router

class Program
{
    static void Main(string[] args)
    {

        const string location = "ws://127.0.0.1:8080/";
        const string realmName = "realm1";

        Task runTask = Run(location, realmName);

        Console.ReadLine();
    }

    private async static Task Run(string wsuri, string realmName)
    {
        using (IWampHost host = new DefaultWampHost(wsuri))
        {
            IWampHostedRealm realm = host.RealmContainer.GetRealmByName(realmName);
            host.Open();

            DefaultWampChannelFactory factory = new DefaultWampChannelFactory();
            IWampChannel channel = factory.CreateJsonChannel(wsuri, realmName);

            IWampClientConnectionMonitor monitor = channel.RealmProxy.Monitor;
            monitor.ConnectionError += ConnectionError;
            monitor.ConnectionEstablished += ConnectionEstablished;

            Console.WriteLine("Server is running on " + wsuri);

            while(true)
            {
                await Task.Delay(TimeSpan.FromSeconds(1))
                .ConfigureAwait(false);
            }
        }
    }

    private static void ConnectionEstablished(object sender, WampSessionEventArgs e)
    {
        Console.WriteLine("A client as connected");
    }

    private static void ConnectionError(object sender, WampConnectionErrorEventArgs e)
    {
        Console.WriteLine("A connections error occured");
    }


}

client:

class Program
{
    static void Main(string[] args)
    {
        const string location = "ws://127.0.0.1:8080/";

        DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

        IWampChannel channel = channelFactory.CreateJsonChannel(location, "realm1");
        IWampRealmProxy realmProxy = channel.RealmProxy;

        channel.Open().Wait();

        Console.ReadLine();
    }
}

It's probably a C# problem rather than a WampSharp problem, but just in case I put the two wamp tags on this question.

1

There are 1 answers

0
darkl On BEST ANSWER

You don't need to create a WampChannel in the router-side. You should subscribe to realm events instead:

private static void Run(string wsuri, string realmName)
{
    using (IWampHost host = new DefaultWampHost(wsuri))
    {
        IWampHostedRealm realm = host.RealmContainer.GetRealmByName(realmName);
        realm.SessionCreated += SessionCreated;
        realm.SessionClosed += SessionRemoved;

        host.Open();

        Console.WriteLine("Server is running on " + wsuri);

        Console.ReadLine();
    }
}

private static void SessionCreated(object sender, WampSessionEventArgs wampSessionEventArgs)
{
    Console.WriteLine("Client connected");
}

private static void SessionRemoved(object sender, WampSessionCloseEventArgs wampSessionCloseEventArgs)
{
    Console.WriteLine("Client disconnected");
}

If you're interested in detecting connection/disconnection of channel in client side, you can subscribe to the events you mentioned:

const string location = "ws://127.0.0.1:8080/";
const string realm = "realm1";

DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

IWampChannel channel = channelFactory.CreateJsonChannel(location, realm);
IWampClientConnectionMonitor monitor = channel.RealmProxy.Monitor;
monitor.ConnectionEstablished += ConnectionEstablised;
monitor.ConnectionError += ConnectionError;
monitor.ConnectionBroken += ConnectionBroken;

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