Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHAP 6. WEBSOCKET API.  In many cases (stock prices, news reports …) which you want get real-time information, you can constantly refresh that page."— Presentation transcript:

1 CHAP 6. WEBSOCKET API

2  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

3  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

4 THE WEBSOCKET HANDSHAKE

5

6 A DRAMATIC REDUCTION IN UNNECESSARY NETWORK TRAFFIC AND LATENCY

7 LATENCY COMPARISION

8  Browser support for HTML5 WebSocket  Checking for Browser Support  if (window.WebSocket) { // support Websocket } else { // not support } BROWSER SUPPORT BrowserDetails ChromeSupported in version 4+ FirefoxSupported in version 4+ Internet ExplorerNot supported yet OperaNot supported yet SafariSupported in version 5+

9  Creating a WebSocket  url = "ws://localhost:8887";  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

10  A Simple Java WebSocket Server  https://github.com/TooTallNate/Java-WebSocket https://github.com/TooTallNate/Java-WebSocket  Compile WebSocket Server Example  ant  Run WebSocket Server  java -cp build/examples:dist/java_websocket.jar ChatServer WEBSOCKET SERVER

11  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("click", connectServer);  document.getElementById("sendMsgButton").addEventListener("click", sendMessage); A WEBSOCKET CHATROOM

12


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

Similar presentations


Ads by Google