diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index 438163fffe7c081961593602f1f47c00827b8c46..882cdca50f8feb9ce2875f2f769d31376fec10ea 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -5,5 +5,4 @@ class DashboardsController < ApplicationController def index @posts = Post.paginate :page => params[:page], :order => 'created_at DESC' end - - end +end diff --git a/app/controllers/sockets_controller.rb b/app/controllers/sockets_controller.rb index 02c28421adf3c6a3a1382e126d9f7a77b276c1be..eb4f57fc93c63fe87185d0d6299ec47108fe68ce 100644 --- a/app/controllers/sockets_controller.rb +++ b/app/controllers/sockets_controller.rb @@ -2,18 +2,14 @@ class SocketsController < ApplicationController include ApplicationHelper include SocketsHelper include Rails.application.routes.url_helpers - before_filter :authenticate_user! def incoming(msg) - puts "#{msg} connected!" + puts "Got a connection to: #{msg}" end - def outgoing(object) + def outgoing(uid,object) @_request = ActionDispatch::Request.new({}) - WebSocket.push_to_clients(action_hash(object)) + WebSocket.push_to_user(uid, action_hash(uid, object)) end - def delete_subscriber(sid) - WebSocket.unsubscribe(sid) - end end diff --git a/app/helpers/sockets_helper.rb b/app/helpers/sockets_helper.rb index 194f321ff90bdb7c23ba52e96c39b1eca5618f1c..749de4b41a26282b176a43e94206c7b84bb38ad3 100644 --- a/app/helpers/sockets_helper.rb +++ b/app/helpers/sockets_helper.rb @@ -9,9 +9,10 @@ module SocketsHelper {:host => ""} end - def action_hash(object) + def action_hash(uid, object) begin - v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction + user = User.first(:id => uid) + v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction rescue Exception => e Rails.logger.error("web socket view rendering failed for object #{object.inspect}.") raise e diff --git a/app/models/post.rb b/app/models/post.rb index 74fce88819ad296a2513d785fbdc4df016438945..b5c61f492a47f6ba0ece5358e16cdd19f16de6b5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -76,7 +76,10 @@ protected end def send_to_view - SocketsController.new.outgoing(self) + people_with_permissions.each{|f| + SocketsController.new.outgoing(f.owner_id, self) if f.owner_id + } + SocketsController.new.outgoing(person.owner_id, self) if person.owner_id end def remove_from_view diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 768c03cb7a3d38329468ce4ba6f70dd6a16d2818..793aea9dbf01e63f2ca5e32a1a1e00a80a801065 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -7,7 +7,9 @@ $(document).ready(function(){ function debug(str){ $("#debug").append("<p>" + str); }; - ws = new WebSocket("ws://#{request.host}:8080/"); + ws = new WebSocket("ws://#{request.host}:8080/#{CGI::escape(current_user.id.to_s)}"); + + //Attach onmessage to websocket ws.onmessage = function(evt) { var obj = jQuery.parseJSON(evt.data); debug("got a " + obj['class']); diff --git a/config/app_config.yml b/config/app_config.yml index 0c87b6b4c6ac5a22b3c90c3ed3b0190d7086f366..02f2b142046595b584817edd0445634ab7d71563 100644 --- a/config/app_config.yml +++ b/config/app_config.yml @@ -1,5 +1,5 @@ development: - debug: false + debug: true socket_port: 8080 pubsub_server: 'https://pubsubhubbub.appspot.com/' diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index e0019b45284d83ec748f2d826051a53e8a0bd69d..711883ae398d6ebe307d76065f9b68594583c5fb 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -3,33 +3,40 @@ require 'eventmachine' module WebSocket EM.next_tick { - initialize_channel + initialize_channels EventMachine::WebSocket.start( :host => "0.0.0.0", :port => APP_CONFIG[:socket_port], :debug =>APP_CONFIG[:debug]) do |ws| ws.onopen { - @ws = ws - sid = @channel.subscribe{ |msg| ws.send msg }#SocketsController.new.new_subscriber + + sid = self.subscribe(ws.request['Path'].gsub('/',''), ws) ws.onmessage { |msg| SocketsController.new.incoming(msg) }#@channel.push msg; puts msg} - ws.onclose { SocketsController.new.delete_subscriber(sid) } + ws.onclose { unsubscribe(ws.request['Path'].gsub('/',''), sid) } } end } - def self.initialize_channel - @channel = EM::Channel.new + def self.initialize_channels + @channels = {} end - def self.push_to_clients(html) - @channel.push(html) + def self.push_to_user(uid, data) + puts "Pushing to #{uid}" + @channels[uid.to_s].push(data) if @channels[uid.to_s] end - def self.unsubscribe(sid) - @channel.unsubscribe(sid) + def self.subscribe(uid, ws) + puts "Subscribing #{uid}" + @channels[uid] ||= EM::Channel.new + @channels[uid].subscribe{ |msg| ws.send msg } + end + + def self.unsubscribe(uid,sid) + @channels[uid].unsubscribe(sid) if @channels[uid] end end diff --git a/spec/lib/socket_renderer_spec.rb b/spec/lib/socket_renderer_spec.rb deleted file mode 100644 index 009de8b78ab416718060708fd1a793c5ff34b145..0000000000000000000000000000000000000000 --- a/spec/lib/socket_renderer_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -=begin - require File.dirname(__FILE__) + '/../spec_helper' - -describe SocketRenderer do - before do - SocketRenderer.instantiate_view - @user = Factory.create(:user, :email => "bob@jones.com") - @user.profile = Factory.create(:profile, :person => @user) - end - - it 'should render a partial for a status message' do - message = Factory.create(:status_message, :person => @user) - html = SocketRenderer.view_for message - html.include? message.message - end - - it 'should prepare a class/view hash' do - message = Factory.create(:status_message, :person => @user) - - hash = SocketRenderer.view_hash(message) - hash[:class].should == "status_messages" - end -end -=end