Skip to content

Instantly share code, notes, and snippets.

@darkl
Created January 19, 2017 20:47
Show Gist options
  • Save darkl/28380eced333ac8ff0fcc4df6cb2f21e to your computer and use it in GitHub Desktop.
Save darkl/28380eced333ac8ff0fcc4df6cb2f21e to your computer and use it in GitHub Desktop.
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();
}
}
}
@StevenBonePgh
Copy link

If you make this alteration by making the task not async, the reconnect issue happens.

            Func<Task> connect = () =>
            {
                logger.Info("Open() start");
                try
                {
                    channel.Open().Wait();
                }
                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");
                return Task.FromResult(0);
            };

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 than System.Threading.Thread.Sleep(5000);?

Thank you for going the extra mile on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment