lib/plezi/controller/controller.rb in plezi-0.14.9 vs lib/plezi/controller/controller.rb in plezi-0.15.0
- old
+ new
@@ -1,9 +1,9 @@
require 'plezi/render/render'
+require 'plezi/controller/identification'
require 'plezi/controller/cookies'
require 'plezi/controller/controller_class'
-require 'plezi/websockets/message_dispatch'
module Plezi
# This module contains the functionality provided to any Controller class.
#
# This module will be included within every Class that is asigned to a route, providing the functionality without forcing an inheritance model.
@@ -117,17 +117,17 @@
# Returns a relative URL for the controller, placing the requested parameters in the URL (inline, where possible and as query data when not possible).
def url_for(func, params = {})
::Plezi::Base::Router.url_for self.class, func, params
end
- # A connection's Plezi ID uniquely identifies the connection across application instances, allowing it to receive and send messages using {#unicast}.
+ # A connection's Plezi ID uniquely identifies the connection across application instances.
def id
- @_pl_id ||= (conn_id && "#{::Plezi::Base::MessageDispatch.pid}-#{conn_id.to_s(16)}")
+ @_pl_id ||= (conn_id && "#{::Plezi::Base::Identification.pid}-#{conn_id.to_s(16)}")
end
# @private
- # This is the process specific Websocket's UUID. This function is here to protect you from yourself. Don't call it.
+ # This is the process specific Websocket's ID. This function is here to protect you from yourself. Don't call it.
def conn_id
defined?(super) && super
end
# Override this method to read / write cookies, perform authentication or perform validation before establishing a Websocket connecion.
@@ -141,75 +141,23 @@
#
# This function can only be called **after** a websocket connection was established (i.e., within the `on_open` callback).
#
# This allows a module "library" to be used similar to the way "rooms" are used in node.js, so that a number of different Controllers can listen to shared events.
#
- # By dynamically extending a Controller instance using a module, Websocket broadcasts will be allowed to invoke the module's functions.
+ # By dynamically extending a Controller instance using a module, Auto Dispatch events can be routed to the newly available methods.
#
# Notice: It is impossible to `unextend` an extended module at this time.
def extend(mod)
raise TypeError, '`mod` should be a module' unless mod.class == Module
- raise "#{self} already extended by #{mod.name}" if is_a?(mod)
- mod.extend ::Plezi::Controller::ClassMethods
- super(mod)
+ unless is_a?(mod)
+ mod.extend ::Plezi::Controller::ClassMethods
+ super(mod)
+ end
_pl_ws_map.update mod._pl_ws_map
_pl_ad_map.update mod._pl_ad_map
end
- # Invokes a method on the `target` websocket connection. When using Iodine, the method is invoked asynchronously.
- #
- # def perform_poke(target)
- # unicast target, :poke, self.id
- # end
- # def poke(from)
- # unicast from, :poke_back, self.id
- # end
- # def poke_back(from)
- # puts "#{from} is available"
- # end
- #
- # Methods invoked using {unicast}, {broadcast} or {multicast} will quietly fail if the connection was lost, the requested method is undefined or the 'target' was invalid.
- def unicast(target, event_method, *args)
- ::Plezi::Base::MessageDispatch.unicast(id ? self : self.class, target, event_method, args)
- end
-
- # Invokes a method on every websocket connection (except `self`) that belongs to this Controller / Type. When using Iodine, the method is invoked asynchronously.
- #
- # self.broadcast :my_method, "argument 1", "argument 2", 3
- #
- # Methods invoked using {unicast}, {broadcast} or {multicast} will quietly fail if the connection was lost, the requested method is undefined or the 'target' was invalid.
- def broadcast(event_method, *args)
- ::Plezi::Base::MessageDispatch.broadcast(id ? self : self.class, event_method, args)
- end
-
- # Invokes a method on every websocket connection in the application (except `self`).
- #
- # self.multicast :my_method, "argument 1", "argument 2", 3
- #
- # Methods invoked using {unicast}, {broadcast} or {multicast} will quietly fail if the connection was lost, the requested method is undefined or the 'target' was invalid.
- def multicast(event_method, *args)
- ::Plezi::Base::MessageDispatch.multicast(id ? self : self.class, event_method, args)
- end
-
- # Writes a message to every client websocket connection, for all controllers(!), EXCEPT self. Accepts an optional filter method using a location reference for a *static* (Class/Module/global) method. The filter method will be passerd the websocket object and it should return `true` / `false`.
- #
- # self.write2everyone {event: "global", message: "This will be sent to everyone"}.to_json
- # # or, we can define a filter method somewhere in our code
- # module Filter
- # def self.should_send? ws
- # true
- # end
- # end
- # # and we can use this filter method.
- # data = {event: "global", message: "This will be sent to everyone"}.to_json
- # self.write2everyone data, ::Filter, :should_send?
- #
- # It's important that the filter method is defined statically in our code and isn't dynamically allocated. Otherwise, scaling the application would be impossible.
- def write2everyone(data, filter_owner = nil, filter_name = nil)
- ::Plezi::Base::MessageDispatch.write2everyone(id ? self : self.class, data, filter_owner, filter_name)
- end
-
# @private
# This function is used internally by Plezi, do not call.
def _pl_ws_map
@_pl_ws_map ||= self.class._pl_ws_map.dup
end
@@ -224,9 +172,10 @@
# This function is used internally by Plezi, for Auto-Dispatch support do not call.
def on_message(data)
json = nil
begin
json = JSON.parse(data, symbolize_names: true)
+ # json.default_proc = Plezi.hash_proc_4symstr
rescue
puts 'AutoDispatch Warnnig: Received non-JSON message. Closing Connection.'
close
return
end