Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

WebSockets

No description
by

Andriy Pashkevych

on 6 July 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of WebSockets

WebSocket
RFC 6455
JSR 356
Why?
Web Applications needed a bidirectional communication
Truly interactive UI, happy users, more money.
Agenda
Why WebSockets
Predecessors
The specs:
RFC 6455
JSR 356
Demonstration
Conclusions
QA

Predecessors
Comet techniques
Streaming
Long Polling

Browser Plugins
Java applets
Flash
Silverlight

WebSocket (RFC 6455)
Part of HTML 5
WebSocket lifecycle
Establish a connection using HTTP handshake
Transfer messages over TCP
Close or fail connection using closing handshake
Handshake
HTTP Upgrade request:
HTTP Upgrade response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
GET /chat HTTP/1.1
Host: server.example.com
Connection: Upgrade
Upgrade: websocket
Origin: http://example.com
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Design philosophy
WebSockset is just a layer over TCP
Web-origin security
Multiple services on one port and multiple host names on one IP
Framing mechanism on top of TCP
Closing handshake
And nothing more.
Java API for WebSockets
Endpoints
Sessions
Receiving Messages
Sending Messages
Closing Connections
Clients and Servers
Configuration
WebSocket Containers
Endpoints
Java component that represents one side of a sequence of interactions between two connected peers.
Sessions
The Java WebSocket API models the sequence of interactions between an endpoint and each of its peers as a Session.
Receiving Messages
Sending Messages
Closing connections
Servers and clients
Configuration
WebSocketContainer
Demonstration
Conclusions
Q & A
Thank you!
Still missing some browser support for the RFC 6455
Good
Unified way to solve well-known problem
Easy to learn and use API
Java EE 7 standard
Bad
Browsers Support
Not supported by:
IE v.<10
Opera Mini
Android browser v.<4.4
Partial support:
Opera <v.12
Opera Mobile <v.12.0
Full support:
Firefox v.11.0
Firefox for Android v. 5.0
Chrome v. 14.0
Chrome for Android v. 18.0
Safari v. 6.0
iOS Safari v. 6.0
BlackBerry Browser 7.0
Security
Web container security model is used
Authentication
Authorization
Transport-Guarantee
Typically HTTP handshake is used
to authenticate clients
Standard, for example <security-constraint> element in web.xml
NONE - allows unencrypted communication
CONFIDENTIAL - requires wss://
Carries configuration properties for endpoints
One WebSocketContainer instance per application per Java VM
Represents websocket implementation to applications
Endpoint lifecycle
* Endpoint may be configured to share instances among connections, but developers need to take care about threads.
Programmatic endpoint
import javax.websocket.*;

public class ChatEndpoint extends Endpoint {

@Override
public void onOpen(Session session, EndpointConfig config) {

session.addMessageHandler(new MessageHandler.Whole<String>() {

@Override
public void onMessage(String message) {
System.out.println(message);
}

});
}

@Override
public void onClose(Session session, CloseReason closeReason) {

}

@Override
public void onError(Session session, Throwable thr) {

}
}
Annotated Endpoint
import javax.websocket.*;

@ServerEndpoint(value = "/chat")
public class AnnotatedChatEndpoint {

@OnOpen
public void onOpen(Session session, EndpointConfig config) {

}

@OnClose
public void onClose(Session session, CloseReason closeReason) {

}

@OnError
public void onError(Session session, Throwable thr) {

}

@OnMessage
public void onMessage(Session session, String message) {
System.out.println(message);
}

}
One MessageHandler per Session, per native websocket message type
Native message types
Text
Programmatic
Annotated
@Override
public void onOpen(Session session, EndpointConfig config) {

session.addMessageHandler(new MessageHandler.Whole<Reader>() {

@Override
public void onMessage(Reader data) {
// handle an entire message
}
});

session.addMessageHandler(new MessageHandler.Partial<String>() {

@Override
public void onMessage(String partialMessage, boolean last) {
//handle message parts
}
});
}
@ClientEndpoint
public class ChatClient {

@OnMessage
public void onMessage(String message) {
broadcastMessage(message);
}

@OnMessage
public void processMessage(byte[] messagePart, boolean last, Session session) {
broadcastMessagePart(messagePart, session);
}

}
Connection close may be initialized by:
By the remote endpoint
By the local container
The protocol is symmetric
Endpoint
Server Endpoint
Client Endpoint
The only difference is how you configure them
Server
Client
*EndpointConfig to the rescue
javax.websocket.server.ServerEndpointConfig.Configurator
javax.websocket.ClientEndpointConfig.Configurator
WebSocket implementations
Project Tyrus
Kaazing
Grizzly
Apache Tomcat 7
Glassfish
Autobahn
Jetty
Caucho Resin
jWebSocket
One instance per application per VM per connected peer.*
Create
instance
call
onOpen()
process
messages
call
onClose()
recycle endpoint
Client
Server
Opend Handshake
Opend Handshake
Session
Close frame
Client
Server
Handshake request
Handshake response
Connection
Binary
Requested subprotocols
Encoders / Decoders
Server URL
Subprotocol negotiation
URL matching
Origin check
Encoders / Decoders
WebSocket to the rescue
API Overview
Remote
Endpoint
Remote
Endpoint
Remote
Endpoint
Session
Session
Session
MessageHandlers
Endpoint
Net
MessageHandlers
Endpoint
MessageHandlers
Endpoint
W3C defined JavaScript API
TCP Based, full duplex communication
Protocol overview
It has two parts:
Handshake
Data transfer
Data Transfer
Basic
@Override
public void onOpen(Session session, EndpointConfig config) {

Basic basicRemote = session.getBasicRemote();

try {
basicRemote.sendText("Hello");

//basicRemote.sendText(partialMessage, isLast)

} catch (IOException e) {
//ouch
}

}
Async
@Override
public void onOpen(Session session, EndpointConfig config) {

Async asyncRemote = session.getAsyncRemote();

try {

Future<Void> future = asyncRemote.sendText("Hello");
while (future.get() == null)
{
//wait
}

} catch (IOException e) {
//ouch
}
}
@OnClose
public void onClose(CloseReason reason) {
reason.getCloseCode();
reason.getReasonPhrase();
}
Final
Reserved
Operation code
Application Data + Extention data length
For client traffic masking
Data
Support: 74.78%
Partial support: 1.56%
Total: 76.34%
information from http://caniuse.com/websockets
Close frame
Full transcript