10/11/2011 Development / java script

Agora sim tá ficando interessante!!!

        <script type="text/javascript">

            function Initialize() {
                if (window.EventSource == undefined) {
                    alert("Seu Browser não suporta Server Side Events.");
                    return;
                }

                var source = new EventSource('httpPublisher.aspx');
                var doc = document;
                var pub = doc.getElementById('PublishMessages');
               
                source.onopen = function(event) {
                    pub.innerHTML += "<font color='green'>Conectado</font><br>";
                };

                source.onerror = function(event) {
                    if (event.eventPhase == EventSource.CLOSED) {
                        pub.innerHTML += "<font color='red'>Desconectado</font><br>";
                    }
                };

                source.onmessage = function(event) {
                    pub.innerHTML += event.data + '<br>';
                };
            }
        </script>
        <div id="PublishMessages"></div>
        <button onclick="Initialize()">Iniciar Conexão</button>

/* httpPublisher.aspx */

        DateTime startDate = DateTime.Now;
        Response.ContentType = "text/event-stream";

        while (startDate.AddMinutes(1) > DateTime.Now)
        {
            Response.Write(string.Format("data: {0}\n\n", DateTime.Now.ToString()));
            Response.Flush();

            System.Threading.Thread.Sleep(1000);
        }
        Response.Close();

Espero que sirva!