Il protocollo WebSocket è ormai diventato un protocollo standard (RFC 6455), e ormai quasi tutti i browsers lo supportano. A differenza di HTTP permette una vera comunicazione bidirezionale tra client e server, permettendo di realizzare applicazioni molto scalabili. In questa sessione daremo uno sguardo al protocollo WebSocket e esamineremo come scrivere una applicazione web utilizzando WebSocket.
6. WebSocket
WebSocket is the HTML 5 standard protocol for
bidirectional web communication
HTTP is always client-initiated
Finally, standard server-push
Work began in 2009, ended in 2012
Excruciatingly painful
But at the end, a good standard protocol
Simone Bordet
6
sbordet@intalio.com
7. WebSocket Protocol
The WebSocket protocol is made of 2 parts
WebSocket upgrade
WebSocket data exchange
WebSocket upgrade
It's plain old HTTP with an “Upgrade” header
WebSocket runs on port 80
Could be a trouble for inspecting HTTP proxies that
do not support WebSocket
Simone Bordet
7
sbordet@intalio.com
10. WebSocket Protocol
Very compact
Smallest data frame has only 2 bytes overhead
Typical first byte is 0x81 for a text frame
0x82 for a binary frame
0x88 for a close frame
WebSocket “close” frame
Sent by the application to signal end of data
Must be replied before closing TCP connection
Prevents loss of data
Browsers MUST mask frames
But servers are not required to
Simone Bordet
10
sbordet@intalio.com
17. WebSocket Server API
No standard API for WebSocket webapps yet
Servlet 3.0 addressed asynchronous HTTP
Servlet 3.1 (JSR 340) to address this
Expected Q1 2013
For now, rely on Jetty API
Java API for both client and server
Simone Bordet
17
sbordet@intalio.com
18. WebSocket Jetty API
public class MyWebSocketServlet extends WebSocketServlet {
public WebSocket doWebSocketConnect(HttpServletRequest r, String p) {
return new MyWebSocket();
}
}
public class MyWebSocket implements WebSocket.OnTextMessage {
public void onOpen(Connection connection) {
}
public void onMessage(String data) {
}
public void onClose(int closeCode, String message) {
}
}
Simone Bordet
18
sbordet@intalio.com
20. Conclusions
WebSocket programming is easy !
However, may not be widely deployed
Old browsers
Old internet proxies
WebSocket is low-level
A framework on top gives much more productivity
CometD
Simone Bordet
20
sbordet@intalio.com