Rails faye不能正常工作

时间:2016-01-25 04:39:52

标签: ruby-on-rails faye

我正在尝试按照tutorial

使用faye


但是我没有得到所需的输出。我只得到一个聊天窗口,我在localhost:3000打开。我输入的消息不会转到另一边....从我这边显示它是去另一边......我完全按照教程但我不知道我错过了什么 [的application.js]

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
  faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});


[application.css]

body {
  background-color: #4B7399;
  font-family: Verdana, Helvetica, Arial;
  font-size: 14px;
}

a img {
  border: none;
}

a {
  color: #0000FF;
}

.clear {
  clear: both;
  height: 0;
  overflow: hidden;
}

#container {
  width: 75%;
  margin: 0 auto;
  background-color: #FFF;
  padding: 20px 40px;
  border: solid 1px black;
  margin-top: 20px;
}

#flash_notice, #flash_error, #flash_alert {
  padding: 5px 8px;
  margin: 10px 0;
}

#flash_notice {
  background-color: #CFC;
  border: solid 1px #6C6;
}

#flash_error, #flash_alert {
  background-color: #FCC;
  border: solid 1px #C66;
}

.fieldWithErrors {
  display: inline;
}

.error_messages {
  width: 400px;
  border: 2px solid #CF0000;
  padding: 0px;
  padding-bottom: 12px;
  margin-bottom: 20px;
  background-color: #f0f0f0;
  font-size: 12px;
}

.error_messages h2 {
  text-align: left;
  font-weight: bold;
  padding: 5px 10px;
  font-size: 12px;
  margin: 0;
  background-color: #c00;
  color: #fff;
}

.error_messages p {
  margin: 8px 10px;
}

.error_messages ul {
  margin: 0;
}

ul#chat {
  list-style: none;
  padding: 0;
  width: 300px;
  height: 150px;
  border: solid 1px #777;
  margin: 5px 0;
  overflow: auto;
}

ul#chat li {
  margin: 2px 5px;
  padding: 0;
}

ul#chat li .created_at {
  float: right;
  color: #777;
  font-size: 11px;
  padding-left: 4px;
  padding-top: 2px;
}

input#message_content {
  width: 240px;
}
<br>

[application_controller]

class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
  skip_before_filter :verify_authenticity_token, only: [:index]
end
<br>

[messages_controller]

class MessagesController < ApplicationController
  skip_before_filter :verify_authenticity_token, only: [:index]

  def index
    @messages = Message.all
  end

  def create
    @message = Message.create!(messages_params)
    redirect_to root_path
  end

  private
    def messages_params
        params.require(:message).permit(:message,:content)
    end
end


[application_helper]

module ApplicationHelper
     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
end

[error_message_helper]

module ErrorMessagesHelper


    # Render error messages for the given objects. The :message and :header_message options are allowed.
      def error_messages_for(*objects)
        options = objects.extract_options!
        options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
        options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.")
        messages = objects.compact.map { |o| o.errors.full_messages }.flatten
        unless messages.empty?
          content_tag(:div, :class => "error_messages") do
            list_items = messages.map { |msg| content_tag(:li, msg) }
            content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
          end
        end
      end

      module FormBuilderAdditions
        def error_messages(options = {})
          @template.error_messages_for(@object, options)
        end
      end
    end

    ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)


                 <br>

[layout_helper]

module LayoutHelper
  def title(page_title, show_title = true)
    content_for(:title) { h(page_title.to_s) }
    @show_title = show_title
  end

  def show_title?
    @show_title
  end

  def stylesheet(*args)
    content_for(:head) { stylesheet_link_tag(*args) }
  end

  def javascript(*args)
    content_for(:head) { javascript_include_tag(*args) }
  end
end


[布局/ application.html.erb]

<!DOCTYPE html>
<html>
<head>
  <title>Twilo</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
      <%= javascript_include_tag :defaults, "http://localhost:9292/faye.js" %>

  <%= csrf_meta_tags %>
</head>
<body>

    <div id="container">
      <% flash.each do |name, msg| %>
        <%= content_tag :div, msg, :id => "flash_#{name}" %>
      <% end %>
      <%= content_tag :h1, yield(:title) if show_title? %>
      <%= yield %>
    </div>

</body>
</html>


[消息/ _messages.html.erb]

<li>
  <span class="created_at"><%= message.created_at.strftime("%H:%M") %></span>
  <%= message.content %>
</li>


[消息/ create.js.erb]

<% broadcast "/messages/new" do %>
  $("#chat").append("<%= escape_javascript render(@message) %>");
<% end %>
$("#new_message")[0].reset();


[消息/ index.html.erb]

<% title "Chat" %>

<ul id="chat">
  <%= render @messages %>
</ul>

<%= form_for Message.new, :remote => true do |f| %>
  <%= f.text_field :content %>
  <%= f.submit "Send" %>
<% end %>


[faye_token.rb]

FAYE_TOKEN = "anything"


[config.ru]

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Rails.application


[faye.ru]

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

class ServerAuth
  def self.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

0 个答案:

没有答案