我目前正在试验WAMP协议的WampSharp实现。
当客户端连接到consol时,我希望代码在consol上打印一条消息。所以我创建了一个路由器和一个客户端。但是消息不会出现在控制台中。这是我的密码:
路由器
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标签放在这个问题上。
发布于 2015-08-07 19:57:17
您不需要在路由器端创建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);https://stackoverflow.com/questions/31876679
复制相似问题