module StatsCloud::SocketIOHelper

This helper works to create socket connection.

Private Instance Methods

connect_client() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 18
def connect_client
  eventmachine.run do
    eventmachine.add_periodic_timer(5) do
      try_connect
      eventmachine.add_timer(3) { eventmachine.stop if connected? }
    end
  end
end
eventmachine() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 73
def eventmachine
  EM
end
listen_connect() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 38
def listen_connect
  this = self

  @client.on :connect do
    emit :tags, this.tags
    this.logger.info "StatsCloud client has connected to StatsCloud server"
  end
end
listen_disconnect() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 47
def listen_disconnect
  this = self

  @client.on :disconnect do
    this.log_error("StatsCloud client disconnected from the StatsCloud server")
  end
end
listen_errors() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 65
def listen_errors
  this = self

  @client.on :error do |error|
    this.log_error error
  end
end
listen_events() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 55
def listen_events
  this = self

  @client.on :events do |names_map|
    this.names_map = names_map
    this.event_name_size_in_bytes = (Math.log(names_map.keys.length) / Math.log(256)).ceil
    this.event_name_size_in_bytes = 1 if this.event_name_size_in_bytes < 1
  end
end
open?() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 14
def open?
  !@client.nil? && @client.open?
end
socketio_client() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 77
def socketio_client
  ::StatsCloudIO::SocketIO::Client::Simple
end
try_connect() click to toggle source
# File lib/statscloud/helpers/statsmeter_client/socketio_helper.rb, line 27
def try_connect
  @client = socketio_client.connect @url, path: "/ws", "auth-token" => @token
  listen_connect
  listen_disconnect
  listen_events
  listen_errors
rescue StandardError => error
  log_error(error)
  close
end