lib/plezi/handlers/ws_object.rb in plezi-0.11.2 vs lib/plezi/handlers/ws_object.rb in plezi-0.12.0
- old
+ new
@@ -25,14 +25,13 @@
end
module InstanceMethods
public
# 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"
+ def on_broadcast data
+ unless data.is_a?(Hash) && (data[:type] || data[:target]) && data[:method] && data[:data]
+ Iodine.warn "Broadcast message unknown... falling back on base broadcasting"
return super(data) if defined? super
return false
end
return false if data[:type] && data[:type] != :all && !self.is_a?(data[:type])
# return ( self.class.placebo? ? true : we.write(ws.data)) if :method == :to_client
@@ -71,24 +70,22 @@
# Get's the websocket's unique identifier for unicast transmissions.
#
# This UUID is also used to make sure Radis broadcasts don't triger the
# boadcasting object's event.
- def uuid
+ def uuid
return @uuid if @uuid
- if @response && @response.is_a?(GRHttp::WSEvent)
- return (@uuid ||= @response.uuid + Plezi::Settings.uuid)
- elsif @io
- return (@uuid ||= (@io[:uuid] ||= SecureRandom.uuid) + Plezi::Settings.uuid)
+ if __get_io
+ return (@uuid ||= Plezi::Settings.uuid + @io.id)
end
nil
end
alias :unicast_id :uuid
protected
def __get_io
- @io ||= (@request ? @request.io : nil)
+ @io ||= (@request ? @request[:io] : nil)
end
end
module ClassMethods
def reset_routing_cache
@@ -109,10 +106,11 @@
end
def has_exposed_method? method_name
@exposed_methods_list ||= ( (self.public_instance_methods - Class.new.instance_methods - Plezi::ControllerMagic::InstanceMethods.instance_methods - [:before, :after, :save, :show, :update, :delete, :initialize, :on_message, :on_broadcast, :pre_connect, :on_open, :on_close]).delete_if {|m| m.to_s[0] == '_'} ).to_set
@exposed_methods_list.include? method_name
end
+
protected
# a callback that resets the class router whenever a method (a potential route) is added
def method_added(id)
reset_routing_cache
@@ -161,11 +159,11 @@
# method_name:: a Symbol with the method's name that should respond to the broadcast.
# *args:: any arguments that should be passed to the method (IF REDIS IS USED, LIMITATIONS APPLY).
def unicast target_uuid, method_name, *args
raise 'No target specified for unicasting!' unless target_uuid
@@uuid_cutoff ||= Plezi::Settings.uuid.length
- _inner_broadcast method: method_name, data: args, target: target_uuid[0...@@uuid_cutoff], to_server: target_uuid[@@uuid_cutoff..-1]
+ _inner_broadcast method: method_name, data: args, target: target_uuid[@@uuid_cutoff..-1], to_server: target_uuid[0...@@uuid_cutoff], type: self
end
# Use this to multicast an event to ALL websocket connections on EVERY controller, including Placebo controllers.
#
# Accepts:
@@ -181,29 +179,41 @@
# WebSockets
# sends the broadcast
def _inner_broadcast data, ignore_io = nil
if data[:target]
- return ( (data[:to_server].nil? || data[:to_server] == Plezi::Settings.uuid) ? GRHttp::Base::WSHandler.unicast(data[:target], data) : false ) || __inner_redis_broadcast(data)
+ if data[:target] && (data[:to_server] == Plezi::Settings.uuid )
+ return ( ::Iodine::Http::Websockets.unicast( data[:target], data ) || ___faild_unicast( data ) )
+ end
+ return ( data[:to_server].nil? && ::Iodine::Http::Websockets.unicast(data[:target], data) ) || ( Plezi::Base::AutoRedis.away?(data[:to_server]) && ___faild_unicast( data ) ) || __inner_redis_broadcast(data)
else
- GRHttp::Base::WSHandler.broadcast data, ignore_io
+ ::Iodine::Http::Websockets.broadcast data, ignore_io
__inner_redis_broadcast data
end
true
end
def __inner_redis_broadcast data
return unless conn = Plezi.redis
data = data.dup
- data[:type] = data[:type].name if data[:type]
+ data[:type] = data[:type].name if data[:type].is_a?(Class)
data[:server] = Plezi::Settings.uuid
- return conn.publish( ( data[:to_server] ? data[:to_server] : Plezi::Settings.redis_channel_name ), data.to_yaml ) if conn
+ return conn.publish( ( data[:to_server] || Plezi::Settings.redis_channel_name ), data.to_yaml ) if conn
false
end
+ def ___faild_unicast data
+ has_class_method?(:failed_unicast) && failed_unicast( data[:to_server].to_s + data[:target], data[:method], data[:data] )
+ true
+ end
+
def has_method? method_name
@methods_list ||= self.instance_methods.to_set
@methods_list.include? method_name
+ end
+ def has_class_method? method_name
+ @class_methods_list ||= self.methods.to_set - Object.methods
+ @class_methods_list.include? method_name
end
end
end
end
end