5. How WebSocket solves it ?
• Bi-directional (client-driven)
• Full-duplex (half-duplex)
• Lean protocol (verbose)
• SingleTCP connection (newTCP)
6. What is WebSocket?
• Bi-directional, full-duplex, communication channel
over a single TCP connection
• Originally proposed as part of HTML5
• IETF-defined Protocol: RFC 6455
• W3C-defined JavaScript API
8. How does it work?
• Upgrade HTTP to WebSocket (single TCP connection)
• Send dataframes in both direction (bi-directional)
• Send messages independent of each other (full-
duplex)
• End the connection
9. How does it work?
Client Server
Handshake Request
Handshake Response
19. Java API WebSocket support
• API forWebSocket server and client endpoint
• Annotated: @ServerEndpoint,@ClientEndpoint
• Programmatic:Endpoint
• WebSocket opening handshake negotiation
• Lifecycle Callback methods
• Integration with Java EE technologies
20. Java API WebSocket support
Method-level annotations
• @OnMessage: Intercepts WebSocket messages
• @OnOpen: Intercepts WebSocket open events
• @OnClose: Intercepts WebSocket close events
• @OnError: Intercepts WebSocket error events
21. Java API WebSocket support
@ServerEndpoint("/chat")
public class ChatEndpoint {
@OnMessage
public void message(String message, Session client) throws IOException, EncodeException {
System.out.println("message: " + message);
for (Session peer : client.getOpenSessions()) {
peer.getBasicRemote().sendText(message);
}
}
}
22. Encoder & Decoder
Decoder = text message -> Java object
Encoder = Java object -> text message
• Often, XML or JSON is used as
23. Encoder & Decoder
@ServerEndpoint(value = "/websocket",
encoders = { FigureEncoder.class },
decoders = { FigureDecoder.class })
public class Whiteboard {
….
public class FigureEncoder implements Encoder.Text<Figure> {
@Override
public String encode(Figure figure) throws EncodeException {
return figure.getJson().toString();
}
}
public class FigureDecoder implements Decoder.Text<Figure> {
@Override
public Figure decode(String string) throws DecodeException {
JsonObject jsonObject = Json.createReader(new StringReader(string)).readObject();
return new Figure(jsonObject);
}
26. Contact
For any question
Email: M.Haque@cimsolutions.nl
Follow me in following site:
Stackoverflow
http://stackoverflow.com/users/1265660/masud
Github
https://github.com/masudcse05
Blog
https://masudulhaque.wordpress.com