没有调用WampSharp ConnectionEstablished回调

本文关键字:ConnectionEstablished 回调 WampSharp 调用 | 更新日期: 2023-09-27 18:06:46

我目前正在试用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标签。

没有调用WampSharp ConnectionEstablished回调

您不需要在路由器端创建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);
相关文章:
  • 没有找到相关文章