2011-09-21 3 views
3

Padrino 응용 프로그램 내에서 웹 소켓을 열고 사용할 수있는 방법을 찾고 있습니다. Padrino는 하나의 스레드로 작동하지만, "onopen" "onclose" "onmessage"메서드와 Padrino 컨트롤러간에 웹 소켓과 변수를 공유하는 방법을 찾고 있습니다.padrino && websockets

어찌 되었습니까?

링크가 내가 들여다 :

Examples of Eventmachine usage with Padrino and Sinatra em-websocket on GitHub

UPDATE (단지시나가 나를 위해 일한) 1 : 이 내 main.rb입니다 :

require 'rubygems'  # <-- Added this require 
require 'em-websocket' 
require 'padrino-core' 
require 'thin' 

require File.expand_path("../config/boot.rb", __FILE__) 

SOCKETS = [] 
EventMachine.run do  # <-- Changed EM to EventMachine 
# class App < Sinatra::Base 
#  get '/' do 
#   SOCKETS.each {|s| s.send "fooooo"} 
#   return "foo" 
#  end 
# end 

    EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws| # <-- Added |ws| 
     # Websocket code here 
     ws.onopen { 
      ws.send "connected!!!!" 
      SOCKETS << ws 
     } 

     ws.onmessage { |msg| 
      puts "got message #{msg}" 
      ws.send "ECHO: #{msg}" 
     } 

     ws.onclose { 
      ws.send "WebSocket closed" 
      SOCKETS.delete ws 
     } 

    end 

    # You could also use Rainbows! instead of Thin. 
    # Any EM based Rack handler should do. 
    #App.run!({:port => 3000}) # <-- Changed this line from Thin.start to App.run! 
    Thin::Server.start Padrino.application, '0.0.0.0', 3000 

이 예외가 발생합니다 :

/home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `require': no such file to load -- daemons (LoadError) 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `<top (required)>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/server.rb:50:in `<class:Server>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/server.rb:48:in `<module:Thin>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/server.rb:1:in `<top (required)>' 
    from main.rb:39:in `block in <main>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call' 
    from /home/cstore/.rvm/gems/[email protected]/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine' 
    from /home/cstore/.rvm/gems/[email protected]/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run' 
    from main.rb:9:in `<main>' 

업데이트 2 : Nathan에게 감사의 말을 전합니다. 방금 ​​Gemfile에 'daemons'를 추가하고 내 응용 프로그램을 다시로드했습니다.

+2

Gemfile에 추가하십시오 :'gem 'daemons'' 일반적으로이 오류는 나열된 gem이 gemset에 설치되어 있지 않거나 Gemfile에 설치되어 있지 않다는 것을 의미합니다. – Nathan

+1

참고로이 예외를 생성하는 행은 단순히 데몬 라이브러리를로드하려고 시도하는 것입니다. https://github.com/macournoyer/thin/blob/v1.2.1/lib/thin/daemonizing.rb#L2 – Nathan

+0

보석. did not work .... 나는 방금 다시 시도해 보았다. 하나님은 신비한 방식으로 일합니다 :) 고맙습니다 !!! – refaelos

답변

3

은 아마 당신은 데몬 설치해야

$ bundle install 
+1

Gemfile : gem 'daemons'를 추가 한 다음'bundle install'을 실행하십시오. – Nathan

1

특히이 예에서 무엇입니까? https://github.com/igrigorik/em-websocketAny success with Sinatra working together with EventMachine WebSockets?은 Padrino에서는 작동하지 않지만 Sinatra에서는 무엇을 했습니까? 오류를 설명하고 그 오류가 왜 실패했는지 설명 할 수 있습니까 (스택 트레이스)? 어쩌면 조사를 도울 수 있습니다.

을 편집 Gemfile :

# Adding this 
gem 'daemons' 

없는 설치 보석 :

+0

. 내 게시물을 업데이 트했습니다. – refaelos

1

내가이 게시물 가로 질러하고는 나에게 약간의 도움 만, 나는 비틀 거리는 다른 사람들에게 대안을 제시하고자했다. config.ru을 직접 수정하고 websocket-rack 응용 프로그램을 탑재하기로 결정했습니다. 지금

#!/usr/bin/env rackup 
# encoding: utf-8 

# This file can be used to start Padrino, 
# just execute it from the command line. 

require File.expand_path("../config/boot.rb", __FILE__) 

# Setup routes 
map '/' do 
    run Padrino.application 
end 
map '/ws' do 
    run WSApp.new 
end 
0

이 구글에서 최고의 히트이기 때문에, 내가 ':

은 여기 내 WSApp이 Rack::WebSocket::Application의 서브 클래스이며, (따라서 자동으로 대부가에 의해로드)를 lib/ 디렉토리에 위치 config.ru있어 Padrino에 웹 소켓 애플리케이션을 작성하기위한 깨끗한 DSL 인 padrino-websockets에 연결하고 싶습니다.

관련 문제