Class: SmartFox::Client
- Inherits:
-
Object
- Object
- SmartFox::Client
- 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
-
#buddy_list ⇒ Object
readonly
Returns the value of attribute buddy_list.
-
#connected ⇒ Object
(also: #connected?)
readonly
Returns the value of attribute connected.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#room_list ⇒ Object
readonly
Returns the value of attribute room_list.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #add_handler(event, &proc) ⇒ Object
- #connect ⇒ Object
- #connect_succeeded ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
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( = {}) @room_list = {} @connected = false @buddy_list = [] @user_id = [:user_id] @user_name = [:user_name] @server = [:server] || 'localhost' @port = [:port] @events = {} end |
Instance Attribute Details
#buddy_list ⇒ Object (readonly)
Returns the value of attribute buddy_list.
9 10 11 |
# File 'lib/smartfox/client.rb', line 9 def buddy_list @buddy_list end |
#connected ⇒ Object (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 |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/smartfox/client.rb', line 9 def port @port end |
#room_list ⇒ Object (readonly)
Returns the value of attribute room_list.
9 10 11 |
# File 'lib/smartfox/client.rb', line 9 def room_list @room_list end |
#server ⇒ Object (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 |
#connect ⇒ Object
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_succeeded ⇒ Object
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 |