|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectsfs2x.client.SmartFox
public class SmartFox
SmartFox is the main class of the SmartFoxServer 2X API. It is responsible for connecting the client to a SmartFoxServer instance and for dispatching all asynchronous events. Developers always interact with SmartFoxServer through this class.
NOTE: in the provided examples, sfs always indicates a SmartFox instance.
| Constructor Summary | |
|---|---|
SmartFox()
|
|
SmartFox(boolean debug)
Creates a new SmartFox instance. |
|
| Method Summary | |
|---|---|
void |
addEventListener(java.lang.String eventType,
sfs2x.client.core.IEventListener listener)
|
void |
addJoinedRoom(Room room)
*Private* |
void |
connect()
|
void |
connect(java.lang.String host)
|
void |
connect(java.lang.String host,
int port)
Establishes a connection between the client and a SmartFoxServer 2X instance. |
void |
disconnect()
Closes the connection between the client and the SmartFoxServer 2X instance. |
void |
dispatchEvent(BaseEvent evt)
|
void |
enableLagMonitor(boolean enabled)
|
void |
enableLagMonitor(boolean enabled,
int interval)
|
void |
enableLagMonitor(boolean enabled,
int interval,
int queueSize)
Enables the automatic realtime monitoring of the lag between the client and the server (round robin) When turned on, the SFSEvent.PING_PONG event type is dispatched continuously, providing the average of the last ten measured lag values. |
IBuddyManager |
getBuddyManager()
Returns a reference to the Buddy Manager. |
int |
getCompressionThreshold()
Returns the current compression threshold. |
ConfigData |
getConfig()
Returns the client configuration details. |
java.lang.String |
getConnectionMode()
|
java.lang.String |
getCurrentIp()
Returns the IP address of the SmartFoxServer 2X instance to which the client is connected. |
int |
getCurrentPort()
Returns the TCP port of the SmartFoxServer 2X instance to which the client is connected. |
java.lang.String |
getCurrentZone()
Returns the Zone currently in use, if the user is already logged in. |
sfs2x.client.core.EventDispatcher |
getDispatcher()
|
java.util.List<Room> |
getJoinedRooms()
Returns a list of Room objects representing the Rooms currently joined by the client. |
sfs2x.client.util.LagMonitor |
getLagMonitor()
*Private* |
Room |
getLastJoinedRoom()
Returns the object representing the last Room joined by the client, if any. |
org.slf4j.Logger |
getLogger()
Returns a reference to the internal Logger instance used by SmartFoxServer 2X. |
int |
getMaxMessageSize()
Returns the maximum size of messages allowed by the server. |
User |
getMySelf()
Returns the User object representing the client when connected to a SmartFoxServer 2X instance. |
int |
getReconnectionSeconds()
*Private* |
Room |
getRoomById(int id)
Retrieves a Room object from its id. |
Room |
getRoomByName(java.lang.String name)
Retrieves a Room object from its name. |
java.util.List<Room> |
getRoomList()
Returns a list of Room objects representing the Rooms currently "watched" by the client. |
java.util.List<Room> |
getRoomListFromGroup(java.lang.String groupId)
Retrieves the list of Rooms which are part of the specified Room Group. |
IRoomManager |
getRoomManager()
Returns a reference to the Room Manager. |
java.lang.String |
getSessionToken()
Return the unique session token. |
sfs2x.client.bitswarm.BitSwarmClient |
getSocketEngine()
*Private* |
IUserManager |
getUserManager()
Returns a reference to the User Manager. |
java.lang.String |
getVersion()
Returns the current version of the SmartFoxServer 2X ActionScript 3 API. |
void |
handleClientDisconnection(java.lang.String reason)
*Private* |
void |
handleHandShake(BaseEvent evt)
|
void |
handleLogin(BaseEvent evt)
|
void |
handleLogout()
*Private* |
void |
initUdp()
|
void |
initUdp(java.lang.String udpHost)
|
void |
initUdp(java.lang.String udpHost,
int udpPort)
Initializes the UDP protocol by performing an handshake with the server. |
boolean |
isConnected()
Indicates whether the client is connected to the server or not. |
boolean |
isDebug()
Indicates whether the client-server messages debug is enabled or not. |
boolean |
isJoining()
*Private* |
boolean |
isUdpAvailable()
Indicates whether the UPD protocol is available or not in the current runtime. |
boolean |
isUdpInited()
Return true if the UDP handshake has been performed successfully |
void |
loadConfig()
|
void |
loadConfig(boolean connectOnSuccess)
|
void |
loadConfig(java.lang.String filePath)
|
void |
loadConfig(java.lang.String filePath,
boolean connectOnSuccess)
Loads the client configuration file. |
void |
removeAllEventListeners()
|
void |
removeEventListener(java.lang.String eventType,
sfs2x.client.core.IEventListener listener)
|
void |
removeJoinedRoom(Room room)
*Private* |
void |
send(sfs2x.client.requests.IRequest request)
Sends a request to the server. |
void |
setDebug(boolean debug)
*Private* |
void |
setJoining(boolean joining)
*Private* |
void |
setLastJoinedRoom(Room lastJoinedRoom)
*Private* |
void |
setMySelf(User mySelf)
*Private* |
void |
setReconnectionSeconds(int seconds)
*Private* |
void |
setUseBlueBox(boolean useBlueBox)
|
boolean |
useBlueBox()
Indicates whether the client should attempt a tunnelled http connection through the BlueBox in case a socket connection can't be established. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public SmartFox(boolean debug)
debug - If true, the SmartFoxServer API debug messages are logged.
ExamplesetDebug(boolean)public SmartFox()
| Method Detail |
|---|
public void enableLagMonitor(boolean enabled,
int interval,
int queueSize)
NOTE: the lag monitoring can be enabled after the login has been performed successfully only.
enabled - The lag monitoring status: true to start the monitoring, false to stop it.interval - An optional amount of seconds to pause between each query (recommended 3-4s)queueSize - The amount of values stored temporarily and used to calculate the average lagenabled - The lag monitoring status: true to start the monitoring, false to stop it.SFSEvent.PING_PONGpublic void enableLagMonitor(boolean enabled)
public void enableLagMonitor(boolean enabled,
int interval)
public sfs2x.client.bitswarm.BitSwarmClient getSocketEngine()
public sfs2x.client.util.LagMonitor getLagMonitor()
public boolean isConnected()
public java.lang.String getVersion()
public ConfigData getConfig()
null object is returned.
loadConfig()public boolean useBlueBox()
NOTE: this property must be set before the connect() method is called. Also, after a connection is established, this property does not return the current connection mode (socket or http); for this purpose use the connectionMode property.
getConnectionMode(),
loadConfig()public void setUseBlueBox(boolean useBlueBox)
public java.lang.String getConnectionMode()
public int getCompressionThreshold()
This value represents the maximum message size (in bytes) before the protocol compression is activated. It is determined by the server configuration.
public int getMaxMessageSize()
Any request exceeding this size will not be sent. The value is determined by the server configuration.
public Room getRoomById(int id)
NOTE: the same object is returned by the IRoomManager.getRoomById() method, accessible through the roomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
id - The id of the Room.
Examplenull if no Room object with the passed id exists in the Rooms list.getRoomByName(String),
getRoomList(),
getRoomManager()public Room getRoomByName(java.lang.String name)
NOTE: the same object is returned by the IRoomManager.getRoomByName() method, accessible through the roomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
name - The name of the Room.
Examplenull if no Room object with the passed name exists in the Rooms list.getRoomById(int),
getRoomList(),
getRoomManager()public java.util.List<Room> getRoomListFromGroup(java.lang.String groupId)
NOTE: the same list is returned by the IRoomManager.getRoomListFromGroup() method, accessible through the roomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
groupId - The name of the Group.
getRoomManager(),
Room
public void connect(java.lang.String host,
int port)
The client usually connects to a SmartFoxServer instance through a socket connection. In case a socket connection can't be established,
and the useBlueBox property is set to true, a tunnelled http connection through the BlueBox module is attempted.
When a successful connection is established, the connectionMode property can be used to check the current connection mode.
host - The address of the server to connect to.port - The TCP port to connect to.
ExampleArgumentError - If an invalid host/address or port is passed, and it can't be found in the loaded settings.loadConfig(),
setUseBlueBox(boolean),
getConnectionMode(),
disconnect(),
SFSEvent.CONNECTIONpublic void connect(java.lang.String host)
public void connect()
public void disconnect()
connect(String, int),
SFSEvent.CONNECTION_LOSTpublic boolean isDebug()
true, detailed debugging informations for all the incoming and outgoing messages are provided.
Debugging can be enabled when instantiating the SmartFox class too.
public void setDebug(boolean debug)
public java.lang.String getCurrentIp()
getCurrentPort(),
connect(),
loadConfig()public int getCurrentPort()
getCurrentIp(),
connect(),
loadConfig()public java.lang.String getCurrentZone()
loadConfig(),
LoginRequestpublic User getMySelf()
null if login was not performed yet.
NOTE: setting the mySelf property manually can disrupt the API functioning.
User.isItMe(),
LoginRequestpublic void setMySelf(User mySelf)
public org.slf4j.Logger getLogger()
public Room getLastJoinedRoom()
null if no Room was joined.
NOTE: setting the lastJoinedRoom property manually can disrupt the API functioning. Use the JoinRoomRequest request to join a new Room instead.
getJoinedRooms(),
JoinRoomRequestpublic void setLastJoinedRoom(Room lastJoinedRoom)
public java.util.List<Room> getJoinedRooms()
NOTE: the same list is returned by the IRoomManager.getJoinedRooms() method, accessible through the roomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
getLastJoinedRoom(),
getRoomManager(),
Room,
JoinRoomRequestpublic java.util.List<Room> getRoomList()
NOTE 1: at login time, the client automatically subscribes all the Room Groups specified in the Zone's Default Room Groups setting.
NOTE 2: the same list is returned by the IRoomManager.getRoomList() method, accessible through the roomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
getRoomManager(),
JoinRoomRequest,
SubscribeRoomGroupRequest,
UnsubscribeRoomGroupRequestpublic IRoomManager getRoomManager()
public IUserManager getUserManager()
public IBuddyManager getBuddyManager()
public boolean isUdpAvailable()
Using the UDP protocol in an application requires that a handshake is performed between the client and the server. By default this is NOT done by the SmartFoxServer 2X API, to avoid allocating resources that might never be used. In order to activate the UDP support, the initUDP() method must be invoked explicitly.
initUdp(String, int)public boolean isUdpInited()
initUdp(String, int)public void initUdp()
initUdp(String, int)public void initUdp(java.lang.String udpHost)
initUdp(String, int)
public void initUdp(java.lang.String udpHost,
int udpPort)
This method needs to be called only once. It can be executed at any moment provided that a connection to the server has already been established. After a successful initialization, UDP requests can be sent to the server-side Extension at any moment.
MTU note
The Maximum Transmission Unit (MTU), represents the largest amount of bytes that can be sent at once before packet fragmentation occurs. Since the UDP protocol uses a "nothing-or-all" approach to the transmission, it is important to keep in mind that, on average, a message size of 1100-1200 bytes is probably the maximum you can reach. If you exceed the MTU size the data will be "lost in hyperspace" (the Internet).
Another interesting matter is that there is no fixed size for the MTU, because each operating system uses a slightly different value. Because of this we suggest a conservative data size of 1000-1200 bytes per packet, to avoid packet loss. The SFS2X protocol compression allows to send 2-3KBytes of uncompressed data, which usually is squeezed down to a size of ~1000 bytes. If you have a larger amount of data to send, we suggest you to organize it in smaller chunks so that they don't exceed the suggested MTU size.
More details about the MTU can be found at the page linked below.
udpHost - The IP address of the server to connect to.udpPort - The UDP port to connect to.
ExampleArgumentError - If an invalid address or port is passed, and it can't be found in the loaded settings.isUdpAvailable(),
loadConfig(),
http://en.wikipedia.org/wiki/Maximum_transmission_unit,
SFSEvent.UDP_INITpublic boolean isJoining()
public void setJoining(boolean joining)
public java.lang.String getSessionToken()
public sfs2x.client.core.EventDispatcher getDispatcher()
getDispatcher in interface sfs2x.client.core.IDispatchablepublic int getReconnectionSeconds()
public void setReconnectionSeconds(int seconds)
public void send(sfs2x.client.requests.IRequest request)
request - A request object.
Examplesfs2x.client.requests,
sfs2x.client.requests.buddylist,
sfs2x.client.requests.game
public void loadConfig(java.lang.String filePath,
boolean connectOnSuccess)
The SmartFox instance can be configured through an external XML configuration file loaded at run-time.
By default, the loadConfig() method loads a file named "sfs-config.xml", placed in the same folder of the application swf file.
If the connectOnSuccess argument is set to true, on loading completion the connect() method is automatically called by the API,
otherwise the SFSEvent.CONFIG_LOAD_SUCCESS event type is dispatched. In case of loading error, the SFSEvent.CONFIG_LOAD_FAILURE event type id fired.
The external XML configuration file has the following structure; ip, port and zone parameters are mandatory, all other parameters are optional:
filePath - Filename of the external XML configuration, including its path relative to the folder of the application swf file.connectOnSuccess - A flag indicating if the connection to SmartFoxServer must be attempted upon configuration loading completion.
Exampleconnect(),
SFSEvent.CONFIG_LOAD_SUCCESS,
SFSEvent.CONFIG_LOAD_FAILUREpublic void loadConfig(java.lang.String filePath)
public void loadConfig(boolean connectOnSuccess)
public void loadConfig()
public void addJoinedRoom(Room room)
throws com.smartfoxserver.v2.exceptions.SFSException
com.smartfoxserver.v2.exceptions.SFSExceptionpublic void removeJoinedRoom(Room room)
public void handleHandShake(BaseEvent evt)
public void handleLogin(BaseEvent evt)
public void handleClientDisconnection(java.lang.String reason)
public void handleLogout()
public void dispatchEvent(BaseEvent evt)
public void addEventListener(java.lang.String eventType,
sfs2x.client.core.IEventListener listener)
addEventListener in interface sfs2x.client.core.IDispatchable
public void removeEventListener(java.lang.String eventType,
sfs2x.client.core.IEventListener listener)
public void removeAllEventListeners()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||