Realizzare applicazioni Web con WebSocket, by Simone Bordet
Upcoming SlideShare
Loading in...5
×
 

Realizzare applicazioni Web con WebSocket, by Simone Bordet

on

  • 2,864 views

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 ...

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.

Statistics

Views

Total Views
2,864
Slideshare-icon Views on SlideShare
2,115
Embed Views
749

Actions

Likes
1
Downloads
20
Comments
1

7 Embeds 749

http://dev.presentz.org 507
http://presentz.org 121
http://local.host 44
http://www.codemotion.it 39
http://roma2012.codemotion.it 33
http://codemotion.loc 4
http://www.presentz.org 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • good
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Realizzare applicazioni Web con WebSocket, by Simone Bordet Realizzare applicazioni Web con WebSocket, by Simone Bordet Presentation Transcript

    • Simone Bordet 1sbordet@intalio.com
    • How to Build WebSocketWeb Applications Simone Bordet 2 sbordet@intalio.com
    • Simone BordetSenior Engineer/Architect at Intalio/Webtide  Support for Jetty and CometD open source projectsOpen Source Contributor  Jetty, CometD, MX4J, Foxtrot, LiveTribe, JBoss, LarexJVM tuning expertWorking on Web Network Protocols  HTTP, WebSocket, SPDY, etc. Simone Bordet 3 sbordet@intalio.com
    • AgendaWhat is WebSocketWebSocket Browser APIWebSocket Server APIDemoQ&A Simone Bordet 4 sbordet@intalio.com
    • What is WebSocket ? Simone Bordet 5 sbordet@intalio.com
    • 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
    • WebSocket ProtocolThe WebSocket protocol is made of 2 parts  WebSocket upgrade  WebSocket data exchangeWebSocket upgrade  Its 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
    • WebSocket UpgradeREQUESTGET / HTTP/1.1Host: localhost:8080Origin: http://localhost:8080Connection: UpgradeUpgrade: websocketSec-WebSocket-Key: SbdIETLKHQ1TNBLeZFZS0g==Sec-WebSocket-Version: 13RESPONSEHTTP/1.1 101 Switching ProtocolsConnection: UpgradeUpgrade: websocketSec-WebSocket-Accept: y4yXRUolfnFfo3Jc5HFqRHNgx2A= Simone Bordet 8 sbordet@intalio.com
    • WebSocket ProtocolWebSocket data exchange  Sequence of WebSocket “frames” 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued | +---------------------------------------------------------------+ Simone Bordet 9 sbordet@intalio.com
    • 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
    • WebSocket Performance Simone Bordet 11 sbordet@intalio.com
    • WebSocket Performance Simone Bordet 12 sbordet@intalio.com
    • WebSocketBrowser API Simone Bordet 13 sbordet@intalio.com
    • WebSocket Browser APIinterface WebSocket { WebSocket(DOMString url, optional (DOMString or DOMString[]) protocols)] attribute Function? onopen; attribute Function? onerror; attribute Function? onclose; attribute Function? onmessage; void send(DOMString data); void close(optional unsigned short code, optional DOMString reason); attribute DOMString binaryType; // “blob” or “arraybuffer” void send(ArrayBuffer data); void send(Blob data);}; Simone Bordet 14 sbordet@intalio.com
    • WebSocket Browser API Usagevar ws = new window.WebSocket(“ws://localhost:8080/ws”);ws.send(“Hello, World”);ws.onopen = function() { ws.send(“Hello, World”);};ws.onclose = function(event) { // Server closed}ws.onmessage = function(event) { var data = event.data; window.console.info(“Server says: ” + data);}; Simone Bordet 15 sbordet@intalio.com
    • WebSocketServer API Simone Bordet 16 sbordet@intalio.com
    • 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
    • WebSocket Jetty APIpublic 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
    • Simone Bordet 19sbordet@intalio.com
    • 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
    • ReferencesWebSocket Protocol  http://www.ietf.org/rfc/rfc6455.txtJSR 340 (Servlet 3.1)  http://jcp.org/en/jsr/detail?id=340WebSocket API  http://dev.w3.org/html5/websockets/Jetty  http://eclipse.org/jettyCometD  http://cometd.org Simone Bordet 21 sbordet@intalio.com
    • Questions & Answers Simone Bordet 22 sbordet@intalio.com