lib/plezi/handlers/placebo.rb in plezi-0.10.12 vs lib/plezi/handlers/placebo.rb in plezi-0.10.13
- old
+ new
@@ -27,10 +27,12 @@
#the methods here will be injected to the Placebo controller.
module Core
def self.included base
base.send :include, InstanceMethods
base.extend ClassMethods
+ base.superclass.instance_eval {extend SuperClassMethods}
+ base.send :include, Plezi::Base::WSObject
end
#the methods here will be injected to the Placebo controller as Instance methods.
module InstanceMethods
public
@@ -43,58 +45,19 @@
# notice of disconnect
def on_close
return super() if defined? super
GR.warn "Placebo #{self.class.superclass.name} disconnected. Ignore if this message appears during shutdown."
end
-
- # handles broadcasts / unicasts
- def on_broadcast ws
- data = ws.data
- unless (data[:type] || data[:target]) && data[:method] && data[:data]
- GReactor.warn "Broadcast message unknown... falling back on base broadcasting"
- return super(data) if defined? super
- return false
- end
- # return false if data[:type] && !self.is_a?(data[:type])
- return false if data[:target] && data[:target] != ws.uuid
- return false unless self.class.has_super_method?(data[:method])
- self.method(data[:method]).call *data[:data]
+ def placebo?
+ true
end
- # Returns the websocket connection's UUID, used for unicasting.
- def uuid
- io[:uuid] ||= SecureRandom.uuid
- end
-
- # Performs a websocket unicast to the specified target.
- def unicast target_uuid, method_name, *args
- GRHttp::Base::WSHandler.unicast target_uuid, data: args, target: target_uuid, method: method_name
- __send_to_redis data: args, target: target_uuid, method: method_name
- end
- # broadcast to a specific controller
- def broadcast controller_class, method_name, *args
- GRHttp::Base::WSHandler.broadcast({data: args, type: controller_class, method: method_name}, self)
- __send_to_redis data: args, type: controller_class, method: method_name
- end
- # multicast to all handlers.
- def multicast method_name, *args
- GRHttp::Base::WSHandler.broadcast({method: method_name, data: args, type: :all}, self)
- __send_to_redis method: method_name, data: args, type: :all
- end
- protected
- def __send_to_redis data
- raise "Wrong method name for websocket broadcasting - expecting type Symbol" unless data[:method].is_a?(Symbol) || data[:method].is_a?(Symbol)
- conn = Plezi.redis_connection
- data[:server] = Plezi::Settings.uuid
- return conn.publish( Plezi::Settings.redis_channel_name, data.to_yaml ) if conn
- false
- end
end
#the methods here will be injected to the Placebo controller as class methods.
module ClassMethods
- public
- def has_super_method? method_name
- @super_methods_list ||= self.superclass.instance_methods.to_set
- @super_methods_list.include? method_name
+ end
+ module SuperClassMethods
+ def placebo?
+ true
end
end
end
class PlaceboIO < GReactor::BasicIO
def clear?