Giter VIP home page Giter VIP logo

Comments (4)

1iveowl avatar 1iveowl commented on May 30, 2024

Thank you for raising this. The documentation is not clear on this and I will update it.

What you need to do is to listen for the exception in your subscription. Like this:

        _subscribeToMessagesReceived = websocketClient.ObserveTextMessagesReceived.Subscribe(
            msg =>
            {
                System.Console.WriteLine($"Reply from test server: {msg}");
            },
            ex =>
            {
                System.Console.WriteLine(ex.Message);
            },
            () =>
            {
                System.Console.WriteLine($"Subscription Completed");
            });

In the above the subscription listens to three things:

  1. The messages
  2. Exceptions
  3. Completion

This is the pattern used with Rx.

Please let me know if this helps?

from websocketclientlite.pcl.

brooksyott avatar brooksyott commented on May 30, 2024

It was very helpful, thank you.
The exception does come now, terrific.
If I leave the network disconnected what I see is:

  • I'll get one exception from the Rx pattern above. Subsequent sends do not seem to generate it as per the pattern above, and result in my code needing to catch it via try/catch.
  • when the network becomes available again, sends won't resume, they continue to fail
  • what I've done is caught the exception as per your code above, then prevented the code from doing future sends until I've reconnected by creating a new MessageWebSocketRx

It seems to work, but it's not pretty :).

Again, thank you so much for the quick reply today. It definitely helped

from websocketclientlite.pcl.

1iveowl avatar 1iveowl commented on May 30, 2024

Great to hear.

What you describe makes perfect sense.

When the connection fails you need to re-connect it before you try sending something new. Not sure you need to create a new instance of the MessageWebSocketRx object.

From the moment the connection fails to you receive an exception via Rx there is a time gap. In mine testing it was 20 seconds. This makes sense as the client don't know that the connection is gone. It has to timeout first.

from websocketclientlite.pcl.

1iveowl avatar 1iveowl commented on May 30, 2024

Regarding looking pretty. I would it something like this:

    static void Main(string[] args)
    {

        var outerCancellationSource = new CancellationTokenSource();

        Task.Run(() => StartWebSocketAsyncWithRetry(outerCancellationSource), outerCancellationSource.Token);

        System.Console.WriteLine("Waiting...");
        System.Console.ReadKey();
        outerCancellationSource.Cancel();

        _subscribeToMessagesReceived.Dispose();
    }

    private static async Task StartWebSocketAsyncWithRetry(CancellationTokenSource outerCancellationTokenSource)
    {
        while (!outerCancellationTokenSource.IsCancellationRequested)
        {
            var innerCancellationSource = new CancellationTokenSource();
            await StartWebSocketAsync(innerCancellationSource);

            while (!innerCancellationSource.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(10), innerCancellationSource.Token);
            }

            // Wait 5 seconds before trying again
            await Task.Delay(TimeSpan.FromSeconds(5), outerCancellationTokenSource.Token);
        }
    }

    private static async Task StartWebSocketAsync(CancellationTokenSource innerCancellationTokenSource)
    {
        using (var websocketClient = new MessageWebSocketRx())
        {
            _subscribeToMessagesReceived = websocketClient.ObserveTextMessagesReceived.Subscribe(
                msg =>
                {
                    // Msg receive code goes here
                },
                ex =>
                {
                    // Cancel the inner loop when exception
                    innerCancellationTokenSource.Cancel();
                },
                () =>
                {
                    // Cancel the inner loop when complete
                    innerCancellationTokenSource.Cancel();
                });
....

See example here

from websocketclientlite.pcl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.