SmartFoxServer 2X JavaScript client API (binary)

Welcome to the SmartFoxServer 2X JavaScript client API documentation.

You can start exploring the API from the SmartFox class. The other relevant classes you will often use are those with names ending with Request, for example the LoginRequest class.

Some of the classes listed here are never instantiated by developers directly; instances are returned by methods or as event parameters (for example instances of SFSUser or SFSRoom). You can use this API reference to learn what properties and methods are available on those instances.

Getting started

IMPORTANT: make sure that WebSocket protocol is active and set to 'binary' mode in the SmartFoxServer 2X configuration. If not, enable it using the AdminTool's Server Configurator module (Web server tab) and restart the SmartFoxServer instance.

If you added the API's JavaScript file to your project manually, in order to get started with the development, you can import it in your main HTML page by means of the <script> tag (the version number in the file name may change):

<script type="text/javascript" src="sfs2x-api-1.8.0.js"></script>;

If you installed the API as an npm package, you can use a module bundler (we recommend webpack) and the ES2015 module loading syntax:

import * as SFS2X from "sfs2x-api";

Whatever import method you use, you can then access all instantiable classes in your project's code by means of the SFS2X namespace. The following example shows how to create an instance of the main SmartFox class, add a few listeners and establish a connection with SmartFoxServer.

var sfs = new SFS2X.SmartFox();
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);

sfs.connect(127.0.0.1, 8080);

function onConnection(evtParams)
{
 	if (evtParams.success)
 		console.log("Connected to SmartFoxServer 2X!");
 	else
 		console.log("Connection failed. Is the server running at all?");
}

Note

If you are using a module bundler, you can also import specific classes instead of the SFS2X namespace, like this:

import {SmartFox, SFSEvent} from "sfs2x-api-1.8.0";

In this case, just ignore the SFS2X namespace in all examples provided in this documentation.

AddBuddyRequest

BaseRequest

ChangeRoomNameRequest

ChangeRoomPasswordStateRequest

CreateSFSGameRequest

GoOnlineRequest

InitBuddyListRequest

LeaveRoomRequest

LogoutRequest

PlayerToSpectatorRequest

RemoveBuddyRequest

SetBuddyVariablesRequest

SetUserVariablesRequest

SpectatorToPlayerRequest

SubscribeRoomGroupRequest

UnsubscribeRoomGroupRequest