Module: Engine2::ActionWebSocketSupport

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

Instance Method Summary collapse

Instance Method Details

#invoke!(handler) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/engine2/action.rb', line 203

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



198
199
200
201
# File 'lib/engine2/action.rb', line 198

def post_run
    super
    @invokable = true
end

#pre_runObject



184
185
186
187
188
# File 'lib/engine2/action.rb', line 184

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

#ws_execute(execute) ⇒ Object



194
195
196
# File 'lib/engine2/action.rb', line 194

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

#ws_options(opts) ⇒ Object



190
191
192
# File 'lib/engine2/action.rb', line 190

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