WebSockets: um upgrade de comunicação no HTML5

24
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 2 WebSockets: um upgrade de comunicação no HTML5 Bruno Borges Oracle Product Manager Java Evangelist @brunoborges

description

Conheça a nova tecnologia WebSockets, e como utilizá-la em Java

Transcript of WebSockets: um upgrade de comunicação no HTML5

Page 1: 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

Page 2: 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 133

Bruno Borges

Oracle Product Manager / Evangelist

Desenvolvedor, Gamer

Entusiasta em Java Embedded e JavaFX

Twitter: @brunoborges

Page 3: 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 134

Java EE 7está pronto!

Page 4: 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 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

Page 5: 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 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

Page 6: 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 137

Construindo aplicações HTML5 com WebSockets

Java API WebSocket 1.0 – JSR 356 RFC 6455 W3C Web Sockets API

– JavaScript objects

Page 7: 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 138

HTTP vs WebSockets

Protocolo HTTP é half-duplex Gambiarras

– Polling

– Long polling

– Streaming

WebSocket resolve o problemade uma vez por todas

– Full-duplex

Page 8: 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 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

Page 9: 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 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!");

Page 10: 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 1311

WebSockets Facts

WS não funciona por proxies Suporte incompleto em browsers Desafios de configurar WebSocket com LBRs

Page 11: 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 1312

WebSockets e Browsers

Page 12: 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 1313

WebSockets só roda em navegadores Web?

Page 13: 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 1314

Clientes além de Navegadores

iOS– SocketRocket

Android

– Async HTTP Client

– AutobahnAndroid

Java libs and JavaFX

– WebView HTML5 component

Page 14: 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 1315

Ping Pong

Serve para manter a conexão ativa Programaticamente possível interceptar

– Server

– Client

Page 15: 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 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

Page 16: 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 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;    }}

Page 17: 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 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));    }}

Page 18: 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 1319

DEMO

WebSockets

Page 19: 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 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

Page 20: 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 1321

Perguntas?

Page 21: 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 1322

Créditos: Fara Systems

Conteúdo de WebSockets extraído de:

farata.github.io/slidedecks/state_of_websocket/slides.html#1.0

Page 22: 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 1323

[email protected]/brunoborges

Page 23: 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 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.

Page 24: 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 1325