websockets work
This commit is contained in:
parent
3581f7a7d6
commit
655dd64a54
6 changed files with 50 additions and 11 deletions
|
|
@ -28,4 +28,5 @@ module ApplicationHelper
|
|||
puts "Not of type post"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
<html>
|
||||
<head>
|
||||
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
|
||||
<script src='javascripts/swfobject.js'></script>
|
||||
<script src='javascripts/FABridge.js'></script>
|
||||
<script src='javascripts/web_socket.js'></script>
|
||||
|
|
@ -24,4 +23,4 @@
|
|||
<div id="debug"></div>
|
||||
<div class="msg"></div>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue