Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:46:49+00:00 2026-06-13T04:46:49+00:00

was trying to encapsulate a partial view to show feedback that i can push

  • 0

was trying to encapsulate a partial view to show feedback that i can push back to the client.

This Article shows a method of pushing back data using HTML5 Server-Sent events (SSE).

I noticed that if i opened up several browser tabs and then closed one i got exceptions as the logic didn’t remove the respective stream from the ConcurrentQueue. I amended the code as below

    private static void TimerCallback(object state)
    {
        StreamWriter data;
        Random randNum = new Random();
        //      foreach (var data in _streammessage)
        for (int x = 0; x < _streammessage.Count; x++)
        {
            _streammessage.TryDequeue(out data);
            data.WriteLine("data:" + randNum.Next(30, 100) + "\n");
            try
            {
                data.Flush();
                _streammessage.Enqueue(data);
            }
            catch (Exception ex)
            {
                // dont re-add the stream as an error ocurred presumable the client has lost connection
            }
        }
        //To set timer with random interval
        _timer.Value.Change(TimeSpan.FromMilliseconds(randNum.Next(1, 3) * 500), TimeSpan.FromMilliseconds(-1));

    }

I also had to amend the OnStreamAvailable member as the framework syntax had changed to the second parameter being a HttpContent rather than HttpContentHeaders

 public static void OnStreamAvailable(Stream stream, HttpContent headers, TransportContext context)

The problem now is i am still getting inconsistant behaviour if i add or remove clients i.e it times out when trying to initialise a new client. Does anyone have any ideas or more examples of using SSE with WinAPI and the correct “framework of methods” to handle disconnected clients

Cheers
Tim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:46:50+00:00Added an answer on June 13, 2026 at 4:46 am

    This article is actually an adaptation of my original article from May – http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/ (notice even variable names and port numbers are the same :-).

    It is a very valid point that you are raising, and detecting a broken connection is something that’s not very easy with this setup. The main reason is that while ASP.NET (the host) allows you to check a broken connection, there is no notification mechanism between ASP.NET (host) and Web API informing about that.

    That is why in order to detect a broken connection (disconnected client) you should really try writing to the stream, and catch any error – this would mean the client has been disconnected.

    I asked the same question to Brad Wilson/Marcin Dobosz/Damien Edwards at aspconf, and Damien suggested using HttpContext.Current.Response.IsClientConnected – so basically bypassing Web API and obtaining the connectivity info from the underlying host directly (however there is still a race condition involved anyway). That is really .NET 4. He also pointed an interesting way in which this problem could be avoided in .NET 4.5 using an async cancellation token. Frankly, I have never got around to test it, but perhaps this is something you should explore.

    You can see their response to this problem in this video – http://channel9.msdn.com/Events/aspConf/aspConf/Ask-The-Experts – fast forward to 48:00

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like
I'm trying to make a set of custom tags that encapsulate form elements (markup
I'm trying to write a class using soil to encapsulate SOIL calls. I can't
I was trying to implement a specialized collection that works like ObservableCollection to encapsulate
I'm trying to make a type which can type-safely encapsulate arbitrary types. I got
I'm trying to encapsulate a near query with a maxDistance in a MongoMapper backed
I'm trying to properly encapsulate a class A, which should only be operated on
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
I am trying to encapsulate the access of an XML configuration file within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.