我正在寻找一种在Padrino应用程序中打开和使用websockets的方法。我知道Padrino使用单线程工作,但我正在寻找一种方法来打开websockets,并在其"onopen“、"onclose”"onmessage“方法和Padrino控制器之间共享变量。
你知道它是怎么做到的吗?
我看过的链接:
Examples of Eventmachine usage with Padrino and Sinatra (只有Sinatra为我工作) em-websocket on GitHub
更新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/ruby-1.9.2-p290@runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `require': no such file to load -- daemons (LoadError)
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `<top (required)>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/gems/thin-1.2.11/lib/thin/server.rb:50:in `<class:Server>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/gems/thin-1.2.11/lib/thin/server.rb:48:in `<module:Thin>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/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/ruby-1.9.2-p290@runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290@runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from main.rb:9:in `<main>'更新2:多亏了Nathan!我刚刚在Gemfile中添加了“守护进程”并重新加载了我的应用程序。
发布于 2011-09-21 17:39:29
也许你需要安装守护进程:
编辑Gemfile:
# Adding this
gem 'daemons'安装缺少的gem:
$ bundle install发布于 2011-09-21 17:32:52
这个例子中有什么特别之处:https://github.com/igrigorik/em-websocket和Any success with Sinatra working together with EventMachine WebSockets?不能与Padrino一起工作,但能与Sinatra一起工作?你能解释一下你得到的错误和这些例子失败的原因(Stacktrace)吗?也许我们能帮上忙。
发布于 2012-10-26 23:13:02
我偶然发现了这篇文章,它对我有一点帮助,但我想为其他任何可能偶然发现它的人提供一个替代的解决方案。我选择直接修改config.ru并挂载websocket-rack应用程序。
下面是我的config.ru,其中WSApp是Rack::WebSocket::Application的子类,放在lib/目录中(因此由Padrino自动加载):
#!/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
endhttps://stackoverflow.com/questions/7497441
复制相似问题