-
-
Save darkl/28380eced333ac8ff0fcc4df6cb2f21e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Reactive.Linq; | |
using System.Reactive.Subjects; | |
using System.Threading.Tasks; | |
using log4net; | |
using log4net.Config; | |
using log4net.Core; | |
using WampSharp.V2; | |
using WampSharp.V2.Client; | |
using WampSharp.V2.MetaApi; | |
namespace Sample | |
{ | |
class Program | |
{ | |
private static ILog logger = LogManager.GetLogger("MyLogger"); | |
static void Main(string[] args) | |
{ | |
XmlConfigurator.Configure(new FileInfo("log4net.config")); | |
Run(); | |
Console.ReadLine(); | |
} | |
private static void Run() | |
{ | |
DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory(); | |
IWampChannel channel = | |
channelFactory.CreateJsonChannel("ws://127.0.0.1:8080/ws", | |
"realm1"); | |
channel.RealmProxy.Monitor.ConnectionEstablished += | |
(sender, args) => | |
{ | |
logger.Debug("connected session with ID " + args.SessionId); | |
dynamic details = args.WelcomeDetails.OriginalValue.Deserialize<dynamic>(); | |
logger.DebugFormat("authenticated using method '{0}' and provider '{1}'", details.authmethod, | |
details.authprovider); | |
logger.DebugFormat("authenticated with authid '{0}' and authrole '{1}'", details.authid, | |
details.authrole); | |
}; | |
channel.RealmProxy.Monitor.ConnectionBroken += (sender, args) => | |
{ | |
logger.DebugFormat("Disconnected"); | |
//dynamic details = args.Details.OriginalValue.Deserialize<dynamic>(); | |
//Console.WriteLine("disconnected " + args.Reason + " " + details.reason + details); | |
}; | |
IWampRealmProxy realmProxy = channel.RealmProxy; | |
Func<Task> connect = async () => | |
{ | |
logger.Info("Open() start"); | |
try | |
{ | |
await channel.Open(); | |
} | |
catch (Exception ex) | |
{ | |
logger.Error("Open().Wait() failed", ex); | |
// Adding the sleep here will cause connect not to happen | |
System.Threading.Thread.Sleep(5000); | |
throw; | |
} | |
logger.Info("Open() done"); | |
}; | |
WampChannelReconnector reconnector = new WampChannelReconnector(channel, connect); | |
reconnector.Start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you make this alteration by making the task not
async
, the reconnect issue happens.In any case, refactoring the whole of the post-channel open configuring to use TPL async/await appears to resolve my problems, even though I don't understand why...
If we want to actually do a sleep, are we better with
await Task.Delay(5000);
rather thanSystem.Threading.Thread.Sleep(5000);
?Thank you for going the extra mile on this one.