1

次のような単一のファイルにFayeとSinatraを統合する簡単な方法はありますか?

# myapp.rb
require 'sinatra'
require 'faye'

get '/' do 
  'Hello world!' 
end

get '/faye_form' do
  erb :form_and_message_list
end

post '/faye_form' do
  erb :form_and_message_list
  # will send messages to faye's channel here.
end

ruby myapp.rb

「rubymyapp.rb」を実行すると、FayeサーバーとSinatraサーバーが単一のプロセスで実行されるようになります(これらは同じポート上にあると思いますか?)?

または:最も「ミニマルな」方法でシナトラにフェイを統合する目的で、これに最も近いものは何ですか?

更新:サンプルコードを修正し、FayeとSinatraを同じパスに配置することに興味がないようにしました;)

4

2 に答える 2

1

http://faye.jcoglan.com/ruby.htmlから適応

# config.ru
require 'sinatra'
require 'faye'
require File.expand_path('../app', __FILE__)

use Faye::RackAdapter, :mount => '/faye', :timeout => 25

run Sinatra::Application

SinatraとFayeはどちらもラックアプリであり、Fayeのように異なるパスで実行する必要があります/faye。また、フェイとSinatraアプリを別の場所にマウントすることもでき/ますが、そのためには「モジュラー」Sinatraアプリを作成する必要があると思います。その後、次のようになります。

run Rack::URLMap.new("/app" => MyApp.new)

あなたの例では、両方を同じパスの下に配置しようとしているのですが、これはうまくいかないと思います。

于 2012-12-17T06:14:34.120 に答える
1

シナトラとフェイが簡単に連携する方法の例を作成しました。ここで表示できます: https ://github.com/eterry1388/sinatra-faye-example

あなたconfig.ruはこのように見えるかもしれません:

require 'faye'
require 'sinatra'

set :port, 9292
set :faye_client, Faye::Client.new( 'http://localhost:9292/faye' )
set :saved_data, Hash.new( [] )

get '/' do
  # This loads the saved data so if the web page is refreshed
  # or a new client connects, the data persists.
  @saved_data = settings.saved_data
  erb :index
end

post '/' do
  channel = params['channel']
  message = params['message']

  # Send data out to connected clients
  settings.faye_client.publish( channel, message )

  # Save data for future clients
  settings.saved_data[channel] += [message]

  redirect to( '/' )
end

Faye::WebSocket.load_adapter 'thin'
use Faye::RackAdapter, mount: '/faye', timeout: 45, extensions: []
run Sinatra::Application

そして、あなたindex.erbはこのように見えるかもしれません:

<script type="text/javascript" src="http://localhost:9292/faye/client.js"></script>

<script type="text/javascript">
  var faye_client = new Faye.Client( 'http://localhost:9292/faye' );

  faye_client.subscribe( '/blue', function( data ) {
    var div = $( 'ul#blue-messages' ).append( '<li>' + data + '</li>' );
  });

  faye_client.subscribe( '/green', function( data ) {
    var div = $( 'ul#green-messages' ).append( '<li>' + data + '</li>' );
  });

  $( document ).ready( function() {
    $( 'button#submit' ).click( function() {
      $.post('/',
      $( 'form#send-message' ).serialize(),
      function() {
        $( 'textarea#message' ).val( '' );
      });
    });
  });
</script>

<h2>Send message</h2>

<form id="send-message">
  <label for="channel">Channel</label>
  <select name="channel" id="channel" class="form-control">
    <option>/blue</option>
    <option>/green</option>
  </select>
  <label for="message">Message</label>
  <textarea name="message" id="message" class="form-control"></textarea>
</form>
<button id="submit" class="btn btn-success">Send</button>

<h2>Blue messages</h2>
<ul id="blue-messages">
  <%
    # The below block is only used for loading historical
    # saved data, not the Faye data!
  %>
  <% @saved_data['/blue'].each do |message| %>
    <li><%= message %></li>
  <% end %>
</ul>

<h2>Green messages</h2>
<ul id="green-messages">
  <%
    # The below block is only used for loading historical
    # saved data, not the Faye data!
  %>
  <% @saved_data['/green'].each do |message| %>
    <li><%= message %></li>
  <% end %>
</ul>
于 2016-01-19T22:53:13.957 に答える