new ObjectMessageRequest(object[, targetRoom][, recipients])

Creates a new ObjectMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be executed.

This request sends an object containing custom data to the users in a Room. The object is delivered to the selected users (or all users excluding the sender) inside the target Room by means of the objectMessage event. It can be useful to send game data, like for example the target coordinates of the user's avatar in a virtual world.

Example

This example sends the player's avatar movement coordinates and handles the respective event (note: the "myAvatar" instance is supposed to be the user sprite on the stage, while the "getUserAvatar()" method retrieves the sprite of other users' characters).

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.OBJECT_MESSAGE, onObjectMessage, this);

	// Send my movement to all players
	var dataObj = new SFS2X.SFSObject();
	dataObj.putInt("x", myAvatar.x);
	dataObj.putInt("y", myAvatar.y);

	sfs.send(new SFS2X.ObjectMessageRequest(dataObj));
}

function onObjectMessage(evtParams)
{
	var dataObj = evtParams.message;

	var sender = evtParams.sender;
	var avatar = getUserAvatar(sender.id);

	avatar.x = dataObj.get("x");
	avatar.y = dataObj.get("y");
}

Parameters

Name Type Optional Description

object

 

 

A SFSObject containing custom parameters to be sent to the message recipients.

targetRoom

 

Yes

The SFSRoom object corresponding to the Room where the message should be dispatched; if null, the last Room joined by the user is used.

Defaults to null.

recipients

 

Yes

A list of SFSUser objects corresponding to the message recipients; if null, the message is sent to all users in the target Room (except the sender himself).

Defaults to null.

See also
SmartFox#send
SFSEvent.OBJECT_MESSAGE