3

我正在寻找一种从Padrino应用程序中打开和使用 websockets 的方法。我知道 Padrino 使用单线程工作,但我正在寻找一种方法来打开 websockets 并在其“onopen”“onclose”“onmessage”方法和 Padrino 控制器之间共享变量。

知道它是怎么做的吗?

我查看的链接:

GitHub 上的事件机器与 Padrino 和 Sinatra 的使用示例(只有 Sinatra 为我工作) em-websocket

更新 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 并重新加载了我的应用程序。

4

4 回答 4

3

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

编辑您的 Gemfile:

# Adding this
gem 'daemons'

安装缺失的 gem:

$ bundle install
于 2011-09-21T09:39:29.213 回答
1

这个例子特别是什么:https ://github.com/igrigorik/em-websocket和Sinatra 与 EventMachine WebSockets 一起工作是否成功?没有与帕德里诺合作,但与西纳特拉合作?您能解释一下您遇到的错误以及这些示例失败的原因(堆栈跟踪)吗?也许我们可以帮助调查。

于 2011-09-21T09:32:51.770 回答
1

我遇到了这篇文章,它对我有所帮助,但我想为可能偶然发现它的其他人提供替代解决方案。我选择直接修改config.ru并挂载一个websocket-rack应用程序。

这是我的config.ruwhere 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
于 2012-10-26T15:13:01.597 回答
0

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

于 2014-04-28T10:48:42.413 回答