WebSockets: um upgrade de comunicação no HTML5
-
Upload
bruno-borges -
Category
Technology
-
view
1.056 -
download
1
description
Transcript of WebSockets: um upgrade de comunicação no HTML5
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 132
WebSockets: um upgrade de comunicação no HTML5
Bruno BorgesOracle Product ManagerJava Evangelist@brunoborges
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 133
Bruno Borges
Oracle Product Manager / Evangelist
Desenvolvedor, Gamer
Entusiasta em Java Embedded e JavaFX
Twitter: @brunoborges
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 134
Java EE 7está pronto!
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 135
Java EE 7 Escopo
● Produtividade de Desenvolvimento– Menos código Boilerplate
– Funcionalidades mais ricas
– Mais convenções e defaults● Suporte a HTML5
– WebSocket
– JSON
– HTML5 Forms
JSR 342
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 136
Java EE 7
EJB 3.2
Servlet 3.1
CDIExtensio
ns
Batch 1.0
Web Fragment
s
JCA 1.7JMS 2.0JPA 2.1
Managed Beans 1.0
Concurrency 1.0Common
Annotations 1.1Interceptors1.2, JTA 1.2✔
CDI 1.1
JSF 2.2,JSP 2.3,EL 3.0
JAX-RS 2.0,JAX-WS 2.2 JSON 1.0
WebSocket 1.0
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 137
Construindo aplicações HTML5 com WebSockets
Java API WebSocket 1.0 – JSR 356 RFC 6455 W3C Web Sockets API
– JavaScript objects
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 138
HTTP vs WebSockets
Protocolo HTTP é half-duplex Gambiarras
– Polling
– Long polling
– Streaming
WebSocket resolve o problemade uma vez por todas
– Full-duplex
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 139
WebSockets Handshake
Cliente solicita um UPGRADE Server confirma (Servlet 3.1) Cliente recebe o OK Inicia a sessão WebSocket
http://farata.github.io/slidedecks/state_of_websocket/slides.html#13.4
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1310
WebSockets API for Javascriptvar ws; if (window.WebSocket) { ws = new WebSocket("ws://echo.websocket.org");
ws.onopen = function () { console.log("onopen"); }; ws.onmessage = function (e) { // e.data contains received string. console.log("echo from server : " + e.data); }; ws.onclose = function () { console.log("onclose"); }; ws.onerror = function () { console.log("onerror"); }; } else { console.log("WebSocket not supported in your browser");}
if (ws.readyState === 1) ws.send("Hello WebSocket!");
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1311
WebSockets Facts
WS não funciona por proxies Suporte incompleto em browsers Desafios de configurar WebSocket com LBRs
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1312
WebSockets e Browsers
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1313
WebSockets só roda em navegadores Web?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1314
Clientes além de Navegadores
iOS– SocketRocket
Android
– Async HTTP Client
– AutobahnAndroid
Java libs and JavaFX
– WebView HTML5 component
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1315
Ping Pong
Serve para manter a conexão ativa Programaticamente possível interceptar
– Server
– Client
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1316
Java API for WebSockets 1.0
API para definir WebSockets, tanto Client como Server– Annotation-driven (@ServerEndpoint)
– Interface-driven (Endpoint)
– Client (@ClientEndpoint)
SPI para data frames– Negociação handshake na abertura do WebSocket
Integração com o Java EE Web container
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1317
Java API for WebSockets 1.0
import javax.websocket.*;import javax.websocket.server.*;
@ServerEndpoint(“/hello”)public class HelloBean {
@OnMessage public String sayHello(String name) { return “Hello “ + name; }}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1318
Java API for WebSockets 1.0
@ServerEndpoint(“/chat”)public class ChatBean {
@OnOpen public void onOpen(Session peer) { peers.add(peer); }
@OnClose public void onClose(Session peer) { peers.remove(peer); }
@OnMessage public void message(String msg, Session client) { peers.forEach(p > p.getRemote().sendMessage(msg)); }}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1319
DEMO
WebSockets
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1320
Casos de Uso para WebSockets
Placar de jogos Controlar equipamentos pela Web Chat MMORPG Atualização de Social Streams em tempo real Ferramentas em geral com auto refresh
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1321
Perguntas?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1322
Créditos: Fara Systems
Conteúdo de WebSockets extraído de:
farata.github.io/slidedecks/state_of_websocket/slides.html#1.0
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1323
[email protected]/brunoborges
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1324
The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1325