Class: Artoo::Api::DeviceEventClient

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger, Celluloid::Notifications
Defined in:
lib/artoo/api/device_event_client.rb

Overview

The Artoo::Api::DeviceEventClient class is how a websocket client can subscribe to event notifications for a specific device. Example: ardrone nav data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, topic) ⇒ DeviceEventClient

Create new event client

Parameters:

  • websocket (Socket)
  • topic (String)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/artoo/api/device_event_client.rb', line 19

def initialize(connection, topic)
  @io = Reel::Response::Writer.new(connection.socket)

  connection.detach

  connection.respond(:ok, {
    'Content-Type' => 'text/event-stream',
    'Connection' => 'keep-alive',
    'Transfer-Encoding' => 'chunked',
    'Cache-Control' => 'no-cache'
  })

  subscribe(topic, :notify_event)
end

Instance Attribute Details

#topicObject (readonly)

Returns the value of attribute topic.



14
15
16
# File 'lib/artoo/api/device_event_client.rb', line 14

def topic
  @topic
end

Instance Method Details

#notify_event(topic, *data) ⇒ Object

Event notification

Parameters:

  • topic (String)
  • data (Object)


37
38
39
# File 'lib/artoo/api/device_event_client.rb', line 37

def notify_event(topic, *data)
  @io.write "data: #{JSON.dump(data[0])}\n\n"
end