Download WebSocket in Java EE

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
WebSocket in Java EE
Štěpán Kopřiva
[email protected]
1
Agenda
ñ  WebSocket Protocol
ñ  Protocol Analysis and Usage
ñ  WebSocket API for Java
ñ  Project Tyrus
ñ  Demo
2
WebSocket Protocol
ñ  IETF RFC 6455 Standard
ñ  Bi – directional, full duplex communication
ñ  One TCP connection
ñ  Stream of messages
ñ  Communication over standard TCP port 80
ñ  ws:// and wss://
3
Establish a Connection
4
Handshake Request
Handshake Request
Http Request!
GET /endpoint HTTP/1.1!
Host: server.example.com!
Upgrade: websocket!
Connection: Upgrade!
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==!
Sec-WebSocket-Protocol: megachat, chat!
Sec-WebSocket-Extensions : compress, mux!
Sec-WebSocket-Version: 13!
Origin: http://example.com!
5
Handshake Response
Handshake Response
Http Response!
HTTP/1.1 101 Switching Protocols!
Upgrade: websocket!
Connection: Upgrade!
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=!
Sec-WebSocket-Protocol: chat!
Sec-WebSocket-Extensions: compress, mux!
!
6
Establish a connection
7
WebSocket Events & Lifecycle
ñ  Open
ñ  Message
ñ  Error
ñ  Close
8
WebSocket Pros & Cons (vs. HTTP)
ñ  Small message size (small overhead)
ñ  Decreased latency
ñ  Useful for following applications:
ñ  Small messages (updates)
ñ  A lot of messages
ñ  Ticker update, games, chat
9
WebSocket vs. AJAX experiment
ñ  Localhost
ñ  AJAX: 1000 iterations in 2.934 s
ñ  WS: 1000 iterations in 0.466 s
ñ  England - California
ñ  AJAX: 1000 iterations in 263.7 s
ñ  WS: 1000 iterations in 227.7 s
ñ  http://www.peterbe.com/plog/arewebsockets-faster-than-ajax
10
WebSocket Scalability
ñ  Extension which
ñ  Separates logical connection from physical one
ñ  Allows multiple logical connections to share
physical connection
11
JSR 356 Java API for Web Socket
ñ  Specification
ñ 
ñ 
ñ 
ñ 
http://jcp.org/en/jsr/detail?id=356
http://java.net/projects/websocket-spec
Early Draft Review
Will be in Java EE 7
ñ  Reference Implementation
ñ  http://tyrus.java.net
ñ  Bundled in latest Glassfish 4 builds
12
JSR 356 Java API for Web Socket
ñ  Package javax.websocket
ñ  Annotated API
ñ  Programmatic API
ñ  Client API
13
Hello World Server - Annotated
@WebSocketEndpoint(value=“/hello”)
public class HelloServer {
@WebSocketMessage
public void onMessage(String message, Session session) {
session.getRemote().sendString(“Hello ”+text);
}
}
14
Main API classes
Endpoint
MessageHandler
RemoteEndpoint
Session
Intercepts websocket lifecycle events
Handles all incoming messages for an
Endpoint
Represents the ‘other end’ of this
conversation
Represents the active conversation
15
Object Model: Server Side
MessageHandler
MessageHandler
MessageHandler
Server
RemoteEndpoint
Client
Session
Endpoint
Session
Client
RemoteEndpoint
MessageHandler
MessageHandler
MessageHandler
16
WebSocketEndpoint Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WebSocketEndpoint {
public String value();
public String[] subprotocols() default {};
public Class<? extends Decoder>[] decoders() default {};
public Class<? extends Encoder>[] encoders() default {};
}
17
WebSocketMessage Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WebSocketMessage {
public long maxMessageSize() default -1;
}
18
Other Method Annotations
ñ  WebSocketOpen
ñ  WebSocketClose
ñ  WebSocketError
19
Hello World Server - Programmatic
public class HelloServer extends Endpoint {
@Override
public void onOpen(Session session) {
session.addMessageHandler(new MessageHandler.Basic<String> {
public void onMessage(String text) {
session.getRemote().sendString(“Hello ”+text);
}
});
}
...
}
20
Endpoint
public abstract class Endpoint {
public abstract EndpointConfiguration
getEndpointConfiguration();
public abstract void onOpen(Session session);
public void onClose(CloseReason closeReason) { }
public void onError(Throwable thr) { }
}
21
Endpoint Configuration
public interface EndpointConfiguration {
List<Encoder> getEncoders();
List<Decoder> getDecoders();
}
22
Session
public interface Session {
RemoteEndpoint getRemote();
void addMessageHandler(MessageHandler listener);
Set<MessageHandler> getMessageHandlers();
void removeMessageHandler(MessageHandler listener);
void close() throws IOException;
...
}
23
RemoteEndpoint
public interface RemoteEndpoint {
void sendString(String text) throws IOException;
void sendBytes(ByteBuffer data) throws IOException;
void sendPartialString(String fragment, boolean isLast)
throws IOException;
void sendPartialBytes(ByteBuffer partialByte, boolean
isLast) throws IOException;
void sendObject(Object o) throws IOException,;
}
24
Hello Client - Programmatic
Client
MessageHandler
MessageHandler
MessageHandler
Endpoint
Session
RemoteEndpoint
Server
25
Hello World Client
public class HelloClient extends Endpoint {
@Override
public void onOpen(Session session) {
session.addMessageHandler(new MessageHandler.Basic<String> {
public void onMessage(String text) {
System.out.println(“Server said: ”+text);
}
});
session.getRemote().sendString(“Peter”);
}
...
}
26
Draw Server Demo
27
Thank You!
ñ  Give it a try
ñ  http://tyrus.java.net
ñ  https://github.com/jersey/hol-sse-websocket
ñ  Specification
ñ 
ñ 
ñ 
ñ 
http://jcp.org/en/jsr/detail?id=356
http://java.net/projects/websocket-spec
Early Draft Review
Will be in Java EE 7
28
Related documents