i trying using faye following tutorial
not getting desired output.i getting chat window opening on localhost:3000 .the messages typing not going other side....from side showing going other side ...i have follow tutorial don't know missing
[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; } img { border: none; } { 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 given objects. :message , :header_message options 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 following errors , try again.") messages = objects.compact.map { |o| o.errors.full_messages }.flatten unless messages.empty? content_tag(:div, :class => "error_messages") 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
[layout/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 |name, msg| %> <%= content_tag :div, msg, :id => "flash_#{name}" %> <% end %> <%= content_tag :h1, yield(:title) if show_title? %> <%= yield %> </div> </body> </html>
[messages/_messages.html.erb]
<li> <span class="created_at"><%= message.created_at.strftime("%h:%m") %></span> <%= message.content %> </li>
[messages/create.js.erb]
<% broadcast "/messages/new" %> $("#chat").append("<%= escape_javascript render(@message) %>"); <% end %> $("#new_message")[0].reset();
[messages/index.html.erb]
<% title "chat" %> <ul id="chat"> <%= render @messages %> </ul> <%= form_for message.new, :remote => true |f| %> <%= f.text_field :content %> <%= f.submit "send" %> <% end %>
[faye_token.rb]
faye_token = "anything"
[config.ru]
# file used rack-based servers start 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
Comments
Post a Comment