Class: CapistranoSentinel::WebsocketClient

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/capistrano_sentinel/classes/websocket_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 { |message|
    log_to_file("#{@actor.class} client received on_close  #{message.inspect}")
    if @actor.present? && @actor.respond_to?(:on_close)
      if @actor.respond_to?(:async)
        @actor.async.on_close(message)
      else
        @actor.on_close(message)
      end
    end
  }

  @on_ping    = lambda { |message|
    log_to_file("#{@actor.class} client received PING  #{message.inspect}")
    if @actor.present? && @actor.respond_to?(:on_ping)
      if @actor.respond_to?(:async)
        @actor.async.on_ping(message)
      else
        @actor.on_ping(message)
      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 { |message|
    message = parse_json(message)
    log_to_file("#{@actor.class} websocket client received JSON  #{message}")
    if @actor.present? && @actor.respond_to?(:async)
      log_to_file("#{@actor.class} works async on message #{message.inspect}")
      @actor.async.on_message(message)
    else
      @actor.on_message(message)
    end
  }
  connect
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor


7
8
9
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 7

def actor
  @actor
end

#auto_pongObject

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_errorObject

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_messageObject

Returns the value of attribute on_message


8
9
10
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8

def on_message
  @on_message
end

#on_pingObject

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_versionObject (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_sizeObject

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_threadObject (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

Parameters:

  • value

    the value to set the attribute reconnect to.


8
9
10
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 8

def reconnect=(value)
  @reconnect = value
end

#retry_timeObject

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

#socketObject (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

Returns:

  • (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)

Parameters:

  • channel (string)
  • data (#to_s)

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

Parameters:

  • channel (string)

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

Parameters:

  • channel (string)

131
132
133
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 131

def unsubscribe(channel)
  send_action('unsubscribe', channel)
end

#unsubscribe_allvoid

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

Parameters:

  • channel (string)

142
143
144
# File 'lib/capistrano_sentinel/classes/websocket_client.rb', line 142

def unsubscribe_clients(channel)
  send_action('unsubscribe_clients', channel)
end