0

我目前正在尝试 WAMP 协议的 WampSharp 实现。

我希望代码在客户端连接到控制台时在控制台上打印一条消息。所以我创建了一个路由器和一个客户端。但是该消息不会出现在控制台中。这是我的代码:

路由器

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");
    }


}

客户:

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();
    }
}

这可能是 C# 问题而不是 WampSharp 问题,但以防万一我将两个 wamp 标签放在这个问题上。

4

1 回答 1

1

您不需要在路由器端创建 WampChannel。您应该订阅领域事件:

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");
}

如果您有兴趣在客户端检测通道的连接/断开连接,您可以订阅您提到的事件:

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);
于 2015-08-07T19:57:17.697 回答