Game Development Reference
In-Depth Information
Table 10-3. Server-Sent Events: Method of continuously sending data from a server to the browser
IE
Firefox
Safari
Chrome
Opera
Two versions back
7.0
5.0
4.0
12.0
11.0
Previous version
8.0
6.0
5.0
13.0
11.1
Current
7.0
5.1
14.0
11.5
9.0
Near future
8.0
15.0
12.0
6.0
Farther future
10.0
9.0
16.0
12.1
What benefits do Server-Sent Events offer?
The protocol works over HTTP, making adoption very easy and eliminating the need for a
dedicated server.
The reconnection is automatic.
Events can have numbers, and arbitrary events can be transmitted.
What are the drawbacks of Server-Sent Events?
e m in aw k is i i ti lity f t e t l: a r n i e t , t t
transmit anything using an open connection.
WebSocket
WebSocket is a completely new protocol that works over TCP, reducing overhead and increasing latency,
thanks to absence of HTTP, but requiring a server that supports it. One of the differences between
WebSocket and Server-Sent Events is that the former is bi-directional; that is, we can both receive and
transmit data in real time.
To connect to a server, you need to enter an address that starts with ws:// or wss:// for secure
connections. Listing 10-11 shows what the WebSocket API looks on the client side.
Listing 10-11. Simple example of creating a WebSocket connection; sending and receiving data from the server
var connection = new WebSocket('ws://sontan.name/stream/');
// When the connection is open, send some data to the server
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
// Log messages from the server
connection.onmessage = function (e) {
 
Search WWH ::




Custom Search