我目前正在尝试 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 标签放在这个问题上。