My server-sent events are not sending anymore. This use to work perfectly and now something has changed and it might be that I am running this as an ASP.Net 4 application. I'm using a pretty standard iHTTPRequest Handler with a loop, a little threading timeout, and around I go, writing messages like this:
A Message - Note: the reason for parsing the message the way I do below, is I wasn't sure if there was a max message length, so in order to elminate this as a potential problem I broke it out. Well forget this bit anyways, because I'm testing now with the most basic message "alive" so it never hits this.
public static void WriteClientMessage(this HttpResponse Response, string Message)
{
if (Message.Length > 50)
{
int start = 0;
int length = 50;
while (length > 0)
{
Response.Write("data: {0}\n", Message.Substring(start, length));
start = start + length;
length = Message.Substring(start, (Message.Substring(start).Length >= length) ? length : Message.Substring(start).Length).Length;
}
}
else
{
Response.Write("data: " + Message);
Response.Write("\n");
}
Response.Write("\n");
Response.Flush();
}
An Event
public static void WriteClientEvent(this HttpResponse Response, string EventName)
{
Response.Write("event: {0}\n", EventName.TrimNewLine());
Response.Write("data: \n\n");
Response.Flush();
}
Something else to note and I think this is really important is that eventually the message do come, but they come all at once, as if they are buffered. So I've played with this, both enable and disable buffering. I've even investigated to make sure that my dynamic content isn't being compressed, although I'm not confident I have this right, but pretty sure that it isn't. I will investigate this further if someone says that for sure it is and this is my problem
So what is it? Is this a .Net 4ism?
**UPDATE - Insert Minimal Sample here - **
I've added a sample of the script here: http://live.meetscoresonline.com/test-sse.aspx This app loops 10 times and then breaks out of the loop. This is the actual SSE service, so you should get the result directly to your browser, at least in chrome, FF tries to download the file, and haven't tested the other browsers. What I expect to see is data: alive to apear in the browser as it happens, instead nothing appears until the script terminates and then all data: alive events appear. I've tested SSE this way before so I know this works (or use to work). Here is this test code. Am I missing something?
UPDATE: I commented out the break to demonstrate a new behavior. If you wait about 20 or 30 loops, the real-time streaming begins and continues as expected. So what is this initial pause?
public class TestSSE : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/event-stream";
var itt = 0; ;
while (true)
{
if (!context.Response.IsClientConnected)
break;
/* **** UPDATE: REMOVED THE BREAK. STREAMING BEGINS AFTER ABOUT THE 20TH OR 30TH LOOP ****/
// if (itt++ > 10)
// break;
context.Response.Write("data: alive {0}\n\n", DateTime.Now.ToLongTimeString());
context.Response.Flush();
System.Threading.Thread.Sleep(2000);
}
}
}