帕德里诺&&网络套接字

发布于 2024-12-05 19:49:06 字数 2845 浏览 0 评论 0原文

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

知道怎么做吗?

我查看的链接:

Eventmachine 与 Padrino 和 Sinatra 的使用示例< /a> (只有 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

end

我收到此异常:

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

i'm looking for a way to open and use websockets from within a Padrino application. i know Padrino works with a single thread but i'm looking for a way to open websockets and share variables between its "onopen" "onclose" "onmessage" methods and Padrino controllers.

any idea how it's done ?

links i looked into:

Examples of Eventmachine usage with Padrino and Sinatra (only Sinatra worked for me)
em-websocket on GitHub

UPDATE 1:
this is my 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

end

i'm getting this exception:

/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>'

UPDATE 2:
Resolved thanks to Nathan !
I just added 'daemons' to Gemfile and reloaded my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏了南城 2024-12-12 19:49:06

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

编辑您的 Gemfile:

# Adding this
gem 'daemons'

安装缺少的 gem:

$ bundle install

Maybe you need to install daemons:

Edit your Gemfile:

# Adding this
gem 'daemons'

Install missing gems:

$ bundle install
伏妖词 2024-12-12 19:49:06

这个例子特别说明了什么: https://github.com/igrigorik/em-websocket任何成功Sinatra 与 EventMachine WebSockets 一起工作? 没有与 Padrino 一起工作,但与 Sinatra 一起工作吗?您能解释一下您遇到的错误以及这些示例失败的原因(堆栈跟踪)吗?也许我们可以帮忙调查一下。

What in particular from this example: https://github.com/igrigorik/em-websocket and Any success with Sinatra working together with EventMachine WebSockets? didn't work with Padrino but did with Sinatra? Can you explain the errors you got and why those examples failed (stacktraces)? Maybe we can help investigate.

苏辞 2024-12-12 19:49:06

我偶然发现了这篇文章,它对我有一些帮助,但我想为其他可能偶然发现它的人提供替代解决方案。我选择直接修改 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

I ran across this post and it helped me a bit, but I wanted to offer an alternative solution to anyone else who might stumble upon it. I chose to just directly modify the config.ru and mount a websocket-rack application.

Here's my config.ru where WSApp is a subclass of Rack::WebSocket::Application and is placed in the lib/ directory (therefore being automatically loaded by 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
原来是傀儡 2024-12-12 19:49:06

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

Since this is the top hit in Google right now, I'd like to link it to padrino-websockets, a clean DSL for writing websockets applications in Padrino.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文