AirConsole

AirConsole

new AirConsole(opts)

Your gateway object to AirConsole. There are getter and setter functions for all properties. Do not access properties of this object directly.
Parameters:
Name Type Description
opts AirConsole~Config Constructor config.
Source:

Members

(static, constant) ORIENTATION_LANDSCAPE :string

The landscape orientation.
Type:
  • string
Source:

(static, constant) ORIENTATION_PORTRAIT :string

The portrait orientation.
Type:
  • string
Source:

(static, constant) SCREEN :number

The device ID of the game screen.
Type:
  • number
Source:

Methods

broadcast(data)

Sends a message to all connected devices.
Parameters:
Name Type Description
data
Source:

convertDeviceIdToPlayerNumber(device_id) → {number|undefined}

Returns the player number for a device_id, if the device_id is part of the active players previously set by the screen by calling setActivePlayers. Player numbers are zero based and are consecutive. If the device_id is not part of the active players, this function returns undefined.
Parameters:
Name Type Description
device_id
Source:
Returns:
Type
number | undefined

convertPlayerNumberToDeviceId(player_number) → {number|undefined}

Returns the device_id of a player, if the player is part of the active players previously set by the screen by calling setActivePlayers. If fewer players are in the game than the passed in player_number or the active players have not been set by the screen, this function returns undefined.
Parameters:
Name Type Description
player_number
Source:
Returns:
Type
number | undefined

editProfile()

Lets the user change his nickname, profile picture and email address. If you need a real nickname of the user, use this function. onDeviceProfileChange will be called if the user logs in.
Source:

getActivePlayerDeviceIds() → {Array}

Returns an array of device_ids of the active players previously set by the screen by calling setActivePlayers. The first device_id in the array is the first player, the second device_id in the array is the second player, ...
Source:
Returns:
Type
Array

getControllerDeviceIds() → {Array}

Returns all controller device ids that have loaded your game.
Source:
Returns:
Type
Array

getCustomDeviceState(device_id) → {Object|undefined}

Gets the custom DeviceState of a device.
Parameters:
Name Type Description
device_id number | undefined The device ID of which you want the custom state. Default is this device.
Source:
Returns:
The custom data previously set by the device.
Type
Object | undefined

getDeviceId() → {number}

Returns the device_id of this device. Every device in an AirConsole session has a device_id. The screen always has device_id 0. You can use the AirConsole.SCREEN constant instead of 0. All controllers also get a device_id. You can NOT assume that the device_ids of controllers are consecutive or that they start at 1. DO NOT HARDCODE CONTROLLER DEVICE IDS! If you want to have a logic with "players numbers" (Player 0, Player 1, Player 2, Player 3) use the setActivePlayers helper function! You can hardcode player numbers, but not device_ids. Within an AirConsole session, devices keep the same device_id when they disconnect and reconnect. Different controllers will never get the same device_id in a session. Every device_id remains reserved for the device that originally got it. For more info read http:// developers.airconsole.com/#/guides/device_ids_and_states
Source:
Returns:
Type
number

getMasterControllerDeviceId() → {number|undefined}

Returns the device ID of the master controller.
Source:
Returns:
Type
number | undefined

getNickname(device_id) → {string|undefined}

Returns the nickname of a user.
Parameters:
Name Type Description
device_id number | undefined The device id for which you want the nickname. Default is this device. Screens don't have nicknames.
Source:
Returns:
Type
string | undefined

getProfilePicture(device_id, size) → {string|undefined}

Returns the url to a profile picture of the user.
Parameters:
Name Type Description
device_id number | undefined The device id for which you want the profile picture. Default is this device. Screens don't have profile pictures.
size number | undefined The size of in pixels of the picture. Default is 64.
Source:
Returns:
Type
string | undefined

getServerTime() → {number}

Returns the current time of the game server. This allows you to have a synchronized clock: You can send the server time in a message to know exactly at what point something happened on a device. This function can only be called if the AirConsole was instantiated with the "synchronize_time" opts set to true and after onReady was called.
Source:
Returns:
Timestamp in milliseconds.
Type
number

