30
Vert.x & Socket.IO 이해 및 활용 12109화요일

Vert.x&Socket.IO 이해 및 활용 | Devon 2012

  • Upload
    daum-dna

  • View
    16.476

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  &����������� ������������������  Socket.IO

이해����������� ������������������  및����������� ������������������  활용

12년 10월 9일 화요일

Page 2: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

백기선NBP����������� ������������������  /����������� ������������������  서비스플랫폼센터����������� ������������������  /����������� ������������������  웹플랫폼개발랩

-����������� ������������������  사내����������� ������������������  스프링����������� ������������������  교육

-����������� ������������������  RTCS����������� ������������������  유지����������� ������������������  보수

✔����������� ������������������  발표자

-����������� ������������������  mod-socket-io����������� ������������������  개발

12년 10월 9일 화요일

Page 3: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

✔����������� ������������������  목차

1.����������� ������������������  Vert.x����������� ������������������  소개����������� ������������������  ����������� ������������������  :����������� ������������������  What����������� ������������������  is����������� ������������������  vert.x?

����������� ������������������  ����������� ������������������  :����������� ������������������  Concepts����������� ������������������  in����������� ������������������  vert.x

����������� ������������������  

2.����������� ������������������  Vert.x����������� ������������������  프로그래밍����������� ������������������  ����������� ������������������  :����������� ������������������  Hello����������� ������������������  Vert.x

����������� ������������������  ����������� ������������������  :����������� ������������������  HttpServer����������� ������������������  &����������� ������������������  HttpClient

����������� ������������������  ����������� ������������������  :����������� ������������������  Using����������� ������������������  Event����������� ������������������  Bus

3.����������� ������������������  Socket.io����������� ������������������  소개

����������� ������������������  

4.����������� ������������������  Socket.io����������� ������������������  프로그래밍����������� ������������������  ����������� ������������������  :����������� ������������������  Sending����������� ������������������  Messages����������� ������������������  &����������� ������������������  Events

����������� ������������������  ����������� ������������������  :����������� ������������������  Authorization

����������� ������������������  ����������� ������������������  :����������� ������������������  Using����������� ������������������  Namesapce

12년 10월 9일 화요일

Page 4: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  소개

12년 10월 9일 화요일

Page 5: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  What����������� ������������������  is����������� ������������������  vert.x?

✔����������� ������������������  Vert.x����������� ������������������  is����������� ������������������  the����������� ������������������  framework����������� ������������������  for����������� ������������������  the����������� ������������������  next����������� ������������������  generation����������� ������������������  of����������� ������������������  asynchronous,����������� ������������������  effortlessly����������� ������������������  scalable,����������� ������������������  concurrent����������� ������������������  applications.

JVM

Vert.x

java js rb py ...

Netty Hazelcast

12년 10월 9일 화요일

Page 6: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Tim����������� ������������������  Fox

✔����������� ������������������  Tim����������� ������������������  Fox����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  핵심����������� ������������������  개발자

����������� ������������������  ����������� ������������������  :����������� ������������������  VMWare����������� ������������������  /����������� ������������������  SpringSource

����������� ������������������  ����������� ������������������  :����������� ������������������  creator����������� ������������������  of����������� ������������������  the����������� ������������������  HometQ

����������� ������������������  ����������� ������������������  :����������� ������������������  worked����������� ������������������  with����������� ������������������  the����������� ������������������  RabbitMQ

12년 10월 9일 화요일

Page 7: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Github/vert-x/vert.x

12년 10월 9일 화요일

Page 8: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Commit

1257

Fork

215

Watch

1520

Vert.x����������� ������������������  :����������� ������������������  Github/vert-x/vert.x

12년 10월 9일 화요일

Page 9: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Verticle

Verticlepublic����������� ������������������  class����������� ������������������  App����������� ������������������  extends

✔����������� ������������������  vert.x에����������� ������������������  배포����������� ������������������  가능한����������� ������������������  애플리케이션����������� ������������������  단위.

~ > vertx run App.java

12년 10월 9일 화요일

Page 10: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Vert.x����������� ������������������  Instance

✔����������� ������������������  Verticle을����������� ������������������  개별적인����������� ������������������  클래스로더를����������� ������������������  사용하여����������� ������������������  실행해주며,����������� ������������������  손쉽게����������� ������������������  클러스터로����������� ������������������  구성할����������� ������������������  수����������� ������������������  있다.

