SmartFox

Main class of the SmartFoxServer 3 Client API. Contains all methods to interact with the server.


This class is responsible for connecting the client to a SmartFoxServer instance, sending requests to the server and dispatching events triggered by the server.

Visit www.smartfoxserver.com for more information.

Constructor

new SmartFox()

Initializes a new instance of the SmartFox class.

Members

(readonly) buddyManager :SFSBuddyManager

Reference to the Buddy Manager.

The Buddy Manager is used internally by the SmartFoxServer Client API; the reference returned by this property gives access to the Buddy List, allowing interaction with SFSBuddy and SFSBuddyVariable objects and access to User properties in the Buddy List System.

(readonly) config :ConfigData

Client configuration settings.

The object properties are set to their default values when the SmartFox class is instantiated.

(readonly) httpUploadURI :string

HTTP/S URI that can be used to upload files to SmartFoxServer, using a regular HTTP POST request.

Uploads must be enabled on the server side for the specific Zone. The value of this property is null if the client is not yet logged in.

Type:
  • string

(readonly) isConnected :boolean

Indicates whether the client is currently connected to the server.

The value is false until a successful connection is established, or a disconnection occurred.

Type:
  • boolean

(readonly) lastJoinedRoom :SFSRoom

SFSRoom object representing the last Room joined by the client, if any.

This property is null if no Room was joined yet, or all joined Rooms have been left.

Type:

(readonly) logger :Logger

Reference to the internal logger used by the SmartFoxServer Client API.
Type:

(readonly) mySelf :SFSUser

SFSUser object representing the client when connected to a SmartFoxServer instance.

This object is generated upon successful login only, so it is null if login was not performed yet or client logged out.

Type:

(readonly) roomManager :SFSRoomManager

Reference to the Room Manager.

The Room Manager is used internally by the SmartFoxServer Client API; the reference returned by this property gives access to the Room List and Groups, allowing interaction with SFSRoom objects.

(readonly) sessionToken :string

Unique session token of the client.

The session token is a string sent by the server to the client after the initial handshake, so it is null until handshake is complete. It is required as a mean of identification when uploading files to the server.

Type:
  • string

(readonly) userManager :SFSUserManager

Reference to the User Manager.

The User Manager is used internally by the SmartFoxServer Client API; the reference returned by this property gives access to the User List, allowing interaction with SFSUser objects.

(readonly) version :string

Version string of the SmartFoxServer C# Client API.
Type:
  • string

Methods

addEventListener(evtType, callback, scope)

Adds a listener for a given API event type that will be used for callbacks.
Parameters:
NameTypeDescription
evtTypestringThe type (name) of event to listen to.
callbackfunctionThe listener function that processes the event. This function should accept an object as its only parameter, which in turn contains the event parameters.
scopeobjectThe object that acts as a context for the event listener: it is the object that acts as a "parent scope" for the callback function, thus providing context (i.e. access to variables and other mehtods) to the function itself.
Example

This example shows how to add a number of common event listeners to the SmartFox instance, usually during initialization:

function init()
{
	sfs = new SmartFox();

	// Add SFSEvent listeners
	sfs.addEventListener(SFSEvent.CONNECTION, onConnection, this);
	sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost, this);
	sfs.addEventListener(SFSEvent.LOGIN_ERROR, onLoginError, this);
	sfs.addEventListener(SFSEvent.LOGIN, onLogin, this);
	sfs.addEventListener(SFSEvent.LOGOUT, onLogout, this);
	sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError, this);
	sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin, this);
}

connect(cfgopt)

Establishes a connection between the client and a SmartFoxServer instance using the passed configuration object, if any.

The client communicates with the SmartFoxServer instance through a WebSocket connection.

It is possible to configure che connection without passing the cfg parameter: just set the properties on the default configuration object throught the SmartFox#config getter.

Parameters:
NameTypeAttributesDefaultDescription
cfgConfigData<optional>
nullThe client configuration object.

disconnect()

Closes the connection between the client and the SmartFoxServer instance.

enableLagMonitor(enabled, intervalopt, queueSizeopt)

Activates the automatic realtime monitoring of the lag between client and server (round robin).

When the lag monitor is activated, the SFSEvent.PING_PONG event type is then dispatched continuously. The event provides the average value of the last ten measured lag values, and the minimum and maximum lag avalues for the current lag monitoring session. The lag monitoring can be enabled after a successful login only and it is automatically halted when the client logs out of a Zone or gets disconnected.

Parameters:
NameTypeAttributesDefaultDescription
enabledbooleanThe lag monitoring status: true to start the monitoring, false to stop it.
intervalnumber<optional>
4An optional amount of seconds to wait between each query (recommended 3-4s).
queueSizenumber<optional>
10The amount of values stored temporarily and used to calculate the average lag.

getJoinedRooms() → {Array.<SFSRoom>}

Returns a list of SFSRoom objects representing the Rooms currently joined by the client.

The same list is returned by the SFSRoomManager#getJoinedRooms method, accessible through the SmartFox#roomManager getter; this is replicated on this class for handy access due to its usually frequent usage.

Returns:
A list of SFSRoom objects representing the Rooms currently joined by the client.
Type: 
Array.<SFSRoom>

killConnection()

Simulates an abrupt disconnection from the server.

This method should be used for testing and simulations only, otherwise use the SmartFox#disconnect method.

removeAllEventListeners()

Removes all event listeners.

removeEventListener(evtType, callback)

Removes a listener for a given API event.
Parameters:
NameTypeDescription
evtTypestringThe type (name) of event to remove.
callbackfunctionThe listener function to be removed.

send(request)

Sends a request to the server.
Parameters:
NameTypeDescription
requestBaseRequestA request object.
Examples

This example shows how to send a login request:

sfs.send(new LoginRequest("KermitTheFrog", "kermitPwd", "TheMuppetZone"));

This example sends a request to join a Room:

sfs.send(new JoinRoomRequest("Lobby"));

This example creates an object containing some parameters and sends it to the server-side Extension:

const params = new SFSObject();
params.putInt("x", 10);
params.putInt("y", 37);

sfs.send(new ExtensionRequest("setPosition", params));

setClientDetails(platformId, version)

Allows to set custom client details used to gather statistics about the client platform in the Analytics module of the SmartFoxServer Administration Tool.

This method must be called before the connection is started. The length of the two strings combined must be less than 512 characters.

If not set, the default value of the underlying property is the generic JavaScript. Version is not specified in this case.

Parameters:
NameTypeDescription
platformIdstringAn identifier of the runtime platform.
versionstringAn optional version number of the runtime platform.