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'를 추가하고 내 응용 프로그램을 다시로드했습니다.
Gemfile에 추가하십시오 :'gem 'daemons'' 일반적으로이 오류는 나열된 gem이 gemset에 설치되어 있지 않거나 Gemfile에 설치되어 있지 않다는 것을 의미합니다. – Nathan
참고로이 예외를 생성하는 행은 단순히 데몬 라이브러리를로드하려고 시도하는 것입니다. https://github.com/macournoyer/thin/blob/v1.2.1/lib/thin/daemonizing.rb#L2 – Nathan
보석. did not work .... 나는 방금 다시 시도해 보았다. 하나님은 신비한 방식으로 일합니다 :) 고맙습니다 !!! – refaelos