Screencast: Einfaches Messaging mit Faye

 

Download:

Download(17.9 MB, 13:42)
Alternativer Download für iPod & Apple TV(18.5 MB, 13:42)

 

Resourcen:

 

Quellcode:

gem install faye
rackup faye.ru -s thin -E production
curl http://localhost:9292/faye -d 'message={"channel":"/messages/new", "data":"hello"}'
# config/application.rb
require "net/http"

# config/initializers/faye_token.rb
FAYE_TOKEN = "anything"

# application_helper.rb
def broadcast(channel, &block)
  message = {:channel => channel, :data => capture(&block), :ext => {:auth_token => FAYE_TOKEN}}
  uri = URI.parse("http://localhost:9292/faye")
  Net::HTTP.post_form(uri, :message => message.to_json)
end

# faye.ru
require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)

class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token'
      end
    end
    callback.call(message)
  end
end

faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
run faye_server
<% broadcast "/messages/new" do %>
  $("#chat").append("<%= escape_javascript render(@message) %>");
<% end %>
$("#new_message")[0].reset();
/* application.js */
$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
  faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>