getUID(device_id) → {string|undefined}

Returns the globally unique id of a device.
Parameters:
Name Type Description
device_id number | undefined The device id for which you want the uid. Default is this device.
Source:
Returns:
Type
string | undefined

isUserLoggedIn(device_id) → {boolean}

Returns true if a user is logged in.
Parameters:
Name Type Description
device_id number | undefined The device_id of the user. Default is this device.
Source:
Returns:
Type
boolean

message(device_id, data)

Sends a message to another device.
Parameters:
Name Type Description
device_id number | undefined The device ID to send the message to. If "device_id" is undefined, the message is sent to all devices (except this one).
data
Source:
Request that all devices return to the AirConsole store.
Source:
Request that all devices load a game by url.
Source:

(abstract) onActivePlayersChange(player_number)

Gets called when the screen sets the active players by calling setActivePlayers().
Parameters:
Name Type Description
player_number number | undefined The player number of this device. Can be undefined if this device is not part of the active players.
Source:

(abstract) onAdComplete(ad_was_shown)

Gets called when an advertisement is finished or no advertisement was shown.
Parameters:
Name Type Description
ad_was_shown boolean True iff an ad was shown and onAdShow was called.
Source:

(abstract) onAdShow()

Gets called if a fullscreen advertisement is shown on this screen. In case this event gets called, please mute all sounds.
Source:

(abstract) onConnect(device_id)

Gets called when a device has connected and loaded the game.
Parameters:
Name Type Description
device_id number the device ID that loaded the game.
Source:

(abstract) onCustomDeviceStateChange(device_id, custom_data)

Gets called when a device updates it's custom DeviceState by calling setCustomDeviceState or setCustomDeviceStateProperty. Make sure you understand the power of device states: http://developers.airconsole.com/#/guides/device_ids_and_states
Parameters:
Name Type Description
device_id number the device ID that changed its custom DeviceState.
custom_data Object The custom DeviceState data value
Source:

(abstract) onDeviceMotion(data)

Gets called every X milliseconds with device motion data iff the AirConsole was instantiated with the "device_motion" opts set to the interval in milliseconds. Only works for controllers. Note: Some browsers do not allow games to access accelerometer and gyroscope in an iframe (your game). So use this method if you need gyroscope or accelerometer data.
Parameters:
Name Type Description
data object data.x, data.y, data.z for accelerometer data.alpha, data.beta, data.gamma for gyroscope
Source:

(abstract) onDeviceProfileChange(device_id)

Gets called when a device updates it's profile pic, nickname or email.
Parameters:
Name Type Description
device_id number The device_id that changed its profile.
Source:

(abstract) onDeviceStateChange(device_id, user_data)

Gets called when a device joins/leaves a game session or updates its DeviceState (custom DeviceState, profile pic, nickname, internal state). This is function is also called every time onConnect, onDisconnect or onCustomDeviceStateChange, onDeviceProfileChange is called. It's like their root function.
Parameters:
Name Type Description
device_id number the device_id that changed its DeviceState.
user_data AirConsole~DeviceState the data of that device. If undefined, the device has left.
Source:

(abstract) onDisconnect(device_id)

Gets called when a device has left the game.
Parameters:
Name Type Description
device_id number the device ID that left the game.
Source:

(abstract) onEmailAddress(email_address)

Gets called if the request of requestEmailAddress() was granted. For privacy reasons, you need to whitelist your game in order to receive the email address of the user. To whitelist your game, contact developers@airconsole.com. For development purposes, localhost is always allowed.
Parameters:
Name Type Description
email_address string | undefined The email address of the user if it was set.
Source:

(abstract) onMessage(device_id, data)

Gets called when a message is received from another device that called message() or broadcast(). If you dont want to parse messages yourself and prefer an event driven approach, have a look at http://github.com/AirConsole/airconsole-events/
Parameters:
Name Type Description
device_id number The device ID that sent the message.
data serializable The data that was sent.
Source:

(abstract) onReady(code)

Gets called when the game console is ready. This event also fires onConnect for all devices that already are connected and have loaded your game. This event also fires onCustomDeviceStateChange for all devices that are connected, have loaded your game and have set a custom Device State.
Parameters:
Name Type Description
code string The AirConsole join code.
Source:

openExternalUrl(url)

Opens url in external (default-system) browser. Call this method instead of calling window.open. In-App it will open the system's default browser. Because of Safari iOS you can only use it with the onclick handler:
Open new window
OR in JS with assigning element.onclick.
Parameters:
Name Type Description
url stirng The url to open
Source:

requestEmailAddress()

Requests the email address of this device and calls onEmailAddress iff the request was granted. For privacy reasons, you need to whitelist your game in order to receive the email address of the user. To whitelist your game, contact developers@airconsole.com. For development purposes, localhost is always allowed.
Source:

setActivePlayers(max_players)

Takes all currently connected controllers and assigns them a player number. Can only be called by the screen. You don't have to use this helper function, but this mechanism is very convenient if you want to know which device is the first player, the second player, the third player ... The assigned player numbers always start with 0 and are consecutive. You can hardcode player numbers, but not device_ids. Once the screen has called setActivePlayers you can get the device_id of the first player by calling convertPlayerNumberToDeviceId(0), the device_id of the second player by calling convertPlayerNumberToDeviceId(1), ... You can also convert device_ids to player numbers by calling convertDeviceIdToPlayerNumber(device_id). You can get all device_ids that are active players by calling getActivePlayerDeviceIds(). The screen can call this function every time a game round starts.
Parameters:
Name Type Description
max_players number The maximum number of controllers that should get a player number assigned.
Source:

setCustomDeviceState(data)

Sets the custom DeviceState of this device.
Parameters:
Name Type Description
data Object The custom data to set.
Source:

setCustomDeviceStateProperty(key, value)

Sets a property in the custom DeviceState of this device.
Parameters:
Name Type Description
key String The property name.
value mixed The property value.
Source:

setOrientation(orientation)

Sets the device orientation.
Parameters:
Name Type Description
orientation string AirConsole.ORIENTATION_PORTRAIT or AirConsole.ORIENTATION_LANDSCAPE.
Source:

showAd()

Requests that AirConsole shows a multiscreen advertisment. Can only be called by the AirConsole.SCREEN. onAdShow is called on all connected devices if an advertisement is shown (in this event please mute all sounds). onAdComplete is called on all connected devices when the advertisement is complete or no advertisement was shown.
Source:

showDefaultUI(visible)

Shows or hides the default UI.
Parameters:
Name Type Description
visible boolean Whether to show or hide the default UI.
Source:

Type Definitions

Config

The configuration for the AirConsole constructor.
Type:
  • object
Properties:
Name Type Description
orientation string AirConsole.ORIENTATION_PORTRAIT or AirConsole.ORIENTATION_LANDSCAPE.
synchronize_time boolean | undefined If set to true, you can call getServerTime() to get the time on the game server. Default is false.
setup_document boolean | undefiend Sets up the document so nothing is selectable, zoom is fixed to 1 and scrolling is disabled (iOS 8 clients drop out of fullscreen when scrolling). Default: true
device_motion number | undefined If set, onDeviceMotion gets called every "device_motion" milliseconds with data from the accelerometer and the gyroscope. Only for controllers.
Source:

DeviceState

DeviceState contains information about a device in this session. Use the helper methods getUID, getNickname, getProfilePicture and getCustomDeviceState to access this data.
Type:
  • object
Properties:
Name Type Description
uid string The globally unique ID of the user.
custom string | undefined Custom device data that this API can set.
nickname string | undefined The nickname of the user.
slow_connection boolean | undefined If the user has a high server latency.
Source: