帕德里诺&&的WebSockets

时间:2011-09-21 09:22:52

标签: ruby sinatra websocket eventmachine padrino

我正在寻找一种在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: 感谢内森! 我刚刚将'守护进程'添加到Gemfile并重新加载了我的应用程序。

4 个答案:

答案 0 :(得分:3)

也许你需要安装守护进程:

编辑您的Gemfile:

# Adding this
gem 'daemons'

安装缺少的宝石:

$ bundle install

答案 1 :(得分:1)

这个例子有什么特别之处:https://github.com/igrigorik/em-websocketAny success with Sinatra working together with EventMachine WebSockets?不能与Padrino合作,而是与Sinatra合作?你能解释一下你得到的错误以及这些例子失败的原因(stacktraces)吗?也许我们可以帮助调查。

答案 2 :(得分:1)

我遇到过这篇帖子,它对我有所帮助,但我想为其他可能偶然发现它的人提供另一种解决方案。我选择直接修改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
end

答案 3 :(得分:0)

由于这是Google目前的热门话题,我想将其链接到padrino-websockets,这是一个用于在Padrino中编写websockets应用程序的干净DSL。