首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >padrino && websockets

padrino && websockets
EN

Stack Overflow用户
提问于 2011-09-21 17:22:52
回答 4查看 1.9K关注 0票数 3

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

你知道它是怎么做到的吗?

我看过的链接:

Examples of Eventmachine usage with Padrino and Sinatra (只有Sinatra为我工作) em-websocket on GitHub

更新1:这是我的main.rb:

代码语言:javascript
复制
    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

结束

我得到了这个异常:

代码语言:javascript
复制
/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中添加了“守护进程”并重新加载了我的应用程序。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-09-21 17:39:29

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

编辑Gemfile:

代码语言:javascript
复制
# Adding this
gem 'daemons'

安装缺少的gem:

代码语言:javascript
复制
$ bundle install
票数 3
EN

Stack Overflow用户

发布于 2011-09-21 17:32:52

这个例子中有什么特别之处:https://github.com/igrigorik/em-websocketAny success with Sinatra working together with EventMachine WebSockets?不能与Padrino一起工作,但能与Sinatra一起工作?你能解释一下你得到的错误和这些例子失败的原因(Stacktrace)吗?也许我们能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2012-10-26 23:13:02

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

下面是我的config.ru,其中WSApp是Rack::WebSocket::Application的子类,放在lib/目录中(因此由Padrino自动加载):

代码语言:javascript
复制
#!/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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7497441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档