Vert.x

V V V

Vert.x

V V V

Vert.x Vert.x

V V V V V V

12년 10월 9일 화요일

Page 11: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Polyglot

✔����������� ������������������  다양한����������� ������������������  언어로����������� ������������������  Verticle을����������� ������������������  작성할����������� ������������������  수����������� ������������������  있다.

~ > vertx run app.js

load('vertx.js')

var����������� ������������������  server����������� ������������������  =����������� ������������������  vertx.createNetServer();

server.connectHandler(function(sock)����������� ������������������  {����������� ������������������  ����������� ������������������  new����������� ������������������  vertx.Pump(sock,����������� ������������������  sock).start();}).listen(1234,����������� ������������������  'localhost');

function����������� ������������������  vertxStop()����������� ������������������  {����������� ������������������  ����������� ������������������  server.close();}

require "vertx"include Vertx

@server = NetServer.new.connect_handler { |socket| Pump.new(socket, socket).start}.listen(1234, 'localhost')

def vertx_stop @server.closeend

~ > vertx run app.rb

12년 10월 9일 화요일

Page 12: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Concurrency

✔����������� ������������������  Verticle����������� ������������������  인스턴스당����������� ������������������  스레드����������� ������������������  한개����������� ������������������  사용.����������� ������������������  ����������� ������������������  :����������� ������������������  멀티스레드����������� ������������������  프로그래밍을����������� ������������������  고려하여����������� ������������������  코딩할����������� ������������������  필요����������� ������������������  없음.

����������� ������������������  ����������� ������������������  :����������� ������������������  즉,����������� ������������������  locking이나����������� ������������������  Synchronization을����������� ������������������  할����������� ������������������  필요����������� ������������������  없음.

����������� ������������������  ����������� ������������������  :����������� ������������������  컨텍스트����������� ������������������  스위칭����������� ������������������  비용����������� ������������������  절감,����������� ������������������  스레드가����������� ������������������  소모하는����������� ������������������  메모리����������� ������������������  절약.

T1 T2 T3

Verticle����������� ������������������  1 I’m not usingmulti threads

12년 10월 9일 화요일

Page 13: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Concurrency

✔����������� ������������������  Verticle����������� ������������������  인스턴스당����������� ������������������  스레드����������� ������������������  한개����������� ������������������  사용.����������� ������������������  ����������� ������������������  :����������� ������������������  멀티스레드����������� ������������������  프로그래밍을����������� ������������������  고려하여����������� ������������������  코딩할����������� ������������������  필요����������� ������������������  없음.

����������� ������������������  ����������� ������������������  :����������� ������������������  즉,����������� ������������������  locking이나����������� ������������������  Synchronization을����������� ������������������  할����������� ������������������  필요����������� ������������������  없음.

����������� ������������������  ����������� ������������������  :����������� ������������������  컨텍스트����������� ������������������  스위칭����������� ������������������  비용����������� ������������������  절감,����������� ������������������  스레드가����������� ������������������  소모하는����������� ������������������  메모리����������� ������������������  절약.

EL1 EL2 EL3

Verticle����������� ������������������  1 Verticle����������� ������������������  2 Verticle����������� ������������������  3

Yes, I’m

12년 10월 9일 화요일

Page 14: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Event-based����������� ������������������  Programming����������� ������������������  Model

✔����������� ������������������  이벤트����������� ������������������  기반����������� ������������������  프로그래밍����������� ������������������  모델����������� ������������������  제공.����������� ������������������  ����������� ������������������  :����������� ������������������  이벤트����������� ������������������  핸들러����������� ������������������  등록과����������� ������������������  이벤트(+메시지)����������� ������������������  전송����������� ������������������  프로그래밍����������� ������������������  제공.

����������� ������������������  ����������� ������������������  :����������� ������������������  소켓.write(메시지)����������� ������������������  기능도����������� ������������������  Non-blocking����������� ������������������  IO로����������� ������������������  처리.

����������� ������������������  ����������� ������������������  :����������� ������������������  http://en.wikipedia.org/wiki/Asynchronous_I/O

EventBus eb = vertx.eventBus();

Handler<Message> myHandler = new Handler<Message>() { public void handle(Message message) { System.out.println("I received a message " + message.body); }};

eb.registerHandler("test.address", myHandler);

eb.publish("test.address", "hello world");

12년 10월 9일 화요일

Page 15: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  :����������� ������������������  Core����������� ������������������  Concepts����������� ������������������  ::����������� ������������������  Event����������� ������������������  Loops

✔����������� ������������������  Verticle마다����������� ������������������  유일하게����������� ������������������  할당되는����������� ������������������  Thread.����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  인스턴스에서����������� ������������������  여러����������� ������������������  Event����������� ������������������  Loop����������� ������������������  스레드를����������� ������������������  관리한다.

����������� ������������������  ����������� ������������������  :����������� ������������������  Vericle이����������� ������������������  배포되면����������� ������������������  그����������� ������������������  중����������� ������������������  한����������� ������������������  스레드를����������� ������������������  Vericle에����������� ������������������  할당한다.

����������� ������������������  ����������� ������������������  :����������� ������������������  Multi-reactor pattern

출처: http://krondo.com/wp-content/uploads/2009/08/twisted-intro.html

Event Loop Run Loop Event Dispatcher

Message Dispatcher Message Pump

“The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.” - iOS docs

12년 10월 9일 화요일

Page 16: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  프로그래밍

12년 10월 9일 화요일

Page 17: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  Programming����������� ������������������  :����������� ������������������  Maven����������� ������������������  Dependency����������� ������������������  :����������� ������������������  vertx-core

✔����������� ������������������  org.vert-x����������� ������������������  ::����������� ������������������  vertx-core����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  핵심����������� ������������������  기능을����������� ������������������  담고����������� ������������������  있는����������� ������������������  라이브러리.

����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  사용하는����������� ������������������  프로젝트에����������� ������������������  반드시����������� ������������������  추가.

����������� ������������������  ����������� ������������������  :����������� ������������������  TCP/SSL,����������� ������������������  HTTP/HTTPS,����������� ������������������  WebSocket,����������� ������������������  Shared����������� ������������������  Map/Set����������� ������������������  제공

<dependency>! <groupId>org.vert-x</groupId>! <artifactId>vertx-core</artifactId>! <version>1.2.3.final</version></dependency>

12년 10월 9일 화요일

Page 18: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  Programming����������� ������������������  :����������� ������������������  Maven����������� ������������������  Dependency����������� ������������������  :����������� ������������������  vertx-platform

✔����������� ������������������  org.vert-x����������� ������������������  ::����������� ������������������  vertx-platform����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  컨테이너����������� ������������������  기능(배포,����������� ������������������  컨텍스트)을����������� ������������������  담고����������� ������������������  있는����������� ������������������  라이브러리.

����������� ������������������  ����������� ������������������  :����������� ������������������  Embedded����������� ������������������  모드로����������� ������������������  사용할����������� ������������������  경우����������� ������������������  생략����������� ������������������  가능.

����������� ������������������  ����������� ������������������  :����������� ������������������  CLI,����������� ������������������  Conatainer,����������� ������������������  Deployment����������� ������������������  제공

<dependency>! <groupId>org.vert-x</groupId>! <artifactId>vertx-platform</artifactId>! <version>1.2.3.final</version></dependency>

12년 10월 9일 화요일

Page 19: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  Programming����������� ������������������  :����������� ������������������  Demo

✔Hello����������� ������������������  Vert.x✔HttpServer&HttpClient✔Using����������� ������������������  Event����������� ������������������  Bus

12년 10월 9일 화요일

Page 20: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  Programming����������� ������������������  :����������� ������������������  HttpServer����������� ������������������  &����������� ������������������  HttpClient

DEMO

12년 10월 9일 화요일

Page 21: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Vert.x����������� ������������������  Programming����������� ������������������  :����������� ������������������  Using����������� ������������������  Event����������� ������������������  Bus

DEMO

12년 10월 9일 화요일

Page 22: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  소개

12년 10월 9일 화요일

Page 23: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  :����������� ������������������  http://socket.io/

✔����������� ������������������  Socket.IO����������� ������������������  aims����������� ������������������  to����������� ������������������  make����������� ������������������  realtime����������� ������������������  apps����������� ������������������  possible����������� ������������������  in����������� ������������������  every����������� ������������������  browser����������� ������������������  and����������� ������������������  mobile����������� ������������������  device,����������� ������������������  blurring����������� ������������������  the����������� ������������������  differences����������� ������������������  between����������� ������������������  the����������� ������������������  different����������� ������������������  transport����������� ������������������  mechanisms

12년 10월 9일 화요일

Page 24: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  :����������� ������������������  Transports

✔����������� ������������������  Supported����������� ������������������  Transports����������� ������������������  ����������� ������������������  :����������� ������������������  WebSocket

����������� ������������������  ����������� ������������������  :����������� ������������������  Flash����������� ������������������  Socket

����������� ������������������  ����������� ������������������  :����������� ������������������  AJAX����������� ������������������  long����������� ������������������  polling

����������� ������������������  ����������� ������������������  :����������� ������������������  AJAX����������� ������������������  multipart����������� ������������������  streaming

����������� ������������������  ����������� ������������������  :����������� ������������������  Forever����������� ������������������  Iframe

����������� ������������������  ����������� ������������������  :����������� ������������������  JSONP����������� ������������������  Polling

✔����������� ������������������  Supported����������� ������������������  Browsers����������� ������������������  ����������� ������������������  :����������� ������������������  IE����������� ������������������  5.5+

����������� ������������������  ����������� ������������������  :����������� ������������������  Safari����������� ������������������  3+

����������� ������������������  ����������� ������������������  :����������� ������������������  Google����������� ������������������  Chrome����������� ������������������  4+

����������� ������������������  ����������� ������������������  :����������� ������������������  Firefox����������� ������������������  3+

����������� ������������������  ����������� ������������������  :����������� ������������������  Opera����������� ������������������  10.61+

����������� ������������������  ����������� ������������������  :����������� ������������������  iPhone����������� ������������������  Safari

����������� ������������������  ����������� ������������������  :����������� ������������������  iPad����������� ������������������  Safari

����������� ������������������  ����������� ������������������  :����������� ������������������  Android����������� ������������������  Webkit

����������� ������������������  ����������� ������������������  :����������� ������������������  WebOs����������� ������������������  Webkit

12년 10월 9일 화요일

Page 25: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  :����������� ������������������  사용법

var  app  =  express.createServer()    ,  io  =  io.listen(app);

app.listen(80);

io.sockets.on('connection',  function  (socket)  {    socket.emit('news',  {  hello:  'world'  });    socket.on('my  other  event',  function  (data)  {        console.log(data);    });});

<script  src="/socket.io/socket.io.js"></script><script>    var  socket  =  io.connect('http://localhost');    socket.on('news',  function  (data)  {        console.log(data);        socket.emit('my  other  event',  {  my:  'data'  });    });</script>

12년 10월 9일 화요일

Page 26: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  :����������� ������������������  Node.js����������� ������������������  Module

~ > npm install socket.io

Java����������� ������������������  구현체는?

12년 10월 9일 화요일

Page 27: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  :����������� ������������������  Mod-Socket-IO����������� ������������������  :����������� ������������������  https://github.com/keesun/mod-socket-io

✔����������� ������������������  Vert.x����������� ������������������  기반����������� ������������������  Socket.IO����������� ������������������  서버����������� ������������������  구현체����������� ������������������  ����������� ������������������  :����������� ������������������  Vert.x����������� ������������������  모듈����������� ������������������  저장소에서����������� ������������������  이용����������� ������������������  가능.

����������� ������������������  ����������� ������������������  :����������� ������������������  Embedded����������� ������������������  Vericle����������� ������������������  형태로����������� ������������������  Spring����������� ������������������  MVC����������� ������������������  애플리케이션과����������� ������������������  연동����������� ������������������  가능.

����������� ������������������  ����������� ������������������  :����������� ������������������  Cloud����������� ������������������  형태로����������� ������������������  이용할����������� ������������������  수����������� ������������������  있도록����������� ������������������  사내����������� ������������������  플랫폼����������� ������������������  개발����������� ������������������  중.

12년 10월 9일 화요일

Page 28: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  프로그래밍

12년 10월 9일 화요일

Page 29: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

Socket.io����������� ������������������  Programming����������� ������������������  :����������� ������������������  Demo

✔Sending����������� ������������������  Events✔Authorization✔Using����������� ������������������  Namespace

12년 10월 9일 화요일

Page 30: Vert.x&Socket.IO 이해 및 활용 | Devon 2012

mod-socket-io����������� ������������������  많이����������� ������������������  써주세요.

감사합니다.

12년 10월 9일 화요일