From f4a8671854b6e0ed60c32253a45c18088f6a726a Mon Sep 17 00:00:00 2001 From: maxwell Date: Mon, 28 Jun 2010 16:15:30 -0700 Subject: [PATCH 1/2] fixed the remote socket updating bug? we will see --- app/models/post.rb | 3 +-- app/views/comments/_comments.html.haml | 2 +- app/views/layouts/application.html.haml | 1 + .../status_messages/_status_message.html.haml | 2 +- config/initializers/socket.rb | 18 +++++++++++------- lib/message_handler.rb | 5 +---- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 683083b30..4567537c9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -43,8 +43,7 @@ class Post protected def send_to_view - self.reload - WebSocket.update_clients(self) + WebSocket.update_clients(self) end diff --git a/app/views/comments/_comments.html.haml b/app/views/comments/_comments.html.haml index 50af21178..374f585e5 100644 --- a/app/views/comments/_comments.html.haml +++ b/app/views/comments/_comments.html.haml @@ -1,5 +1,5 @@ %div.comments - /= render "comments/new_comment", :post => post + = render "comments/new_comment", :post => post %ul.comment_set - for comment in post.comments = render "comments/comment", :comment => comment diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 399d69c06..42b1577d7 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -30,6 +30,7 @@ ws = new WebSocket("ws://#{request.host}:8080/"); ws.onmessage = function(evt) { var obj = jQuery.parseJSON(evt.data); + debug("got a " + obj['class']); if((location.href.indexOf(obj['class']) != -1 ) || (location.pathname == '/')) { $("#stream").prepend($(obj['html']).fadeIn("fast")); }; diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index f9ec3c03b..5e643efae 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -4,7 +4,7 @@ = post.message %div.time = link_to(how_long_ago(post), status_message_path(post)) - = render "comments/comments", :post => post + /= render "comments/comments", :post => post - if mine?(post) = link_to 'Destroy', status_message_path(post), :confirm => 'Are you sure?', :method => :delete diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index b3542cf34..52c8ea3d9 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -5,20 +5,21 @@ module WebSocket EM.next_tick { EM.add_timer(0.1) do @channel = EM::Channel.new + puts @channel.inspect @view = ActionView::Base.new(ActionController::Base.view_paths, {}) class << @view include ApplicationHelper include Rails.application.routes.url_helpers - include ActionView::Helpers::FormTagHelper + #include ActionView::Helpers::FormTagHelper end end - EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug =>false) do |ws| + EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug =>true) do |ws| ws.onopen { sid = @channel.subscribe { |msg| ws.send msg } - ws.onmessage { |msg|}#@channel.push msg; puts msg} + ws.onmessage { |msg| }#@channel.push msg; puts msg} ws.onclose { @channel.unsubscribe(sid) } } @@ -33,10 +34,13 @@ module WebSocket begin puts "I be working hard" v = WebSocket.view_for(object) - puts v.inspect - rescue - puts "in failzord " + v .inspect - raise "i suck" + puts v.inspect + + rescue Exception > e + puts "in failzord " + v.inspect + puts object.inspect + puts e.inspect + raise e end {:class =>object.class.to_s.underscore.pluralize, :html => v} diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 8a7845e4f..91226a426 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -13,9 +13,6 @@ class MessageHandler def add_post_request(destinations, body) - puts "yay" - puts destinations.inspect - puts body.inspect destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} end @@ -24,7 +21,7 @@ class MessageHandler case query.type when :post http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} - http.callback {puts query.body; process} + http.callback {process} when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http.callback {send_to_seed(query, http.response); process} From 36b5a90d340e88ad039e246e9fe0f7472c3bf679 Mon Sep 17 00:00:00 2001 From: maxwell Date: Mon, 28 Jun 2010 17:35:38 -0700 Subject: [PATCH 2/2] MS figured out how to get comments to come thru the websocket. this is super super super hacky, and kind of terrible, but it works --- app/helpers/status_messages_helper.rb | 2 +- app/views/comments/_new_comment.html.haml | 5 ++--- app/views/layouts/application.html.haml | 2 +- app/views/status_messages/_status_message.html.haml | 2 +- config/initializers/socket.rb | 13 +++++++++---- public/javascripts/view.js | 1 + 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 32a742d9e..2e933d784 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -3,7 +3,7 @@ module StatusMessagesHelper def my_latest_message message = StatusMessage.my_newest unless message.nil? - return message.message + " " + how_long_ago(message) + return message.message + " - " + how_long_ago(message) else return "No message to display." end diff --git a/app/views/comments/_new_comment.html.haml b/app/views/comments/_new_comment.html.haml index 7cfc43756..bb16dffbb 100644 --- a/app/views/comments/_new_comment.html.haml +++ b/app/views/comments/_new_comment.html.haml @@ -1,6 +1,5 @@ -= form_tag("/comments",:remote => true, :class =>"new_comment", :id => "new_comment") do += form_tag("/comments", :remote => true, :class =>"new_comment", :id => "new_comment-#{post.id}") do %p = text_field_tag "comment_text", 'dislike!', :size => 30, :name => 'comment[text]' = hidden_field_tag "comment_post_id", "#{post.id}", :name => "comment[post_id]" - = submit_tag 'comment', :id => "comment_submit", :name => "commit" - + = submit_tag 'comment', :id => "comment_submit_#{post.id}", :name => "commit" diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 42b1577d7..31b14138f 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -8,7 +8,7 @@ = stylesheet_link_tag "blueprint/screen", :media => 'screen' = stylesheet_link_tag "application" - /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" + /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" = javascript_include_tag 'jquery142', 'rails', 'view' = javascript_include_tag 'tiny_mce/tiny_mce.js' diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index 5e643efae..f9ec3c03b 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -4,7 +4,7 @@ = post.message %div.time = link_to(how_long_ago(post), status_message_path(post)) - /= render "comments/comments", :post => post + = render "comments/comments", :post => post - if mine?(post) = link_to 'Destroy', status_message_path(post), :confirm => 'Are you sure?', :method => :delete diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index 52c8ea3d9..61fb85d59 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -11,7 +11,12 @@ module WebSocket class << @view include ApplicationHelper include Rails.application.routes.url_helpers - #include ActionView::Helpers::FormTagHelper + include ActionController::RequestForgeryProtection::ClassMethods + include ActionView::Helpers::FormTagHelper + include ActionView::Helpers::UrlHelper + def protect_against_forgery? + false + end end end @@ -36,13 +41,13 @@ module WebSocket v = WebSocket.view_for(object) puts v.inspect - rescue Exception > e + rescue Exception => e puts "in failzord " + v.inspect puts object.inspect - puts e.inspect + puts e.message raise e end - + puts "i made it here" {:class =>object.class.to_s.underscore.pluralize, :html => v} end diff --git a/public/javascripts/view.js b/public/javascripts/view.js index 3593fa4c6..31ab2d57e 100644 --- a/public/javascripts/view.js +++ b/public/javascripts/view.js @@ -30,6 +30,7 @@ $(document).ready(function(){ $('#bookmark_title').click(clearForm); $('#bookmark_link').click(clearForm); + $('#debug_more').hide(); function clearForm(){ $(this).val("");