2011-09-21 7 views
3

Padrinoアプリケーション内からウェブソケットを開いて使用する方法をお探しです。私はPadrinoが1つのスレッドで動作することを知っていますが、私は "onopen" "onclose" "onmessage"メソッドとPadrinoコントローラの間でWebSocketと変数を共有する方法を探しています。padrino && websockets

どうすればいいですか?私はに見えた

リンク:

Examples of Eventmachine usage with Padrino and Sinatra(のみシナトラが私のために働いていた) em-websocket on GitHub

UPDATE 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/[email protected]/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `require': no such file to load -- daemons (LoadError) 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `<top (required)>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/server.rb:50:in `<class:Server>' 
    from /home/cstore/.rvm/gems/[email protected]/gems/thin-1.2.11/lib/thin/server.rb:48:in `<module:Thin>' 
    from /home/cstore/.rvm/gems/[email protected]/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/[email protected]/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call' 
    from /home/cstore/.rvm/gems/[email protected]/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine' 
    from /home/cstore/.rvm/gems/[email protected]/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

+0

私はそれを追加しました宝石。 did not work ....私はもう一度試してみると、うまくいきました。神は神秘的な方法で働いています:)感謝! – refaelos

答えて

3

たぶん、あなたは、デーモンをインストールする必要があります。

編集しGemfile:

# Adding this 
gem 'daemons' 

行方不明の宝石をインストールします。

$ bundle install 
+1

Gemfile:gem 'daemons'に追加してみて、 'bundle install'を実行してください。 – Nathan

1

特にこの例から:https://github.com/igrigorik/em-websocketAny success with Sinatra working together with EventMachine WebSockets?は、Padrinoでは機能しませんでしたが、Sinatraではどうでしたか?あなたが持っているエラーとその失敗の理由(スタックトレース)を説明できますか?おそらく私たちは調査を手伝うことができます。

+0

私は私のポストを更新しました。 – refaelos

1

を、私はこの記事に出くわしましたし、それは私に少しを助けたが、私はそれにつまずくかもしれない他の誰かに代替のソリューションを提供したかったのです。 config.ruを直接変更し、websocket-rackアプリケーションをマウントすることにしました。

ここWSAPPがRack::WebSocket::Applicationのサブクラスであり、lib/ディレクトリ(したがって、自動的にPadrinoのでロードされている)に配置された私のconfig.ruです:これは今のグーグルのトップヒットであるので

#!/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 
0

、私は」 Padrinoのウェブソケットアプリケーションを書くためのきれいなDSLのpadrino-websocketsにリンクしています。