HTML and CSS Reference
In-Depth Information
document.getElementById("stop").onclick = function () {
if (worker) {
We also need a function for the Start button, an error event handler, and a function for the Stop button.
The function that actually performs the counting takes place in an external .js file (Listing 6-18).
Listing 6-18. The webworker.js File
onmessage = function (evt) {
for (var i =, t = 10000; i < t; i++) {
The Web Workers API is supported by IE10+, Firefox 3.5+, Chrome 5.0+, Safari 4.0+, and Opera 10.6+.
The HTML5 WebSocket API
The WebSocket API can be used for bidirectional, full-duplex communication over a Transmission Control Protocol
(TCP) socket.
The WebSocket API is being standardized by the World Wide Web Consortium [15].
After building a WebSocket connection with the web server, data can be retrieved from the server using the
onmessage event handler and can be sent from the client to the server by the send() method.
A new WebSocket object can be created as shown in Listing 6-19.
Listing 6-19. A New WebSocket Object
var Socket = new WebSocket(;
Optionally, the protocol can also be specified after the URI.
The WebSocket object has two read-only attributes: Socket.readyState and Socket.bufferedAmount . The first
one represents the connection state ( 0 is no connection yet, 1 is connection has been built, 2 is closing handshake, 3 is
connection closed or cannot be established). The second attribute gives the number of bytes queued using the send()
The WebSocket API supports four events: open (socket connection established), message (client receives data
from server), error (error in communication), and close (the connection is closed). They can be handled by the
Socket.onopen , Socket.onmessage , Socket.onerror , and Socket.onclose event handlers, respectively.
The two methods of WebSocket are Socket.send() (the send() method transmits data through the connection),
and Socket.close() (the close() method is used to terminate the existing connection).
As an example, we create a bidirectional TCP socket between the client and the server in the document head
(Listing 6-20).
Search WWH ::

Custom Search