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"
|
puts "Not of type post"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
class Post
|
class Post
|
||||||
require_relative '../../lib/common'
|
require_relative '../../lib/common'
|
||||||
|
include ApplicationHelper
|
||||||
|
|
||||||
# XML accessors must always preceed mongo field tags
|
# XML accessors must always preceed mongo field tags
|
||||||
|
|
||||||
|
|
@ -19,6 +19,8 @@ class Post
|
||||||
|
|
||||||
before_create :set_defaults
|
before_create :set_defaults
|
||||||
|
|
||||||
|
after_save :send_to_view
|
||||||
|
|
||||||
@@models = ["StatusMessage", "Bookmark", "Blog"]
|
@@models = ["StatusMessage", "Bookmark", "Blog"]
|
||||||
|
|
||||||
def self.stream
|
def self.stream
|
||||||
|
|
@ -38,6 +40,10 @@ class Post
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def send_to_view
|
||||||
|
WebSocket.update_clients (self.to_json)
|
||||||
|
end
|
||||||
|
|
||||||
def set_defaults
|
def set_defaults
|
||||||
user_email = User.first.email
|
user_email = User.first.email
|
||||||
self.owner ||= user_email
|
self.owner ||= user_email
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<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/swfobject.js'></script>
|
||||||
<script src='javascripts/FABridge.js'></script>
|
<script src='javascripts/FABridge.js'></script>
|
||||||
<script src='javascripts/web_socket.js'></script>
|
<script src='javascripts/web_socket.js'></script>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
= javascript_include_tag 'jquery142'
|
= javascript_include_tag 'jquery142'
|
||||||
/= 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.1/jquery.min.js"
|
||||||
= javascript_include_tag 'rails'
|
= javascript_include_tag 'rails'
|
||||||
|
= javascript_include_tag 'socket'
|
||||||
= csrf_meta_tag
|
= csrf_meta_tag
|
||||||
= yield(:head)
|
= yield(:head)
|
||||||
|
|
||||||
|
|
@ -40,3 +41,6 @@
|
||||||
- flash.each do |name, msg|
|
- flash.each do |name, msg|
|
||||||
= content_tag :div, msg, :id => "flash_#{name}"
|
= content_tag :div, msg, :id => "flash_#{name}"
|
||||||
= yield
|
= yield
|
||||||
|
|
||||||
|
#debug
|
||||||
|
.msg
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,39 @@
|
||||||
require 'em-websocket'
|
require 'em-websocket'
|
||||||
require 'eventmachine'
|
require 'eventmachine'
|
||||||
|
|
||||||
EM.next_tick {
|
module WebSocket
|
||||||
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
|
|
||||||
ws.onopen { ws.send "Hello Client!"}
|
#mattr_accessor :channel
|
||||||
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
|
|
||||||
ws.onclose { puts "WebSocket closed" }
|
EM.next_tick {
|
||||||
end
|
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.onclose = function() { debug("socket closed"); };
|
||||||
ws.onopen = function() {
|
ws.onopen = function() {
|
||||||
debug("connected...");
|
debug("connected...");
|
||||||
ws.send("hello server");
|
//ws.send("hello server");
|
||||||
ws.send("hello again");
|
// ws.send("hello again");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue