Class: SmartFox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smartfox/client.rb

Defined Under Namespace

Classes: ApiIncompatibleError, ConnectionFailureError, TransportTimeoutError

Constant Summary collapse

CLIENT_VERSION =
"1.5.8"
CONNECTION_TIMEOUT =
5
TRANSPORTS =
[ SmartFox::Socket::Connection, SmartFox::BlueBox::Connection ]
HEADER_SYSTEM =
'sys'
ACTION_VERSION_CHECK =
'verChk'
ACTION_API_OK =
'apiOK'
ACTION_API_OBSOLETE =
'apiKO'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
30
# File 'lib/smartfox/client.rb', line 21

def initialize(options = {})
  @room_list = {}
  @connected = false
  @buddy_list = []
  @user_id = options[:user_id]
  @user_name = options[:user_name]
  @server = options[:server] || 'localhost'
  @port = options[:port]
  @events = {}
end

Instance Attribute Details

#buddy_listObject (readonly)

Returns the value of attribute buddy_list.



9
10
11
# File 'lib/smartfox/client.rb', line 9

def buddy_list
  @buddy_list
end

#connectedObject (readonly) Also known as: connected?

Returns the value of attribute connected.



9
10
11
# File 'lib/smartfox/client.rb', line 9

def connected
  @connected
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/smartfox/client.rb', line 9

def port
  @port
end

#room_listObject (readonly)

Returns the value of attribute room_list.



9
10
11
# File 'lib/smartfox/client.rb', line 9

def room_list
  @room_list
end

#serverObject (readonly)

Returns the value of attribute server.



9
10
11
# File 'lib/smartfox/client.rb', line 9

def server
  @server
end

Instance Method Details

#add_handler(event, &proc) ⇒ Object



48
49
50
51
# File 'lib/smartfox/client.rb', line 48

def add_handler(event, &proc)
  @events[event.to_sym] = [] unless @events[event.to_sym]
  @events[event.to_sym] << proc
end

#connectObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/smartfox/client.rb', line 32

def connect()
  unless @connected
    TRANSPORTS.each do |transport_class|
      begin
        @transport = transport_class.new(self)
        @transport.connect
        if connected?
          return @transport
        end
      rescue
      end
    end
    raise ConnectionFailureError.new "Could not negotiate any transport with server."
  end
end

#connect_succeededObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/smartfox/client.rb', line 53

def connect_succeeded
  send_packet(HEADER_SYSTEM, ACTION_VERSION_CHECK) { |x| x.ver(:v => CLIENT_VERSION.delete('.')) }
  connect_response = wait_for_packet(CONNECTION_TIMEOUT)
  if connect_response.header == HEADER_SYSTEM and connect_response.action == ACTION_API_OK
    @connected = true
    SmartFox::Logger.info "SmartFox::Client successfully connected with transport #{@transport.inspect}"
    raise_event :connected, self
  else
    raise ApiIncompatibleError if connect_response.action == ACTION_API_OBSOLETE
    raise ConnectionFailureError.new "Did not recieve an expected response from the server."
  end
end