Table Of Contents

Previous topic

23.8. The MappingTable Object

Next topic

23.10. The MultiPlayerSessionManager Object

This Page

23.9. The MultiPlayerSession Object

The MultiPlayerSession object represents the user’s current multiplayer session. You should only have one instance of a MultiPlayerSession at a time. Once the session is over you should call destroy before creating a new session. You should also destroy any remaining MultiPlayerSession objects in TurbulenzEngine.onunload.

A MultiPlayerSession object can be created through the MultiPlayerSessionManager object which can be accessed through TurbulenzServices.createMultiPlayerSessionManager.

Required scripts

The MultiPlayerSession object requires:

/*{{ javascript("jslib/utilities.js") }}*/
/*{{ javascript("jslib/services/multiplayersession.js") }}*/

23.9.1. Properties

23.9.1.1. onmessage

Summary

Callback for received messages.

Syntax

multiPlayerSession.onmessage = function onReceivedMessage(senderId,
                                                          messageType,
                                                          messageData)
{
    if (messageType === MyNetworkMessageTypes.update)
    {
        if (messageData)
        {
            var updateData = JSON.parse(messageData);

            game.update(senderId, updateData);
        }
    }
};
senderId
Unique string identifier representing the user that sent the message.
messageType
User-defined number specifying the type of message received.
messageData
String containing the message data.

23.9.1.2. onclose

Summary

Callback for sessions closed by the remote server.

Syntax

multiPlayerSession.onclose = function onSessionClosed()
{
    // Alert the user of session closed
    window.alert("Connection lost!");
};

23.9.1.3. sessionId

Summary

The unique string identifier representing multiplayer session.

Syntax

var sessionId = multiPlayerSession.sessionId;

23.9.1.4. playerId

Summary

The unique string identifier representing the user on the multiplayer session.

Syntax

var playerId = multiPlayerSession.playerId;

23.9.2. Methods

23.9.2.1. sendTo

Summary

Sends a message to a single user.

Syntax

var messageType = MyNetworkMessageTypes.update;
var messageData = JSON.stringify(updateData);
multiPlayerSession.sendTo(playerId, messageType, messageData);
playerId
Unique string identifier representing the user to send the message to.
messageType
User-defined number specifying the type of message.
messageData
String containing the message data to send.

23.9.2.2. sendToGroup

Summary

Sends a message to multiple users.

Syntax

var playersIds = [playerAid, playerBid];
var messageType = MyNetworkMessageTypes.update;
var messageData = JSON.stringify(updateData);
multiPlayerSession.sendToGroup(playersIds, messageType, messageData);
playersIds
Array containing the unique string identifiers representing the users to send the message to.
messageType
User-defined number specifying the type of message.
messageData
String containing the message data to send.

23.9.2.3. sendToAll

Summary

Sends a message to all the users on the multiplayer session except the current one.

Syntax

var messageType = MyNetworkMessageTypes.update;
var messageData = JSON.stringify(updateData);
multiPlayerSession.sendToAll(messageType, messageData);
messageType
User-defined number specifying the type of message.
messageData
String containing the message data to send.

23.9.2.4. connected

Summary

Check if the MultiPlayerSession object is connected to a remote server.

Syntax

if (multiPlayerSession.connected())
{
    // Draw connected icon
}

23.9.2.5. makePublic

Summary

Make the multiplayer session public and allow anyone to join.

Syntax

var callbackFn = function () {window.alert('Session is public.')};
multiPlayerSession.makePublic(callbackFn);
callbackFn
(Optional) Callback triggered when session has been made public

23.9.2.6. destroy

Summary

Destroy a MultiPlayerSession.

Syntax

multiPlayerSession.destroy();

Example

//example usage:
gameDestroy = function gameDestroyFn()
{
    // destroy Turbulenz engine and JavaScript library objects
    ...

    multiPlayerSession.destroy();
    multiPlayerSession = null;

    multiPlayerSessionManager.destroy();
    multiPlayerSessionManager = null;

    mappingTable = null;
    userDataManager = null;
    leaderboardManager = null;

    gameSession.destroy();
    gameSession = null;
};

TurbulenzEngine.onunload = gameDestroy;