using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; // Encoding using System.Threading; namespace BasicAjaxCSharp { public partial class PushServer : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string[] pictures = new string[3]; string[] pictureInfo = new string[3]; int pic = 0; // Create an array of images and information pictures[0] = "boat.gif"; pictures[1] = "car.gif"; pictures[2] = "train.gif"; pictureInfo[0] = "A boat"; pictureInfo[1] = "A car"; pictureInfo[2] = "A train"; pic = 0; // Set the content type Response.ContentType = "text/plain"; Response.ContentEncoding = Encoding.UTF8; // Prevent page from being cached when we are polling //Response.Expires = -1; // Create the HTML for this push page // We need to call the heartbeat function of the // parent page using this server page. Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); // Loop forever pushing content to the HTML page while (true) { // Every time around the loop push more content to the // parent page by calling it's function Response.Write(""); Response.Flush(); // This example is just for simulation, so we just wait 5 seconds // before sending more data. Normally, this page would be // monitoring something or fetching new data for display. // In theory this works, in practice the web server times out to protect // web pages looping forever and killing the servers performance. // So in effect I'm cheating and allowing the timeout to happen, which // will also cause a reload but the principle idea is illustrated here. Thread.Sleep(5000); // Just for simulation, here we just pick a new picture if (pic + 1 < 3) { pic = pic + 1; } else { pic = 0; } } // THIS PAGE ACTUALLY NEVER ENDS BUT LOOPS FOREVER // HOWEVER, THE WEBSERVER WILL EVENTUALLY TIMEOUT THE // PAGE BECAUSE IT TRYS TO PREVENT BROKEN CODE FROM // KILLING THE WEBSERVER // CALLING PAGE MUST CHECK FOR THIS AND RELOAD THIS PAGE // Never gets here //Response.Write(""); //Response.Write(""); } } }