Class: Multi

client/multi~ Multi

Use this class to create new sessions or connect to existing ones. You can get ready a to use instance of this class by initializing the multi framework with multiModule.init(options).

<protected> new Multi(options)

Parameters:
Name Type Description
options module:client/multi~MultiOptions to tweak this instances behaviour
Source:

Methods

autoJoinOrCreateSession() → {external:Promise}

Tries to auto join an existing session. When no valid session token can be extracted from the URL a new session will be created instead.
As this operation is executed asynchrony a Q promise will be returned.
Source:
Returns:
On success the promise will be resolved with the created or joined Session instance.

On error it will be rejected with either NoSuchSessionError, SessionFullError, TokenAlreadyExistsError, JoiningDisabledError, or NoConnectionError.
Type
external:Promise

autoJoinSession() → {external:Promise}

Tries to connect to a session that does already exist on the server. The session token will be extracted from the URL by using characters after the url hash.
As this operation is executed asynchrony a Q promise will be returned.
Source:
Returns:
On success the promise will be resolved with the joined Session instance.

On error it will be rejected with either NoSuchSessionError, SessionFullError, JoiningDisabledError, NoSessionTokenFoundError, or NoConnectionError.
Type
external:Promise

createSession(options) → {external:Promise}

Tries to create a new game session on the server. As this operation is executed asynchrony a Q promise will be returned.
Parameters:
Name Type Argument Description
options SessionOptions <optional>
To tweak this new sessions behaviour. If not provided, the session section of the multiOptions-object will be used. If that does not exist either the default values will be used.
Source:
Returns:
On success the promise will be resolved with the created Session instance.

On error it will be rejected with either TokenAlreadyExistsError,
ScriptNameNotAllowedError, or NoConnectionError.
Type
external:Promise
Example
var multiOptions = {
 server: 'http://mySocketioServer/',
 session: {
   minPlayerNeeded: 3,
   maxPlayerAllowed: 5
 }
};

function onSession(session) {
 console.log('session created', session.token);
}
function onSessionFailed(error) {
 console.log('session creation failed:', error.message);
}

// init and try to create the session
var multi = multiModule.init(multiOptions);
multi.createSession().then(onSession, onSessionFailed).done();

joinSession(sessionToken) → {external:Promise}

Tries to connect to a session that does already exist on the server. As this operation is executed asynchrony a Q promise will be returned.
Parameters:
Name Type Description
sessionToken string unique token of the session you want to join
Source:
Returns:
On success the promise will be resolved with the joined Session instance.

On error it will be rejected with either NoSuchSessionError, SessionFullError, JoiningDisabledError, or NoConnectionError.
Type
external:Promise
Example
var multiOptions = {
 server: 'http://mySocketioServer/'
};

function onSession(session) {
 console.log('session joined', session.token);
}
function onSessionFailed(error) {
 console.log('session joining failed:', error.message);
}

// init and join the session
var multi = multiModule.init(multiOptions);
multi.joinSession('123').then(onSession, onSessionFailed).done();