This document discusses Java API specifications for WebSockets (JSR 356) and JSON processing (JSR 353) in Java EE 7. It provides an overview of creating and consuming WebSocket endpoints and messages on both the client and server sides using annotations and message encoder/decoder classes. Code examples are given for defining server and client WebSocket endpoints, sending messages between endpoints, and using Java objects with WebSocket messages. The document also lists some artifacts like the Tyrus reference implementation and GlassFish promoted builds for working with these APIs.
2. JSR 356 - Java API for Websocket
Creating WebSocket Java components to handle bi-
directional WebSocket conversations
Initiating and intercepting WebSocket events
Creation and consumption of WebSocket text and binary
messages
The abililty to define WebSocket protocols and content
models for an application
Heads up on JSR's
3. Heads up on JSR's (Contd.)
JSR 353 - JAva API for JSON processing
Produce and consume JSON text in a streaming fashion
Build a Java object model for JSON text using API classes
Binding of JSON text to Java objects and vice versa.
4. Websocket - Server endpoint
@ServerEndpoint(value = "/HomeWS",
encoders = {DataEncoder.class},
decoders = {DataDecoder.class})
public class HomeWS {
@OnOpen
public void onOpen(Session peer) {}
@OnClose
public void onClose(Session peer) {}
@OnMessage
public void broadCastMessage(GameData figure, Session session) throws
IOException, EncodeException {
}
}
WebSocket - Server Endpoint
clients call
with this URL
5. Websocket - client endpoint
@ClientEndpoint(decoders = {DataDecoder.class}, encoders = {DataEncoder.class})
public class SignalChangeEndpointClient {
@OnOpen
public void onOpen(Session session) throws IOException, EncodeException {}
}
WebSocket - Client Endpoint
has no url value
6. public class DataDecoder implements Decoder.Text<GameData> {
@Override
public GameData decode(String string) throws DecodeException {
JsonObject jsonObject = Json.createReader(
new StringReader(string)).readObject();
return new GameData(jsonObject);
}
}
public class DataEncoder implements Encoder.Text<GameData> {
@Override
public String encode(GameData figure) throws EncodeException {
return figure.getJson().toString();
}
}
WebSocket - Message Carriers
7. this code could be used with any java object to initiate a websocket message
EJB
Servlet
JSF handler
WebSocket - Message Sending
Client
//...
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri = "ws://websocket.appserver.org/websocket"; // illustrative URL
container.connectToServer(SignalChangeEndpointClient.class, URI.create(uri));
//...
8. Artifacts
for the well grounded ....
Tyrus - a RI of JSR 356
http://tyrus.java.net/
for the impatient ....
GlassFish 4 (JEE7) - via Promoted builds
http://download.java.net/glassfish/4.0/promoted/
upcoming
WildFly
http://www.wildfly.org/