Web Socket
Client/Server communications
Polling Vs LongPolling Vs Streaming
HTTP half-duplex
HTTP Overhead-less efficient ...
To create desktop-like applications
in the browser
● Developers used hacks in servers to keep connections open longer
● Op...
WebSockets give you the...
● Ability to utilize an upgraded HTTP request
● Sends data in a message-based way similar to
UD...
WebSocket is an...
● Event-driven, full duplex, asynchronous communications channel with single TCP
Connection
● Primary b...
Events
●
Open
ws.onopen = function(e) {
console.log("Connection established");
ws.send(JSON.stringify(stock_request));
};
...
Methods
● Send
var ws = new WebSocket("ws://localhost:8181");
ws.onopen = function(e) {
ws.send(JSON.stringify(_request));...
Attributes
● ReadyState
Attribute Name Attribute Value Description
WebSocket.CONNECTING 0 The connection is not yet open
W...
Row 1 Row 2 Row 3 Row 4
0
2
4
6
8
10
12
Column 1
Column 2
Column 3
JSR-356
● Endpoint Classes
● javax.websocket.server.ServerEndpoint
● javax.websocket.ClientEndpoint
● Endpoint method-leve...
XMLHttpRequest object properties
Property Description
• readyState An integer from 0. . .4. (0 means the call
is uninitial...
● readyState Holds the status of the
XMLHttpRequest. Changes from 0 to 4:
● 0: request not initialized
● 1: server connect...
XMLHttpRequest object methods
Method Description
• open('method', 'URL', asyn) Specifies the HTTP method to be used (GET
o...
Intercepting form submission & Ajax
● var xhttp;
● if (window.XMLHttpRequest) {
● xhttp = new XMLHttpRequest();
● } else {...
Nashorn
● Java-to-JavaScript interoperability
● JavaScript Engine for the JVM
● Implementation of the ECMAScript Edition 5...
ScriptEngineManager factory = new ScriptEngineManager();
// create a Nashorn script engine
ScriptEngine engine =
factory.g...
Accessing Java Classes
● Two approaches
– recommended - Java global object
– Traditional – Packages global object
Packages object
● Enables you to access Java packages and
classes using their fully qualified names, if they
are propertie...
Accessing standard Java packages
●
jjs> java.lang
[JavaPackage java.lang]
●
jjs> typeof java.lang
object
●
jjs> java.lang....
Thank you
Srikanth Pallerla
+91-99-59-787-202
psrr.sri@gmail.com
srikanth.pallerla@vekomy.com
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nasho...
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nasho...
of 21

Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.

Published on: Mar 4, 2016
Source: www.slideshare.net


Transcripts - Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.

  • 1. Web Socket Client/Server communications Polling Vs LongPolling Vs Streaming HTTP half-duplex HTTP Overhead-less efficient communication
  • 2. To create desktop-like applications in the browser ● Developers used hacks in servers to keep connections open longer ● Open up resource allocation issues on servers ● Perceived latency to the end-user may be low, but ● Polling – unnecessary requests and stream of opening and closing connections ● No facility for layering other protocols on top of Comet or AJAX ● WebSocket gives you a bi-directional full-duplex communications channel that operates over HTTP through a single socket.
  • 3. WebSockets give you the... ● Ability to utilize an upgraded HTTP request ● Sends data in a message-based way similar to UDP ● Has the reliability of TCP. ● Single-connection, negligible penalty in resource uti‐lization, layer another protocol on top of WebSocket, a secure way over TLS
  • 4. WebSocket is an... ● Event-driven, full duplex, asynchronous communications channel with single TCP Connection ● Primary benefit is reducing resource needs on both the client and more importantly, the server. ● IETF standardized as RFC-6455 ● Utilizes HTTP as the initial transport mechanism ● Doesn’t end after a response is received by the client ● The client and server can freely send messages asynchronously without polling for anything new. ● Initialize the browser native WebSocket object var ws = new WebSocket("ws://localhost:8181", protocol (optional)); ● Sub-protocols so a single server can implement multiple WebSocket sub-protocols. (Registerd, open(XMPP, STOMP), custom)
  • 5. Events ● Open ws.onopen = function(e) { console.log("Connection established"); ws.send(JSON.stringify(stock_request)); }; ● Message ws.onmessage = function(e) { var _sData = JSON.parse(e.data); //can close connection }; ● Error ws.onerror = function(e) { console.log("WebSocket failure, error", e); handleErrors(e); }; ● PING / PONG – PING frames get sent out by the server only – Browser implementations should send back PONG frames in response ● Close ws.onclose = function(e) { console.log(e.reason + " " + e.code); };
  • 6. Methods ● Send var ws = new WebSocket("ws://localhost:8181"); ws.onopen = function(e) { ws.send(JSON.stringify(_request)); } ● Close ws.close(1000, "Goodbye, World!"); https://tools.ietf.org/html/rfc6455#section-7.4
  • 7. Attributes ● ReadyState Attribute Name Attribute Value Description WebSocket.CONNECTING 0 The connection is not yet open WebSocket.OPEN 1 The connection is open and ready to communicate WebSocket.CLOSING 2 The connection is in the process of closing WebSocket.CLOSED 3 The connection is closed or couldn’t be opened ● BufferedAmount Ensuring all data is sent before closing a connection, ● Protocol The handshake when completed should contain a selection from one that was sent by the client
  • 8. Row 1 Row 2 Row 3 Row 4 0 2 4 6 8 10 12 Column 1 Column 2 Column 3
  • 9. JSR-356 ● Endpoint Classes ● javax.websocket.server.ServerEndpoint ● javax.websocket.ClientEndpoint ● Endpoint method-level annotations ● @OnOpen ● @OnClose ● @OnError ● @OnMessage ● http://www.oracle.com/technetwork/articles/java/jsr356-1937161.htm
  • 10. XMLHttpRequest object properties Property Description • readyState An integer from 0. . .4. (0 means the call is uninitialized, 4 means that the call is complete.) • onreadystatechange Determines the function called when the objects readyState changes. • responseText Data returned from the server as a text string (read-only). • responseXML Data returned from the server as an XML document object (read-only). • status HTTP status code returned by the server • statusText HTTP status phrase returned by the server We use the readyState to determine when the request has been completed, and then check the status to see if it executed without an error. (We’ll see how to do this shortly.)
  • 11. ● readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4: ● 0: request not initialized ● 1: server connection established ● 2: request received ● 3: processing request ● 4: request finished and response is ready
  • 12. XMLHttpRequest object methods Method Description • open('method', 'URL', asyn) Specifies the HTTP method to be used (GET or POST as a string, the target URL, and whether or not the request should be handled asynchronously (asyn should be true or false, if omitted, true is assumed). • send(content) Sends the data for a POST request and starts the request, if GET is used you should call send(null). • setRequestHeader('x','y') Sets a parameter and value pair x=y and assigns it to the header to be sent with the request. • getAllResponseHeaders() Returns all headers as a string. • getResponseHeader(x) Returns header x as a string. • abort() Stops the current operation. The open object method is used to set up the request, and the send method starts the request by sending it to the server (with data for the server if the POST method is used).
  • 13. Intercepting form submission & Ajax ● var xhttp; ● if (window.XMLHttpRequest) { ● xhttp = new XMLHttpRequest(); ● } else { ● // code for IE6, IE5 ● xhttp = new ActiveXObject("Microsoft.XMLHTTP"); ● } ● xhttp.onreadystatechange = function() { ● if (xhttp.readyState == 4 && xhttp.status == 200) { ● document.getElementById("demo").innerHTML = xhttp.responseText; ● } ● } ● xhttp.open("GET/POST", "url", true/false); ● xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); -POST ● xhttp.send(); ● http://jsfiddle.net/vintharas/9f7sb409/ ● http://jsfiddle.net/clickthelink/Uwcuz/1/
  • 14. Nashorn ● Java-to-JavaScript interoperability ● JavaScript Engine for the JVM ● Implementation of the ECMAScript Edition 5.1 Language Specification ● Based on features of Da Vinci Machine - reference implementation of (JSR) 292 ● Pass the script to the 'jjs' or 'jrunscript' tool. ● javax.script package. ● ScriptEngineManager object.
  • 15. ScriptEngineManager factory = new ScriptEngineManager(); // create a Nashorn script engine ScriptEngine engine = factory.getEngineByName("nashorn"); // evaluate JavaScript statement try { engine.eval("print('Hello, World!');"); } catch (final ScriptException se) { se.printStackTrace(); }
  • 16. Accessing Java Classes ● Two approaches – recommended - Java global object – Traditional – Packages global object
  • 17. Packages object ● Enables you to access Java packages and classes using their fully qualified names, if they are properties of the Packages object ● jjs> Packages.MyPackage [JavaPackage MyPackage] ● jjs> Packages.MyPackage.MyClass [JavaClass MyPackage.MyClass]
  • 18. Accessing standard Java packages ● jjs> java.lang [JavaPackage java.lang] ● jjs> typeof java.lang object ● jjs> java.lang.System [JavaClass java.lang.System] ● jjs> typeof java.lang.System Function https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nas horn/api.html
  • 19. Thank you Srikanth Pallerla +91-99-59-787-202 psrr.sri@gmail.com srikanth.pallerla@vekomy.com

Related Documents