Class: CapistranoSentinel::WebsocketClient
- Includes:
- ApplicationHelper
- Defined in:
- lib/capistrano_sentinel/classes/websocket_client.rb
Instance Attribute Summary collapse
-
#actor ⇒ Object
Returns the value of attribute actor.
-
#auto_pong ⇒ Object
Returns the value of attribute auto_pong.
-
#on_error ⇒ Object
Returns the value of attribute on_error.
-
#on_message ⇒ Object
Returns the value of attribute on_message.
-
#on_ping ⇒ Object
Returns the value of attribute on_ping.
-
#protocol_version ⇒ Object
readonly
Returns the value of attribute protocol_version.
-
#read_buffer_size ⇒ Object
Returns the value of attribute read_buffer_size.
-
#read_thread ⇒ Object
readonly
Returns the value of attribute read_thread.
-
#reconnect ⇒ Object
writeonly
Sets the attribute reconnect.
-
#retry_time ⇒ Object
Returns the value of attribute retry_time.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#initialize(opts) ⇒ WebsocketClient
constructor
host
-
Host of request.
- #is_windows? ⇒ Boolean
-
#publish(channel, data) ⇒ void
publishes to a channel some data (can be anything).
-
#subscribe(channel, data = {}) ⇒ void
subscribes to a channel .
-
#unsubscribe(channel) ⇒ void
unsubscribes current client from a channel.
-
#unsubscribe_all ⇒ void
unsubscribes all clients from all channels.
-
#unsubscribe_clients(channel) ⇒ void
unsubscribes all clients subscribed to a channel.
Methods included from ApplicationHelper
get_question_details, message_from_bundler?, message_is_about_a_task?, message_is_for_stdout?, msg_for_stdin?, parse_json
Methods included from Logging
error_filtered?, execute_with_rescue, find_worker_log, format_error, log_error, log_output_error, log_to_file, logger, logging_enabled?, print_to_log_file, rescue_error, setup_filename_logger, setup_logger_formatter, show_warning
Constructor Details
#initialize(opts) ⇒ WebsocketClient
host
-
Host of request. Required if no :url param was provided.
path
-
Path of request. Should start with ‘/’. Default: ‘/’
query
-
Query for request. Should be in format “aaa=bbb&ccc=ddd”
secure
-
Defines protocol to use. If true then wss://, otherwise ws://. This option will not change default port - it should be handled by programmer.
port
-
Port of request. Default: nil
opts
-
Additional options:
:reconnect - if true, it will try to reconnect
:retry_time - how often should retries happen when reconnecting [default = 1s]
Alternatively it can be called with a single hash where key names as symbols are the same as param names
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 20 def initialize(opts) # Initializing with a single hash @options = opts.symbolize_keys @auto_pong = @options.fetch(:auto_pong, nil) || CapistranoSentinel.config.auto_pong @read_buffer_size = @options.fetch(:read_buffer_size, nil) || CapistranoSentinel.config.read_buffer_size @reconnect = @options.fetch(:reconnect, nil) || CapistranoSentinel.config.reconnect @retry_time = @options.fetch(:retry_time, nil) || CapistranoSentinel.config.retry_time @secure = @options.fetch(:secure, nil) || CapistranoSentinel.config.secure @host = @options.fetch(:host, nil) || CapistranoSentinel.config.host @port = @secure ? 443 : (@options.fetch(:port, nil) || CapistranoSentinel.config.port) @path = @options.fetch(:path, nil) || CapistranoSentinel.config.path @query = @options.fetch(:query, nil) @actor ||= @options.fetch(:actor, nil) @channel ||= @options.fetch(:channel, nil) @closed = false @opened = false @on_open = lambda { log_to_file("native websocket client websocket connection opened") subscribe(@channel) if @channel.present? } @on_close = lambda { || log_to_file("#{@actor.class} client received on_close #{.inspect}") if @actor.present? && @actor.respond_to?(:on_close) if @actor.respond_to?(:async) @actor.async.on_close() else @actor.on_close() end end } @on_ping = lambda { || log_to_file("#{@actor.class} client received PING #{.inspect}") if @actor.present? && @actor.respond_to?(:on_ping) if @actor.respond_to?(:async) @actor.async.on_ping() else @actor.on_ping() end end } @on_error = lambda { |error| log_to_file("#{@actor.class} received ERROR #{error.inspect} #{error.backtrace}") if @actor.present? && @actor.respond_to?(:on_error) if @actor.respond_to?(:async) @actor.async.on_error(error) else @actor.on_error(error) end end } @on_message = lambda { || = parse_json() log_to_file("#{@actor.class} websocket client received JSON #{}") if @actor.present? && @actor.respond_to?(:async) log_to_file("#{@actor.class} works async on message #{.inspect}") @actor.async.() else @actor.() end } connect end |
Instance Attribute Details
#actor ⇒ Object
Returns the value of attribute actor.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def actor @actor end |
#auto_pong ⇒ Object
Returns the value of attribute auto_pong.
8 9 10 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8 def auto_pong @auto_pong end |
#on_error ⇒ Object
Returns the value of attribute on_error.
8 9 10 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8 def on_error @on_error end |
#on_message ⇒ Object
Returns the value of attribute on_message.
8 9 10 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8 def @on_message end |
#on_ping ⇒ Object
Returns the value of attribute on_ping.
8 9 10 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8 def on_ping @on_ping end |
#protocol_version ⇒ Object (readonly)
Returns the value of attribute protocol_version.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def protocol_version @protocol_version end |
#read_buffer_size ⇒ Object
Returns the value of attribute read_buffer_size.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def read_buffer_size @read_buffer_size end |
#read_thread ⇒ Object (readonly)
Returns the value of attribute read_thread.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def read_thread @read_thread end |
#reconnect=(value) ⇒ Object
Sets the attribute reconnect
8 9 10 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8 def reconnect=(value) @reconnect = value end |
#retry_time ⇒ Object
Returns the value of attribute retry_time.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def retry_time @retry_time end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
7 8 9 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7 def socket @socket end |
Instance Method Details
#is_windows? ⇒ Boolean
96 97 98 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 96 def is_windows? RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ end |
#publish(channel, data) ⇒ void
This method returns an undefined value.
publishes to a channel some data (can be anything)
120 121 122 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 120 def publish(channel, data) send_action('publish', channel, data) end |
#subscribe(channel, data = {}) ⇒ void
This method returns an undefined value.
subscribes to a channel . need to be used inside the connect block passed to the actor
107 108 109 110 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 107 def subscribe(channel, data = {}) log_to_file("#{self.class} tries to subscribe to channel #{channel} with #{data.inspect}") send_action('subscribe', channel, data) end |
#unsubscribe(channel) ⇒ void
This method returns an undefined value.
unsubscribes current client from a channel
131 132 133 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 131 def unsubscribe(channel) send_action('unsubscribe', channel) end |
#unsubscribe_all ⇒ void
This method returns an undefined value.
unsubscribes all clients from all channels
151 152 153 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 151 def unsubscribe_all send_action('unsubscribe_all') end |
#unsubscribe_clients(channel) ⇒ void
This method returns an undefined value.
unsubscribes all clients subscribed to a channel
142 143 144 |
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 142 def unsubscribe_clients(channel) send_action('unsubscribe_clients', channel) end |