Class: SmartFox::Client
- Inherits:
-
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 ]
- EVENTS =
[ :connected, :logged_in, :rooms_updated ]
'sys'
'xt'
- ACTION_VERSION_CHECK =
'verChk'
- ACTION_API_OK =
'apiOK'
- ACTION_API_OBSOLETE =
'apiKO'
- ACTION_LOGIN =
'login'
- ACTION_LOGIN_OK =
'logOK'
- ACTION_AUTO_JOIN =
'autoJoin'
- ACTION_JOIN_ROOM =
'joinRoom'
- ACTION_UPDATE_ROOMS =
'getRmList'
- ACTION_ROOM_LIST =
'rmList'
- ACTION_JOIN_OK =
'joinOK'
- ACTION_JOIN_FAIL =
'joinKO'
- EXTENDED_RESPONSE =
'xtRes'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/smartfox/client.rb', line 37
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 = {}
@rooms = {}
@users = {}
end
|
Instance Attribute Details
#buddy_list ⇒ Object
Returns the value of attribute buddy_list.
10
11
12
|
# File 'lib/smartfox/client.rb', line 10
def buddy_list
@buddy_list
end
|
#connected ⇒ Object
Also known as:
connected?
Returns the value of attribute connected.
10
11
12
|
# File 'lib/smartfox/client.rb', line 10
def connected
@connected
end
|
#current_room ⇒ Object
Returns the value of attribute current_room.
11
12
13
|
# File 'lib/smartfox/client.rb', line 11
def current_room
@current_room
end
|
#port ⇒ Object
Returns the value of attribute port.
10
11
12
|
# File 'lib/smartfox/client.rb', line 10
def port
@port
end
|
#room_list ⇒ Object
Returns the value of attribute room_list.
10
11
12
|
# File 'lib/smartfox/client.rb', line 10
def room_list
@room_list
end
|
#server ⇒ Object
Returns the value of attribute server.
10
11
12
|
# File 'lib/smartfox/client.rb', line 10
def server
@server
end
|
#user_id ⇒ Object
Returns the value of attribute user_id.
13
14
15
|
# File 'lib/smartfox/client.rb', line 13
def user_id
@user_id
end
|
#user_name ⇒ Object
Returns the value of attribute user_name.
13
14
15
|
# File 'lib/smartfox/client.rb', line 13
def user_name
@user_name
end
|
#users ⇒ Object
Returns the value of attribute users.
11
12
13
|
# File 'lib/smartfox/client.rb', line 11
def users
@users
end
|
Instance Method Details
#add_handler(event, &proc) ⇒ Object
86
87
88
89
|
# File 'lib/smartfox/client.rb', line 86
def add_handler(event, &proc)
@events[event.to_sym] = [] unless @events[event.to_sym]
@events[event.to_sym] << proc
end
|
#connect ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/smartfox/client.rb', line 50
def connect()
unless @connected
TRANSPORTS.each do |transport_class|
begin
started_at = Time.now
@transport = transport_class.new(self)
@transport.connect
while not connected? and Time.now <= started_at + CONNECTION_TIMEOUT
Thread.pass
end
return @transport if connected?
rescue
end
end
raise ConnectionFailureError.new "Could not negotiate any transport with server."
end
end
|
#connect_succeeded ⇒ Object
#disconnect ⇒ Object
70
71
72
73
74
75
|
# File 'lib/smartfox/client.rb', line 70
def disconnect
if @connected
@transport.disconnect
@connected = false
end
end
|
#extended_command(action, data = nil, room = -1,, options = {}) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/smartfox/client.rb', line 77
def extended_command(action, data = nil, room = -1, options = {})
options[:format] ||= :xml
case options[:format]
when :xml
send_packet(HEADER_EXTENDED, action, room) { |x| x.cdata!(data || '') }
end
end
|
#join_room(room) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/smartfox/client.rb', line 103
def join_room(room)
if(@current_room == -1 || @current_room == nil)
leave_current_room = "0"
room_to_leave = -1
else
leave_current_room = "1"
room_to_leave = @current_room
end
send_packet(HEADER_SYSTEM, ACTION_JOIN_ROOM, room.id) { |x|
x.room(:id=>room.id,:pwd=>'',:spec=>'0',:leave=>leave_current_room,:old=>room_to_leave)
}
end
|
#login(zone, username, password = nil) ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'lib/smartfox/client.rb', line 137
def login(zone, username, password = nil)
send_packet(HEADER_SYSTEM, ACTION_LOGIN) do |packet|
packet.login(:z => zone) do |login|
login.nick { |nick| nick.cdata! username }
login.pword { |pword| pword.cdata! password || '' }
end
end
Thread.pass
end
|
#packet_recieved(data) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/smartfox/client.rb', line 117
def packet_recieved(data)
begin
SmartFox::Logger.debug "SmartFox::Client#packet_recieved('#{data}')"
packet = SmartFox::Packet.parse(data)
case packet.
when HEADER_SYSTEM
if @connected
handle_system_packet(packet)
else
complete_login(packet)
end
when HEADER_EXTENDED
raise_event :extended_response, self, packet.data
end
rescue => e
SmartFox::Logger.error "In SmartFox::Client#packet_received"
SmartFox::Logger.exception e
end
end
|
#parse_user(node) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/smartfox/client.rb', line 161
def parse_user(node)
if user = @users[node['i'].to_i]
user.parse(node)
else
@users[node['i'].to_i] = SmartFox::User.parse(node)
end
end
|
#send_extended(extension, action, options) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/smartfox/client.rb', line 147
def send_extended(extension, action, options)
SmartFox::Logger.debug "send_extended #{extension},#{action},#{options}"
options[:format] ||= :xml
options[:room] ||= (@current_room ? @current_room.id : 0)
options[:parameters] ||= {}
case options[:format]
when :xml
when :json
send_extended_json_packet(extension, action, options[:room], options[:parameters])
end
end
|