Moved socket code out to lib, unsocketing from view should now work again
This commit is contained in:
parent
144e9ed439
commit
599d1da620
6 changed files with 65 additions and 55 deletions
|
|
@ -9,7 +9,7 @@ class SocketsController < ApplicationController
|
|||
|
||||
def outgoing(uid,object)
|
||||
@_request = ActionDispatch::Request.new({})
|
||||
WebSocket.push_to_user(uid, action_hash(uid, object))
|
||||
Diaspora::WebSocket.push_to_user(uid, action_hash(uid, object))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ class Comment
|
|||
include ROXML
|
||||
include Diaspora::Webhooks
|
||||
include Encryptable
|
||||
include Diaspora::Socketable
|
||||
|
||||
xml_accessor :text
|
||||
xml_accessor :person, :as => Person
|
||||
|
|
@ -75,11 +76,4 @@ class Comment
|
|||
end
|
||||
end
|
||||
|
||||
def send_to_view
|
||||
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
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ class Post
|
|||
include ROXML
|
||||
include Diaspora::Webhooks
|
||||
include Encryptable
|
||||
include Diaspora::Socketable
|
||||
|
||||
xml_accessor :_id
|
||||
xml_accessor :person, :as => Person
|
||||
|
|
@ -75,16 +76,7 @@ protected
|
|||
Retraction.for(self).notify_people
|
||||
end
|
||||
|
||||
def send_to_view
|
||||
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
|
||||
SocketsController.new.outgoing(Retraction.for(self))
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
%span.from
|
||||
= link_to post.person.real_name, post.person
|
||||
= auto_link post.message
|
||||
= auto_link sanitize post.message
|
||||
|
||||
%div.time
|
||||
= link_to(how_long_ago(post), object_path(post))
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
require 'em-websocket'
|
||||
require 'eventmachine'
|
||||
|
||||
module WebSocket
|
||||
require "lib/diaspora/websocket"
|
||||
EM.next_tick {
|
||||
initialize_channels
|
||||
Diaspora::WebSocket.initialize_channels
|
||||
|
||||
EventMachine::WebSocket.start(
|
||||
:host => "0.0.0.0",
|
||||
|
|
@ -11,43 +10,12 @@ module WebSocket
|
|||
:debug =>APP_CONFIG[:debug]) do |ws|
|
||||
ws.onopen {
|
||||
|
||||
sid = self.subscribe(ws.request['Path'].gsub('/',''), ws)
|
||||
sid = Diaspora::WebSocket.subscribe(ws.request['Path'].gsub('/',''), ws)
|
||||
|
||||
ws.onmessage { |msg| SocketsController.new.incoming(msg) }#@channel.push msg; puts msg}
|
||||
|
||||
ws.onclose { unsubscribe(ws.request['Path'].gsub('/',''), sid) }
|
||||
ws.onclose { Diaspora::WebSocket.unsubscribe(ws.request['Path'].gsub('/',''), sid) }
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
def self.initialize_channels
|
||||
@channels = {}
|
||||
end
|
||||
|
||||
def self.push_to_user(uid, data)
|
||||
Rails.logger.debug "Websocketing to #{uid}"
|
||||
@channels[uid.to_s][0].push(data) if @channels[uid.to_s]
|
||||
end
|
||||
|
||||
def self.subscribe(uid, ws)
|
||||
Rails.logger.debug "Subscribing socket to #{User.first(:id => uid).email}"
|
||||
self.ensure_channel(uid)
|
||||
@channels[uid][0].subscribe{ |msg| ws.send msg }
|
||||
@channels[uid][1] += 1
|
||||
end
|
||||
|
||||
def self.ensure_channel(uid)
|
||||
@channels[uid] ||= [EM::Channel.new, 0 ]
|
||||
end
|
||||
|
||||
def self.unsubscribe(uid,sid)
|
||||
Rails.logger.debug "Unsubscribing socket #{sid} from #{User.first(:id => uid).email}"
|
||||
@channels[uid][0].unsubscribe(sid) if @channels[uid]
|
||||
@channels[uid][1] -= 1
|
||||
if @channels[uid][1] <= 0
|
||||
@channels.delete(uid)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
|||
56
lib/diaspora/websocket.rb
Normal file
56
lib/diaspora/websocket.rb
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
module Diaspora
|
||||
module WebSocket
|
||||
def self.initialize_channels
|
||||
@channels = {}
|
||||
end
|
||||
|
||||
def self.push_to_user(uid, data)
|
||||
Rails.logger.debug "Websocketing to #{uid}"
|
||||
@channels[uid.to_s][0].push(data) if @channels[uid.to_s]
|
||||
end
|
||||
|
||||
def self.subscribe(uid, ws)
|
||||
Rails.logger.debug "Subscribing socket to #{User.first(:id => uid).email}"
|
||||
self.ensure_channel(uid)
|
||||
@channels[uid][0].subscribe{ |msg| ws.send msg }
|
||||
@channels[uid][1] += 1
|
||||
end
|
||||
|
||||
def self.ensure_channel(uid)
|
||||
@channels[uid] ||= [EM::Channel.new, 0 ]
|
||||
end
|
||||
|
||||
def self.unsubscribe(uid,sid)
|
||||
Rails.logger.debug "Unsubscribing socket #{sid} from #{User.first(:id => uid).email}"
|
||||
@channels[uid][0].unsubscribe(sid) if @channels[uid]
|
||||
@channels[uid][1] -= 1
|
||||
if @channels[uid][1] <= 0
|
||||
@channels.delete(uid)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Socketable
|
||||
def socket_to_uid id
|
||||
SocketsController.new.outgoing(id, self)
|
||||
end
|
||||
|
||||
def unsocket_from_uid id
|
||||
SocketsController.new.outgoing(id, Retraction.for(self))
|
||||
end
|
||||
|
||||
def send_to_view
|
||||
people_with_permissions.each{|f|
|
||||
socket_to_uid f.owner_id if f.owner_id
|
||||
}
|
||||
socket_to_uid person.owner_id if person.owner_id
|
||||
end
|
||||
|
||||
def remove_from_view
|
||||
people_with_permissions.each{|f|
|
||||
unsocket_from_uid f.owner_id if f.owner_id
|
||||
}
|
||||
unsocket_from_uid person.owner_id if person.owner_id
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue