Oyatel / CometD.NET

CometD.NET is a C# client library for the Bayeux protocol
43 stars 34 forks source link

Reconnection issue #3

Closed srkamineni closed 13 years ago

srkamineni commented 13 years ago

After the server goes down, the .net client is not reconnecting at all. My understanding was that it clients will try to reconnect after the server goes down.

Or is there any thing that we can do to reconnect.

Thanks Raj

grEvenX commented 13 years ago

Thanks for the observation Raj. This is the responsibility of the library for sure, and I'm pretty sure we tested this well on our end. How are you able to confirm that it does not "reconnect" ? It should reconnect to the server, but the question might be what is happending for subscribtions etc.

srkamineni commented 13 years ago

Well, after the server went down and came back up, the client never received any messages from the server. I had to restart my .net client to connect back again and start getting the messages.

I am wondering if I am missing some addition of a listener - like disconnect and then connect again. What do you suggest.

Raj

guruprasad83 commented 6 years ago

@srkamineni - Even am facing the same issue - Well, after the server went down and came back up, the client never received any messages from the server. I had to restart my .net client to connect back again and start getting the messages. Did you get any solution for this? Am also using .net client

ohaucke commented 6 years ago

@guruprasad83 I've implemented an extension which raises an event if the connection got closed by the server. You could check the message in rcvMeta and modify the code to match your response and raise the event.

using Cometd.Bayeux;
using Cometd.Bayeux.Client;
using System;

namespace Foobar.Utilities
{
    public class LoggingExtension : IExtension
    {
        private static NLog.Logger logger = NLog.LogManager.GetLogger("Foobar");

        public event EventHandler ConnectionError;

        public bool rcv(IClientSession session, IMutableMessage message)
        {
            return true;
        }

        public bool rcvMeta(IClientSession session, IMutableMessage message)
        {
            if (message.Successful)
            {
                logger.Debug(message.JSON);
            }

            if (message.ContainsKey("exception"))
            {
                logger.Fatal(message["exception"]);
            }

            if (message.ContainsKey("error"))
            {
                logger.Error(message["error"]);
                if (message["error"].ToString().ToLower() == "403::unknown client")
                {
                    this.OnConnectionError();
                }
            }

            return true;
        }

        public bool send(IClientSession session, IMutableMessage message)
        {
            return true;
        }

        public bool sendMeta(IClientSession session, IMutableMessage message)
        {
            return true;
        }

        private void OnConnectionError()
        {
            this.ConnectionError?.Invoke(this, new EventArgs());
        }
    }
}

Afterwards you need to add the extension to your bayeuxClient and subscribe to the event where you can re-establish the connection

LoggingExtension loggingExtension = new LoggingExtension();
loggingExtension.ConnectionError += loggingExtension_ConnectionError;
bayeuxClient.addExtension(loggingExtension);
swagathchandra commented 3 years ago

@ohaucke - Can you please share code for extension. I am facing the same issue

ohaucke commented 3 years ago

@swagathchandra I already posted it (LoggingExtension)

nahoj28 commented 3 years ago

Hi @ohaucke , Thanks for the information. It is very helpfull for me, however I'm not sure of understand what is the steps to add into the EventHanlder (loggingExtension_ConnectionError). Should I connect to the instance again into the handler? or into the hanlder only is needed subscribe to the channels again?

ohaucke commented 3 years ago

Hi @nahoj28, sorry that is way in the past, so i don't remember it but from the code, i would say you need to completely reconnect because the accesstoken isn't valid anymore.