HTML and CSS Reference
In-Depth Information
}
function OnAbort(evt) {
alert(“File Upload Aborted!”);
}
The OnProgress() event-handler function receives an event parameter of type ProgressEvent that
provides progress information about the upload operation. The lengthComputable property returns a
Boolean value indicating whether the progress of the operation can be determined. The loaded and total
properties indicate the number of bytes uploaded and the total number of bytes to be uploaded. Based on
these two values, a progress percentage is calculated. The value attribute of the <progress> element is set
to this calculated value using the jQuery attr() method.
The other event-handler functions are straightforward and simply display a message (success, error,
or cancellation) to the user.
Notifying the Browser Using Server-Sent Events
So far in this topic you've been using techniques that initiate communication from the client to the server
( $.ajax() or XMLHttpRequest , for example). In such client-to-server techniques, once a request is sent and
a response is received from the server, the underlying communication channel is closed. Consider a case
where a server is continuously performing a business operation. A web page displays the status of the
processing for the user. Because the operation is continuing on the server, you want that status to be
periodically updated. How do you accomplish this task? A common approach is to poll the server
periodically and retrieve the status of the operation. You can use functions such as setTimeout() and
setInterval() and make a request to the server in an attempt to retrieve the operation's status.
The disadvantage of this polling technique is that there are too many request-response cycles. The
client keeps sending requests, and the server keeps responding to every request. Each request-response
cycle needs its own communication channel, which is closed once the cycle is complete. Wouldn't it be
nice if the server notified you when something interesting happened, without any need for polling? This is
what server-sent events allow you to do.
As the name suggests, server-sent events are dispatched by the server. Instead of the client
periodically checking the server for updates, the server notifies the client if anything interesting happens
on the server. Server-sent events use a common communication channel to send multiple notifications,
thus avoiding continuous request-response cycles. The server-side resource that sends the notifications to
the client is wrapped in an EventSource object. The open , message , and error events of the EventSource
object are then used to open a communication channel, receive messages from the server, and deal with
the errors, respectively.
Let's develop an application that illustrates how to use server-sent events. Figure 11-6 shows the
application's main web form.
 
Search WWH ::




Custom Search