Class SFS2X.Requests.System.PublicMessageRequest

Sends a public chat message.

Class Summary
Constructor Attributes Constructor Name and Description
 
SFS2X.Requests.System.PublicMessageRequest(message, params, targetRoom)
Creates a new PublicMessageRequest instance.

Class Detail

SFS2X.Requests.System.PublicMessageRequest(message, params, targetRoom)
Creates a new PublicMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be performed.

Using this request a public message is dispatched to all the users in the specified Room, including the message sender (this allows showing chat messages in the correct order in the application interface); the corresponding event is the publicMessage event. It is also possible to send an optional object together with the message: it can contain custom parameters useful to transmit, for example, additional informations related to the message, like the text font or color, or other formatting details.

In case the target Room is not specified, the message is sent in the last Room joined by the sender.

NOTE: the publicMessage event is dispatched if the Room is configured to allow public messaging only (see the RoomSettings.permissions parameter).

The following example sends a public message and handles the respective event:

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.PUBLIC_MESSAGE, onPublicMessage, this);
	
	// Send a public message
	sfs.send(new SFS2X.Requests.System.PublicMessageRequest("Hello everyone!"));
}

function onPublicMessage(evtParams)
{
	// As messages are forwarded to the sender too,
	// I have to check if I am the sender
	
	var sender = evtParams.sender;
	
	if (sender == sfs.mySelf)
		console.log("I said: " + evtParams.message);
	else
		console.log("User " + sender.name + " said: " + evtParams.message);
}
Parameters:
{String} message
The message to be sent to all the users in the target Room.
{Object} params Optional, Default: null
An object containing additional custom parameters to be sent to the message recipients (for example the color of the text, etc).
{SFSRoom} targetRoom Optional, Default: null
The SFSRoom object corresponding to the Room where the message should be dispatched; if null, the last Room joined by the user is used.
See also:
SFS2X.SmartFox#send
SFS2X.SFSEvent.PUBLIC_MESSAGE