Method: Sc2::Connection#connect

Defined in:
lib/sc2ai/connection.rb

#connectvoid

This method returns an undefined value.

Attempts to connect for a period of time, ignoring errors nad performing on_* callbacks



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sc2ai/connection.rb', line 53

def connect
  attempt = 1
  Sc2.logger.debug { "Waiting for client..." } if (attempt % 5).zero?

  begin
    @websocket = Async::WebSocket::Client.connect(endpoint) # , handler: Sc2::Connection::Connection)
    @listeners[ConnectionListener.name]&.each { _1.on_connected(self) }
    # do initial ping to ensure status is set and connection is working
    response_ping = ping
    Sc2.logger.debug { "Game version: #{response_ping.game_version}" }
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EADDRNOTAVAIL
    raise Error, "Connection timeout. Max retry exceeded." unless (attempt += 1) < 30 # 30s attempts
    @listeners[ConnectionListener.name]&.each { _1.on_connection_waiting(self) }
    sleep(1)
    retry
  rescue Error => e
    Sc2.logger.error "#{e.class}: #{e.message}"
    @listeners[ConnectionListener.name]&.each { _1.on_disconnect(self) }
  end
  nil
end