diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index e0aa18da091d3b8a92826c7ed7b883544d6ae27a..ce821988d5f4fd150d819d8c21ef05915072286a 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -5,6 +5,7 @@ class DashboardController < ApplicationController def index @posts = Post.paginate :page => params[:page], :order => 'created_at DESC' + puts session.inspect end diff --git a/app/controllers/socket_controller.rb b/app/controllers/socket_controller.rb index a5c91f89f13fca25e7384796a28ef10527f16837..c3c2df8f949fbb8eacca4ff30b6b82d24f7f1e99 100644 --- a/app/controllers/socket_controller.rb +++ b/app/controllers/socket_controller.rb @@ -2,13 +2,16 @@ class SocketController < ApplicationController include ApplicationHelper include SocketHelper include Rails.application.routes.url_helpers + before_filter :authenticate_user! - def default_url_options() - {:host=> 'example.com'} - end + + + # def default_url_options() + # {:host=> 'example.com'} + # end def incoming(msg) - puts msg + puts "#{msg} connected!" end def new_subscriber @@ -16,32 +19,18 @@ class SocketController < ApplicationController end def outgoing(object) - puts "made it sucka" + begin + @_request = ActionDispatch::Request.new(:socket => true) WebSocket.push_to_clients(action_hash(object)) + rescue Exception => e + puts e.inspect + raise e + end end def delete_subscriber(sid) WebSocket.unsubscribe(sid) end - - - -# need a data strucutre to keep track of who is where - -#the way this is set up now, we have users on pages - -#could have... a channel for every page/collection...not that cool -#or, have a single channel, which has a corresponding :current page => [sid] -# can i cherry pick subscribers from a a channel? - - -# we want all sorts of stuff that comes with being a controller -# like, protect from forgery, view rendering, etc - - -#these functions are not really routes -#so the question is, whats the best way to call them? -#also, this is an input output controller end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index dbceffe7859e025955175f6672cc47d179c29d61..c476210ab1a1dbb877e3685c61f8d4af1db0e9cd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -38,8 +38,8 @@ module ApplicationHelper end def owner_picture - default = "#{root_url}images/user/default.jpg" - image = "#{root_url}images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg" + default = "/images/user/default.jpg" + image = "/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg" if File.exist?("public/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg") image_tag image, :id => "user_picture" diff --git a/app/helpers/socket_helper.rb b/app/helpers/socket_helper.rb index fa996d811fc550eb3f70fa5fdb7e300581520707..fbdd37f7a539139886f9a47497d996bf690a0075 100644 --- a/app/helpers/socket_helper.rb +++ b/app/helpers/socket_helper.rb @@ -13,14 +13,15 @@ module SocketHelper begin v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction + rescue Exception => e - puts "in failzord " + v.inspect + puts "web socket view rendering failed for some reason." + v.inspect puts object.inspect puts e.message raise e end - {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} + {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}.to_json end diff --git a/app/models/post.rb b/app/models/post.rb index b8eb39dc3d40ea6b360c2e04f85979c414e2ff04..266e16d0b73ecfede3092e7d755d3787865d3e68 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -53,11 +53,11 @@ class Post end def send_to_view - WebSocket.push_to_clients(self) + SocketController.new.outgoing(self) end def remove_from_view - WebSocket.push_to_clients(Retraction.for(self)) + SocketController.new.outgoing(Retraction.for(self)) end end diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 93286423cc7e64135551d91c17a750f912adbecd..a4ec035e209762736ae6aca4f0622ef90b65f2f6 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -8,7 +8,7 @@ function debug(str){ $("#debug").append("<p>" + str); }; ws = new WebSocket("ws://#{request.host}:8080/"); - ws.onmessage = function(evt) { + ws.onmessage = function(evt) { var obj = jQuery.parseJSON(evt.data); debug("got a " + obj['class']); diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index ac79810fd624b020b25cead5aeba78fff23aec89..badb33c6f4d8ca4c4a751821c056ec50849a2b05 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -13,7 +13,7 @@ module WebSocket :debug =>APP_CONFIG[:debug]) do |ws| ws.onopen { @ws = ws - sid = SocketController.new.new_subscriber(ws) + sid = SocketController.new.new_subscriber ws.onmessage { |msg| SocketController.new.incoming(msg) }#@channel.push msg; puts msg} @@ -24,10 +24,10 @@ module WebSocket def self.initialize_channel @channel = EM::Channel.new - puts @channel.inspect end def self.push_to_clients(html) + puts html @channel.push(html) end @@ -37,7 +37,7 @@ module WebSocket def self.subscribe - @channel.subscribe{ |msg| puts "hello #{msg}";@ws.send msg } + @channel.subscribe{ |msg| puts @ws.inspect; puts "ehllo" ; @ws.send msg } end end diff --git a/lib/socket_render.rb b/lib/socket_render.rb deleted file mode 100644 index 6f8bcbad95565b12a535dff59487309b7aaa190e..0000000000000000000000000000000000000000 --- a/lib/socket_render.rb +++ /dev/null @@ -1,40 +0,0 @@ -# module SocketRenderer -# require 'app/helpers/application_helper' -# def self.instantiate_view -# @view = ActionView::Base.new(ActionController::Base.view_paths, {}) -# class << @view -# include ApplicationHelper -# include Rails.application.routes.url_helpers -# include ActionController::RequestForgeryProtection::ClassMethods -# def protect_against_forgery? -# false -# end -# end -# end -# -# def self.view_hash(object) -# begin -# v = view_for(object) unless object.is_a? Retraction -# -# rescue Exception => e -# puts "in failzord " + v.inspect -# puts object.inspect -# puts e.message -# raise e -# end -# -# {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} -# end -# -# def self.view_for(object) -# @view.render @view.type_partial(object), :post => object -# end -# -# def self.obj_id(object) -# if object.is_a? Post -# object.id -# else -# object.post_id -# end -# end -# end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8a8b09e507aea27b4ca69a5f0297e61ee283f8bd..268305b3201c02a41f40f8d086d729caa5c96397 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,6 +24,12 @@ RSpec.configure do |config| config.before(:each) do DatabaseCleaner.start + SocketController.stub!(:incoming).and_return(true) + SocketController.stub!(:new_subscriber).and_return(true) + SocketController.stub!(:outgoing).and_return(true) + SocketController.stub!(:delete_subscriber).and_return(true) + + WebSocket.stub!(:push_to_clients).and_return("stub") WebSocket.stub!(:unsubscribe).and_return("stub") WebSocket.stub!(:subscribe).and_return("stub")