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") }}*/
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);
}
}
};
Summary
Callback for sessions closed by the remote server.
Syntax
multiPlayerSession.onclose = function onSessionClosed()
{
// Alert the user of session closed
window.alert("Connection lost!");
};
Summary
The unique string identifier representing multiplayer session.
Syntax
var sessionId = multiPlayerSession.sessionId;
Summary
The unique string identifier representing the user on the multiplayer session.
Syntax
var playerId = multiPlayerSession.playerId;
Summary
Sends a message to a single user.
Syntax
var messageType = MyNetworkMessageTypes.update;
var messageData = JSON.stringify(updateData);
multiPlayerSession.sendTo(playerId, messageType, messageData);
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);
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);
Summary
Check if the MultiPlayerSession object is connected to a remote server.
Syntax
if (multiPlayerSession.connected())
{
// Draw connected icon
}
Summary
Make the multiplayer session public and allow anyone to join.
Syntax
var callbackFn = function () {window.alert('Session is public.')};
multiPlayerSession.makePublic(callbackFn);
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;