Class: Patch::IO::Websocket::Node
- Inherits:
-
Object
- Object
- Patch::IO::Websocket::Node
- Defined in:
- lib/patch/io/websocket/node.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#active? ⇒ Boolean
(also: #running?)
Is the server active?.
-
#disable(patch) ⇒ Boolean
Disable the message listener.
-
#initialize(id, host, port, options = {}) ⇒ Node
constructor
A new instance of Node.
-
#listen(patch, &callback) ⇒ Boolean
Listen for messages with the given patch context.
-
#puts(patch, messages) ⇒ String?
Send a message over the socket.
-
#socket ⇒ Boolean
(also: #start)
Start the websocket.
Constructor Details
#initialize(id, host, port, options = {}) ⇒ Node
Returns a new instance of Node.
16 17 18 19 20 21 22 23 |
# File 'lib/patch/io/websocket/node.rb', line 16 def initialize(id, host, port, = {}) @config = { :host => host, :port => port } @id = id @log = [:log] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/patch/io/websocket/node.rb', line 9 def id @id end |
Instance Method Details
#active? ⇒ Boolean Also known as: running?
Is the server active?
74 75 76 |
# File 'lib/patch/io/websocket/node.rb', line 74 def active? !@socket.nil? && @socket.active? end |
#disable(patch) ⇒ Boolean
Disable the message listener
50 51 52 |
# File 'lib/patch/io/websocket/node.rb', line 50 def disable(patch) @socket.disable end |
#listen(patch, &callback) ⇒ Boolean
Listen for messages with the given patch context
58 59 60 61 62 63 |
# File 'lib/patch/io/websocket/node.rb', line 58 def listen(patch, &callback) ensure_socket. do |data| handle_input(patch, data, &callback) end true end |
#puts(patch, messages) ⇒ String?
Send a message over the socket
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/patch/io/websocket/node.rb', line 29 def puts(patch, ) if running? unless ( = [].flatten.compact).empty? json = .to_json @log.puts("Sending messages: #{json}") if @log begin @socket.puts(json) rescue Exception => exception # failsafe @log.exception(exception) if @log ::Thread.main.raise(exception) end json end else @log.puts("Warning: No connection") if @log nil end end |
#socket ⇒ Boolean Also known as: start
Start the websocket
67 68 69 |
# File 'lib/patch/io/websocket/node.rb', line 67 def socket ensure_socket end |