Class: WebsocketRails::BaseController
- Inherits:
-
Object
- Object
- WebsocketRails::BaseController
- Includes:
- AbstractController::Callbacks, Metal
- Defined in:
- lib/websocket_rails/base_controller.rb
Overview
Provides controller helper methods for developing a WebsocketRails controller. Action methods defined on a WebsocketRails controller can be mapped to events using the EventMap class.
Example WebsocketRails controller
class ChatController < WebsocketRails::BaseController
# Can be mapped to the :client_connected event in the events.rb file.
def new_user
:new_message, {:message => 'Welcome to the Chat Room!'}
end
end
It is best to use the provided DataStore to temporarily persist data for each client between events. Read more about it in the DataStore documentation.
Direct Known Subclasses
Defined Under Namespace
Modules: Metal
Class Method Summary collapse
- .controller_name ⇒ Object
-
.filter_for_channels(*channels) ⇒ Object
Tell the dispatcher to use channel filtering on specific channels.
-
.inherited(controller) ⇒ Object
Tell Rails that BaseController and children can be reloaded when in the Development environment.
Instance Method Summary collapse
- #accept_channel(data = nil) ⇒ Object
- #action_name ⇒ Object
-
#broadcast_message(event_name, message, options = {}) ⇒ Object
Broadcasts a message to all connected clients.
-
#client_id ⇒ Object
The numerical ID for the client connection that initiated the event.
-
#connection ⇒ Object
Provides direct access to the connection object for the client that initiated the event that is currently being executed.
- #connection_store ⇒ Object
- #controller_name ⇒ Object
-
#controller_store ⇒ Object
Provides access to the DataStore for the current controller.
- #deny_channel(data = nil) ⇒ Object
-
#event ⇒ Object
The Event object that triggered this action.
-
#message ⇒ Object
(also: #data)
The current message that was passed from the client when the event was initiated.
- #request ⇒ Object
-
#send_message(event_name, message, options = {}) ⇒ Object
Sends a message to the client that initiated the current event being executed.
- #stop_event_propagation! ⇒ Object
-
#trigger_failure(data = nil) ⇒ Object
Trigger the failure callback function attached to the client event that triggered this action.
-
#trigger_success(data = nil) ⇒ Object
Trigger the success callback function attached to the client event that triggered this action.
Methods included from Metal
#process_action, #response_body
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
189 190 191 192 193 194 195 |
# File 'lib/websocket_rails/base_controller.rb', line 189 def method_missing(method,*args,&block) if delegate.respond_to? method delegate.send method, *args, &block else super end end |
Class Method Details
.controller_name ⇒ Object
175 176 177 |
# File 'lib/websocket_rails/base_controller.rb', line 175 def self.controller_name self.name.underscore.gsub(/_controller$/,'') end |
.filter_for_channels(*channels) ⇒ Object
Tell the dispatcher to use channel filtering on specific channels. If supplied, the :catch_all => :method will be processed for every event that comes into the channel(s).
Example: To process events based upon the event_name inside :channel_one
filter_for_channels :channel_one
To process events based upon the event_name and a catch all
filter_for_channels :channel_one, :catch_all => :logger_method
63 64 65 66 67 68 |
# File 'lib/websocket_rails/base_controller.rb', line 63 def self.filter_for_channels(*channels) = channels.last.is_a?(Hash) ? channels.pop : {} channels.each do |channel| WebsocketRails.filtered_channels[channel] = [:catch_all].nil? ? self : [self, [:catch_all]] end end |
.inherited(controller) ⇒ Object
Tell Rails that BaseController and children can be reloaded when in the Development environment.
43 44 45 46 47 |
# File 'lib/websocket_rails/base_controller.rb', line 43 def self.inherited(controller) unless controller.name == "WebsocketRails::InternalController" || Rails.version =~/^4/ unloadable controller end end |
Instance Method Details
#accept_channel(data = nil) ⇒ Object
116 117 118 119 120 |
# File 'lib/websocket_rails/base_controller.rb', line 116 def accept_channel(data=nil) channel_name = event.data[:channel] WebsocketRails[channel_name].subscribe connection trigger_success data end |
#action_name ⇒ Object
160 161 162 |
# File 'lib/websocket_rails/base_controller.rb', line 160 def action_name @_action_name end |
#broadcast_message(event_name, message, options = {}) ⇒ Object
Broadcasts a message to all connected clients. See #send_message for message passing details.
150 151 152 153 154 |
# File 'lib/websocket_rails/base_controller.rb', line 150 def (event_name, , ={}) .merge! :connection => connection, :data => event = Event.new( event_name, ) @_dispatcher. event if @_dispatcher.respond_to?(:broadcast_message) end |
#client_id ⇒ Object
The numerical ID for the client connection that initiated the event. The ID is unique for each currently active connection but can not be used to associate a client between multiple connection attempts.
79 80 81 |
# File 'lib/websocket_rails/base_controller.rb', line 79 def client_id connection.id end |
#connection ⇒ Object
Provides direct access to the connection object for the client that initiated the event that is currently being executed.
72 73 74 |
# File 'lib/websocket_rails/base_controller.rb', line 72 def connection @_event.connection end |
#connection_store ⇒ Object
171 172 173 |
# File 'lib/websocket_rails/base_controller.rb', line 171 def connection_store connection.data_store end |
#controller_name ⇒ Object
179 180 181 |
# File 'lib/websocket_rails/base_controller.rb', line 179 def controller_name self.class.controller_name end |
#controller_store ⇒ Object
167 168 169 |
# File 'lib/websocket_rails/base_controller.rb', line 167 def controller_store @_controller_store end |
#deny_channel(data = nil) ⇒ Object
122 123 124 |
# File 'lib/websocket_rails/base_controller.rb', line 122 def deny_channel(data=nil) trigger_failure data end |
#event ⇒ Object
The Event object that triggered this action. Find the current event name with event.name Access the data sent with the event with event.data Find the event’s namespace with event.namespace
87 88 89 |
# File 'lib/websocket_rails/base_controller.rb', line 87 def event @_event end |
#message ⇒ Object Also known as: data
The current message that was passed from the client when the event was initiated. The message is typically a standard ruby Hash object. See the README for more information.
93 94 95 |
# File 'lib/websocket_rails/base_controller.rb', line 93 def @_event.data end |
#request ⇒ Object
156 157 158 |
# File 'lib/websocket_rails/base_controller.rb', line 156 def request connection.request end |
#send_message(event_name, message, options = {}) ⇒ Object
Sends a message to the client that initiated the current event being executed. Messages are serialized as JSON into a two element Array where the first element is the event and the second element is the message that was passed, typically a Hash.
To send an event under a namespace, add the ‘:namespace => :target_namespace` option.
:new_message, , :namespace => :product
Nested namespaces can be passed as an array like the following:
:new, , :namespace => [:products,:glasses]
See the EventMap documentation for more on mapping namespaced actions.
143 144 145 146 147 |
# File 'lib/websocket_rails/base_controller.rb', line 143 def (event_name, , ={}) .merge! :connection => connection, :data => event = Event.new( event_name, ) @_dispatcher. event if @_dispatcher.respond_to?(:send_message) end |
#stop_event_propagation! ⇒ Object
126 127 128 |
# File 'lib/websocket_rails/base_controller.rb', line 126 def stop_event_propagation! event.propagate = false end |
#trigger_failure(data = nil) ⇒ Object
Trigger the failure callback function attached to the client event that triggered this action. The object passed to this method will be passed as an argument to the callback function on the client.
110 111 112 113 114 |
# File 'lib/websocket_rails/base_controller.rb', line 110 def trigger_failure(data=nil) event.success = Event::FAILED event.data = data event.trigger end |
#trigger_success(data = nil) ⇒ Object
Trigger the success callback function attached to the client event that triggered this action. The object passed to this method will be passed as an argument to the callback function on the client.
101 102 103 104 105 |
# File 'lib/websocket_rails/base_controller.rb', line 101 def trigger_success(data=nil) event.success = Event::SUCCEEDED event.data = data event.trigger end |