diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index efb413b32..b9b94f28a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -28,4 +28,5 @@ module ApplicationHelper puts "Not of type post" end end + end diff --git a/app/models/post.rb b/app/models/post.rb index af11641fe..39dedfe4c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,6 +1,6 @@ class Post require_relative '../../lib/common' - + include ApplicationHelper # XML accessors must always preceed mongo field tags @@ -19,6 +19,8 @@ class Post before_create :set_defaults + after_save :send_to_view + @@models = ["StatusMessage", "Bookmark", "Blog"] def self.stream @@ -38,6 +40,10 @@ class Post protected + def send_to_view + WebSocket.update_clients (self.to_json) + end + def set_defaults user_email = User.first.email self.owner ||= user_email diff --git a/app/views/dashboard/socket.erb b/app/views/dashboard/socket.erb index f33b3c49d..ca397440b 100644 --- a/app/views/dashboard/socket.erb +++ b/app/views/dashboard/socket.erb @@ -1,6 +1,5 @@ - @@ -24,4 +23,4 @@
- \ No newline at end of file + diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 7f48e98b5..c4f0a6866 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -11,6 +11,7 @@ = javascript_include_tag 'jquery142' /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" = javascript_include_tag 'rails' + = javascript_include_tag 'socket' = csrf_meta_tag = yield(:head) @@ -40,3 +41,6 @@ - flash.each do |name, msg| = content_tag :div, msg, :id => "flash_#{name}" = yield + + #debug + .msg diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index 3d1978f71..8e1193f3e 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -1,10 +1,39 @@ require 'em-websocket' require 'eventmachine' -EM.next_tick { - EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| - ws.onopen { ws.send "Hello Client!"} - ws.onmessage { |msg| ws.send "Pong: #{msg}" } - ws.onclose { puts "WebSocket closed" } +module WebSocket + + #mattr_accessor :channel + + EM.next_tick { + EM.add_timer(0.1) do + #puts "channel set" + @channel = EM::Channel.new + end + + EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| + ws.onopen { + sid = @channel.subscribe { |msg| ws.send msg } + #@channel.push "#{sid} connectdfged!" + puts @channel.inspect + + ws.onmessage { |msg| + @channel.push "#{msg}" + } + + ws.onclose { + @channel.unsubscribe(sid) + } + + } + end +} + #this should get folded into message queue i think? + def self.update_clients(json) + #EM.add_timer(5) do + #puts @channel + @channel.push json + #end end -} \ No newline at end of file + +end diff --git a/public/javascripts/socket.js b/public/javascripts/socket.js index e9621770f..a4d6e04bb 100644 --- a/public/javascripts/socket.js +++ b/public/javascripts/socket.js @@ -6,7 +6,7 @@ $(document).ready(function(){ ws.onclose = function() { debug("socket closed"); }; ws.onopen = function() { debug("connected..."); - ws.send("hello server"); - ws.send("hello again"); + //ws.send("hello server"); + // ws.send("hello again"); }; });