


A brief analysis of HTML5's WebSocket and server push events_html5 tutorial skills
WebSockets
Web Sockets is a new generation of two-way communication technology for web applications, running on a single socket, which is exposed in HTML5-compatible browsers through a JavaScript interface.
Once you have obtained the Web Socket connection on the web server, you can send data from the browser to the server by calling the send() method, and receive data from the server to the browser through the onmessage event handler.
The following is the API to create a new WebSocket object.
- var Socket = new WebSocket(url, [protocal] );
The first parameter url is used to specify the URL to connect to. The second attribute - port is optional and, if provided, specifies a subprotocol that the server must support for a successful connection.
WebSocket Properties
The following are the properties of the WebSocket object. Assume we have created the above Socket object:
Attribute | Description | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Socket.readyState |
readyState represents the status of the connection. It has the following values:
1 means the connection is established and communication is possible.
|
|||||||||||||||||||||||||||
Socket.bufferedAmount | Read-only attributebufferedAmountRepresents the number of URF-8 text bytes that have been queued using the send() method. |
Method | Description |
---|---|
Socket.send() | The send(data) method uses a connection to transfer data. |
Socket.close() | The close() method is used to terminate any existing connection. |
Server Push Events
Traditional web applications generate events that are sent to the web server side. For example, clicking a link will request a new page from the server.
This type of time from the web browser to the web server can be called a client-side event.
With the advent of HTML5, WHATWG Web Applications 1.0 introduced an event stream from the web server to the web browser called Server Push Events (SSE). Use SSE to continuously push DOM events to the user's browser.
This event streaming method will open a persistent connection to the server and send data to the client when new messages are available, eliminating the need for continuous polling.
SSE Web Application
To use server push events in a web application, we need to add an
This URL will point to a PHP, PERL or any Python script that continuously sends event data. Here is a simple example of a web application that expects the server time.
- >
- <html>
- <head>
- <script type="text/ javascript">
- /* Define event handling logic here */
- script>
- head>
- <body>
- <div id="sse" >
- <eventsource src="/cgi -bin/ticker.cgi" />
- div>
- <div id="ticker" >
- <TIME>
- div>
- body>
- html>
SSE server-side script
Server-side script should send Content-type header specifying the type as text/event-stream, as shown below:
After setting the Content-type, the server-side script will send an Event: tag followed by the event name. The following example will send a newline-terminated Server-Time as the event name.
The last step is to send the event data using the Data: tag, followed by the integer string value terminated with a newline, like this:
print " Data: $timen";
The following is the complete ticker.cgi written in perl:
#!/usr/bin/perl
print "Content-Type: text/event-streamnn";
while(true){
print "Event: server-timen";
$time = localtime();
print " Data: $timen";
sleep(5);
Handling server push events
Let’s modify our web application to handle server push events. Here is the final example:
- >
- <html>
- <head>
- <script type="text/javascript">
- document.getElementsByTagName("eventsource")[0].
- addEventListener("server-time", eventHandler, false);
- function eventHandler(event)
- {
- // Alert time sent by the server
- document.querySelector('#ticker').innerHTML = event.data;
- }
- script>
- head>
- <body>
- <div id="sse">
- <eventsource src="/cgi-bin/ticker.cgi" />
- div>
- <div id="ticker" name="ticker">
- [TIME]
- div>
- body>
- html>
在测试服务器推送事件之前,建议你确保你的 Web 浏览器支持这一概念。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
