Skip to content
Extraits de code Groupes Projets
Valider 599d1da6 rédigé par Raphael's avatar Raphael
Parcourir les fichiers

Moved socket code out to lib, unsocketing from view should now work again

parent 144e9ed4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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))
......
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
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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter