Padrino和WebSockets

3
我正在寻找一种在Padrino应用程序中打开和使用WebSockets的方法。我知道Padrino只能使用单个线程,但我正在寻找一种打开WebSockets并在其“onopen”、“onclose”和“onmessage”方法与Padrino控制器之间共享变量的方法。
有什么想法吗?
我查看了以下链接:
- Eventmachine在Padrino和Sinatra中的用法示例(只有Sinatra适用于我) - GitHub上的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中添加“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
我添加了那个宝石,但是没有起作用...我刚刚又尝试了一次,现在它可以工作了。上帝的方式真是神秘 :) 非常感谢!!! - refaelos
4个回答

3
也许你需要安装守护进程:
编辑你的 Gemfile:
# Adding this
gem 'daemons'

安装缺失的gems:

$ bundle install

1
可以尝试在Gemfile中添加gem 'daemons',然后运行bundle install - Nathan

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

1

0

由于这是目前谷歌搜索排名最高的内容,我想将其链接到padrino-websockets,这是一个在Padrino中编写WebSockets应用程序的清晰DSL。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接