Module: Engine2::ActionWebSocketSupport

Included in:
WebSocketAction
Defined in:
lib/engine2/action.rb

Instance Method Summary collapse

Instance Method Details

#invoke!(handler) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/engine2/action.rb', line 186

def invoke! handler
    if Faye::WebSocket.websocket?(handler.env)
        ws = Faye::WebSocket.new(handler.env)
        @ws_methods.each do |method, blk|
            ws.on(method) do |evt|
                begin
                    data = method == :message ? JSON.parse(evt.data, symbolize_names: true) : evt
                    action = self.class.new(node, assets, self)
                    result = action.instance_exec(data, ws, evt, &blk)
                    result = {} unless result.is_a?(Hash)
                    result[:meta] = action.meta
                    ws.send! result unless action.meta.empty?
                rescue Exception => e
                    ws.send! error: {exception: e, method: method}
                end
            end
        end
        ws.rack_response
    else
        super
    end
end

#post_runObject



181
182
183
184
# File 'lib/engine2/action.rb', line 181

def post_run
    super
    @invokable = true
end

#pre_runObject



167
168
169
170
171
# File 'lib/engine2/action.rb', line 167

def pre_run
    super
    @ws_methods = {}
    @meta[:websocket] = {options: {}}
end

#ws_execute(execute) ⇒ Object



177
178
179
# File 'lib/engine2/action.rb', line 177

def ws_execute execute
    (@meta[:websocket][:execute] ||= {}).merge! execute
end

#ws_options(opts) ⇒ Object



173
174
175
# File 'lib/engine2/action.rb', line 173

def ws_options opts
   @meta[:websocket][:options].merge! opts
end