Class: SocketCake::BaseSocket
- Inherits:
-
Object
- Object
- SocketCake::BaseSocket
- Defined in:
- lib/socketcake/base_socket.rb
Instance Attribute Summary collapse
-
#completed_tasks ⇒ Object
Returns the value of attribute completed_tasks.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#current_task ⇒ Object
Returns the value of attribute current_task.
-
#local_storage ⇒ Object
Returns the value of attribute local_storage.
-
#open_message ⇒ Object
Returns the value of attribute open_message.
-
#task_list ⇒ Object
Returns the value of attribute task_list.
-
#url ⇒ Object
Returns the value of attribute url.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #handle(msg) ⇒ Object
-
#initialize(task_list, url, open_message, cookie, verbose = false) ⇒ BaseSocket
constructor
A new instance of BaseSocket.
- #start(arg = nil) ⇒ Object
Constructor Details
#initialize(task_list, url, open_message, cookie, verbose = false) ⇒ BaseSocket
Returns a new instance of BaseSocket.
9 10 11 12 13 14 15 16 17 |
# File 'lib/socketcake/base_socket.rb', line 9 def initialize task_list, url, , , verbose = false self.url = url self. = self.task_list = task_list self. = self.completed_tasks = Array.new self.local_storage = Hash.new self.verbose = verbose end |
Instance Attribute Details
#completed_tasks ⇒ Object
Returns the value of attribute completed_tasks.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def completed_tasks @completed_tasks end |
#cookie ⇒ Object
Returns the value of attribute cookie.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def end |
#current_task ⇒ Object
Returns the value of attribute current_task.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def current_task @current_task end |
#local_storage ⇒ Object
Returns the value of attribute local_storage.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def local_storage @local_storage end |
#open_message ⇒ Object
Returns the value of attribute open_message.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def end |
#task_list ⇒ Object
Returns the value of attribute task_list.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def task_list @task_list end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def url @url end |
#verbose ⇒ Object
Returns the value of attribute verbose.
3 4 5 |
# File 'lib/socketcake/base_socket.rb', line 3 def verbose @verbose end |
Class Method Details
.get_sequence ⇒ Object
5 6 7 |
# File 'lib/socketcake/base_socket.rb', line 5 def self.get_sequence ObjectSpace.each_object(self) end |
Instance Method Details
#handle(msg) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/socketcake/base_socket.rb', line 49 def handle msg = JSON.parse(msg) unless self.current_task[:completed] self.send(self.current_task[:name], self.current_task[:args]) end end |
#start(arg = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/socketcake/base_socket.rb', line 19 def start(arg = nil) @socket = WebSocket::EventMachine::Client.connect(:uri => self.url, headers: { 'Cookie': self.}) @socket.onopen do puts "#{Time.now} Connected" get_task @socket.send self. end @socket. do |msg| puts msg if @verbose handle(msg) end @socket.onerror do |error| puts error end @socket.onclose do |code, reason| puts "#{Time.now} Disconnected with status code: #{code} and reason: #{reason}" yield(local_storage) end def handle msg = JSON.parse(msg) unless self.current_task[:completed] self.send(self.current_task[:name], self.current_task[:args]) end end end |