What is Server-Side Code in HTML5

In this article I have describe about HTML5 SSC.
  • 2713

Server-Side Code Example

  • We need a server sending data updates ASP or PHP in this Example.
  • In this example above to work, we need a server capable of sending data updates (like PHP or ASP).
  • Syntax of SSE issimple, Set the "Content-Type" header to "text/event-stream".

Code in PHP (Server.php):

if ($_SERVER['HTTP_ACCEPT'] === 'text/event-stream')

{  

    header('Content-Type: text/event-stream');  

    header('Cache-Control: no-cache');

    echo "data: This is the first event\n\n";

    flush();

    $i = 5;

    while (--$i)

 {

        sleep(1);

        $time = date('r');

        echo "data: The server time is: {$time}\n\n";

        flush();

    }

}

 

Code in ASP (VB) (Server.asp):

 

<%
Response.ContentType="text/event-stream"
Response.Expires=-1
Response.Write("data: " & now())
Response.Flush()
%>

 

Output

 Clipboard01.jpg


Define the code

  • "Content-Type" header to "text/event-stream" is created.
  • Page should not cache in this server.asp.
  • "data" start to send the data and the output data back to the page.

Further Readings

You may also want to  read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.