lib/plezi/handlers/ws_object.rb in plezi-0.12.20 vs lib/plezi/handlers/ws_object.rb in plezi-0.12.21

- old
+ new

@@ -75,11 +75,11 @@ 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 + # return (data[:data].each {|e| emit(e)}) if data[:method] == :emit return ((data[:type] == :all) ? false : (raise "Broadcasting recieved but no method can handle it - dump:\r\n #{data.to_s}") ) unless self.class.has_super_method?(data[:method]) self.__send__(data[:method], *data[:data]) end # Get's the websocket's unique identifier for unicast transmissions. @@ -101,10 +101,18 @@ # allows writing of data to the websocket (if opened). Otherwise appends the message to the Http response. def write data (@ws_io || @response) << data end + # # @!visibility public + # # A helper method for easily sending JSON data. Accepts a Hash that will be translated to JSON and sent to the client as a JSON string. + # # + # # This method is available as a broadcast event. + # def emit event + # write event.to_json + # end + # @!visibility public # Closes the connection def close # @request[:io] contains the Websockets Protocol instance (@ws_io || @request[:io]).go_away @@ -151,13 +159,15 @@ def reset_routing_cache @methods_list = nil @exposed_methods_list = nil @super_methods_list = nil + @auto_dispatch_list = nil has_method? nil has_exposed_method? nil has_super_method? nil + has_auto_dispatch_method? nil end def has_method? method_name @methods_list ||= self.instance_methods.to_set @methods_list.include? method_name end @@ -173,10 +183,19 @@ Plezi::Base::ControllerCore::InstanceMethods.public_instance_methods + [:before, :after, :save, :show, :update, :delete, :initialize] @exposed_methods_list ||= ( (self.public_instance_methods - @reserved_methods_list ).delete_if {|m| m.to_s[0] == '_'} ).to_set @exposed_methods_list.include? method_name end + def has_auto_dispatch_method? method_name + @auto_dispatch_list ||= (( self.instance_methods - (Class.new.instance_methods + + Plezi::Base::WSObject::InstanceMethods.instance_methods + + Plezi::Base::WSObject::SuperInstanceMethods.instance_methods + + Plezi::ControllerMagic::InstanceMethods.instance_methods + + Plezi::Base::ControllerCore::InstanceMethods.instance_methods + + [:before, :after, :initialize, :unknown , :unknown_event]) ).delete_if {|m| m.to_s[0] == '_' || instance_method(m).arity == 0 }).to_set + @auto_dispatch_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) @@ -227,11 +246,11 @@ # target_uuid:: the target's unique UUID. # 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[@@uuid_cutoff..-1], to_server: target_uuid[0...@@uuid_cutoff], type: :all + @uuid_cutoff ||= Plezi::Settings.uuid.length + _inner_broadcast method: method_name, data: args, target: target_uuid[@uuid_cutoff..-1], to_server: target_uuid[0...@uuid_cutoff], type: :all end # Use this to multicast an event to ALL websocket connections on EVERY controller, including Placebo controllers. # # Accepts: