将Faye与Sinatra整合在一个文件中的简单方法?

时间:2012-12-16 18:32:28

标签: ruby websocket sinatra faye

有没有一种简单的方法可以将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

这样当我运行“ruby myapp.rb”时,我会在一个进程中运行Faye和Sinatra服务器(我猜他们会在同一个端口上?)?

或:最接近这一点的是什么,目的是以最“简约”的方式将Faye融入Sinatra?

更新:修复了示例代码,因此看起来我并不感兴趣让Faye和Sinatra在同一条路径下;)

2 个答案:

答案 0 :(得分: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。你也可以在/安装faye,在其他地方安装你的Sinatra应用程序,虽然我认为你需要创建一个“模块化”的Sinatra应用程序才能做到这一点,然后像:

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

在你的例子中,你有点试图让它们都在同一条路径下,我认为这不会起作用。

答案 1 :(得分:1)

我已经创建了一个关于Sinatra和Faye如何简单合作的例子。您可以在此处查看: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>