Imports System.Threading

' THIS PAGE SHOULD BE LOADED INTO A HTML IFRAME THAT HAS BEEN ADDED
' TO THE HTML FILE TO WHICH WE ARE PUSHING CONTENT

Partial Class PushServer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim pictures(2) As String
        Dim pictureInfo(2) As String
        Dim pic As Integer

        ' 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
        '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("<html xmlns='http://www.w3.org/1999/xhtml' >")
        Response.Write("<head>")
        Response.Write("</head>")

        Response.Write("<body>")

        ' Loop forever pushing content to the HTML page
        Do While True
            ' Every time around the loop push more content to the
            ' parent page by calling it's function
            Response.Write("<script type='text/javascript' language='javascript'>")
            Response.Write("parent.heartbeat();")
            Response.Write("parent.newpicture('" & pictures(pic) & "', '" & pictureInfo(pic) & "');")
            Response.Write("</script>")
            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.
            Thread.Sleep(5000)

            ' Just for simulation, here we just pick a new picture
            If pic + 1 < 3 Then
                pic = pic + 1
            Else
                pic = 0
            End If
        Loop

        ' 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

        Response.Write("</body>")
        Response.Write("</html>")
    End Sub
End Class
