Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.
Ähnlich wie Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.
Ähnlich wie Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation. (20)
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.
3. 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.
4. 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
5. 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)
6. 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);
};
7. 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
8. 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
11. 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.)
12. ● 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
13.
14. 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).
16. 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.
18. Accessing Java Classes
● Two approaches
– recommended - Java global object
– Traditional – Packages global object
19. 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]