chap 5. websocket api. in many cases (stock prices, news reports …) which you want get real-time...

10
CHAP 5. WEBSOCKET API

Upload: cameron-riley

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

CHAP 5. WEBSOCKETAPI

Page 2: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page. But that’s obviously not a great solution.

With polling, the browser sends HTTP requests at regular intervals and immediately receives a response. Making unnecessary requests inevitable and as a result, many

connections are opened and closed needlessly in low-message-rate situations.

With long-polling, the browser sends a request to the server and the server keeps the request open for a set period of time. When you have a high message-volume, long-polling does not

provide any substantial performance improvements over traditional polling.

REAL-TIME AND HTTP

Page 3: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

With streaming, the browser sends a complete request, but the server sends and maintains an open response that is continuously updated and kept open indefinitely (or for a set period of time). Streaming is still encapsulated in HTTP, intervening

firewalls and proxy servers may choose to buffer the response increasing the latency of the message delivery.

All of these methods for providing real-time data involve HTTP request and response headers, which contain lots of additional, unnecessary header data and introduce latency

REAL-TIME AND HTTP

Page 4: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

THE WEBSOCKET HANDSHAKE

Page 5: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

A DRAMATIC REDUCTION IN UNNECESSARY NETWORK TRAFFIC AND

LATENCY

Page 6: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

LATENCY COMPARISION

Page 7: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

Browser support for HTML5 WebSocket

Checking for Browser Support if (window.WebSocket) {

// support Websocket} else { // not support}

BROWSER SUPPORT

Browser Details

Chrome Supported in version 4+

Firefox Supported in version 4+

Internet Explorer Not supported yet

Opera Not supported yet

Safari Supported in version 5+

Page 8: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

Creating a WebSocket url = "ws://localhost:8080/echo"; w = new WebSocket(url);

Add Event Listeners w.onopen = function() {

log("open");w.send("thank you for accepting this websocket request");

} w.onmessage = function(e) {

log(e.data);}

w.onclose = function(e) {log("closed");

} Sending Message

document.getElementById("sendButton").onclick = function() {w.send(document.getElementById("inputMessage").value);

}

BASIC API USAGE

Page 9: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

A Simple Java WebSocket Server https://github.com/TooTallNate/Java-WebSocket

Compile WebSocket Server Example javac -cp build/examples;dist/java_websocket.jar

src/main/example/ChatServer.java java -cp build/examples;dist/java_websocket.jar;src/main/example

ChatServer

WEBSOCKET SERVER

Page 10: CHAP 5. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page

function connectServer() { var url = "ws:// localhost:8887"; socket = new WebSocket(url); socket.onopen = function() { updateSocketStatus("Connected to WebSocket server"); } socket.onmessage = function(e) { updateSocketStatus("Update message: " + e.data); } } function sendMessage() { var message = document.getElementById("sendMessage").value; socket.send(message); } document.getElementById("connectButton").addEventListener("cl ick",

connectServer); document.getElementById("sendMsgButton").addEventListener("cl ick",

sendMessage);

A WEBSOCKET CHATROOM