6.x Java Extensions CookBook

This document provides quick snippets of code organized by theme that will get you started with most of the server side coding tasks. Feel free to suggest more "recipes" by sending us an email or posting in our support forums

» sendResponse() Recipes

The following recipes present various use cases of sendResponse() calls sending a message to a single user, group of users, an entire room, a group of rooms and an entire zone. The method takes a list of recipients that can be assembled in many ways, as it is demonstrated in the code below.

NOTE:
The List of recipients passed in the sendResponse() call is expected to contain SocketChannel objects, which represent the User session/connection. You can obtain the session from a User object by invoking its getChannel() method.

Recipe #1:

This is the simplest possible sendResponse() invocation, where we send a message to a single recipient (the user who sent the original request). The example performs a simple arithmetic operation (a sum) on two numbers (x,y) sent by the client.

ExtensionHelper helper;
Zone currentZone;

public void init()
{
	helper = ExtensionHelper.instance();
	currentZone = helper.getZone(this.getOwnerZone);
}

public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
	if (cmd.equals("sum"))
	{
		int x = asObj.getInt("x");
		int y = asObj.getInt("y");
		
		ActionscriptObject response = new ActionscriptObject();
		response.put("_cmd", "sum");
		response.put("sum", x + y);
		
		// Prepare the list of recipients, in this case we only one.
		LinkedList<SocketChannel> recipientList = new LinkedList<SocketChannel>();
		recipientList.add(user.getChannel());

		// Send response
		sendResponse(response, -1, null, recipientList);
	}
}	

Recipe #2:

In this recipe we show how to send a response to a selection of users: in this case we select all users in the current Room and send a message only to those with certain user variables.

ExtensionHelper helper;
Zone currentZone;

public void init()
{
	helper = ExtensionHelper.instance();
	currentZone = helper.getZone(this.getOwnerZone);
}

public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
	if (cmd.equals("sum"))
	{
		int x = asObj.getInt("x");
		int y = asObj.getInt("y");
		
		ActionscriptObject response = new ActionscriptObject();
		response.put("_cmd", "sum");
		response.put("sum", x + y);
		
		// Select recipients
		Room currentRoom = currentZone.getRoom(fromRoom);
		LinkedList<SocketChannel> recipientList = new LinkedList<SocketChannel>();
		
		for (User u : currentRoom.getAllUsers())
		{
			if (u.getVariable("status").getValue().equals("ok"))
				recipientList.add(u.getChannel());
		}
		
		// Send response
		sendResponse(response, -1, null, recipientList);
	}
}	

Recipe #3:

In this recipe we show how to send a message to all users in a room with the exception of the sender.

ExtensionHelper helper;
Zone currentZone;

public void init()
{
	helper = ExtensionHelper.instance();
	currentZone = helper.getZone(this.getOwnerZone);
}

public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
	if (cmd.equals("sum"))
	{
		int x = asObj.getInt("x");
		int y = asObj.getInt("y");
		
		ActionscriptObject response = new ActionscriptObject();
		response.put("_cmd", "sum");
		response.put("sum", x + y);
		
		// Select recipients
		Room currentRoom = currentZone.getRoom(fromRoom);
		LinkedList<SocketChannel> recipientList = new LinkedList<SocketChannel>();
		
		Room currentRoom = currentZone.getRoom(fromRoom);
		for (User u : currentRoom.getAllUsersButOne(user))
		{
			recipientList.add(u.getChannel())
		}
		
		// Send response
		sendResponse(response, -1, null, recipientList);
	}
}	

Recipe #4:

The following example shows how to select the recipients from a group of Rooms. We send the message to all users in any Room whose name starts with the word "game"

ExtensionHelper helper;
Zone currentZone;

public void init()
{
	helper = ExtensionHelper.instance();
	currentZone = helper.getZone(this.getOwnerZone);
}

public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
	if (cmd.equals("sum"))
	{
		int x = asObj.getInt("x");
		int y = asObj.getInt("y");
		
		ActionscriptObject response = new ActionscriptObject();
		response.put("_cmd", "sum");
		response.put("sum", x + y);
		
		// Select recipients
		Room currentRoom = currentZone.getRoom(fromRoom);
		LinkedList<SocketChannel> recipientList = new LinkedList<SocketChannel>();
		
		for (Room room : currentZone.getRoomList())
		{
			if (room.getName().startsWith("game"))
			{
				for (User u : room.getAllUsers())
				{
					recipientList.add(u.getChannel());
				}
			}
		}
		
		// Send response
		sendResponse(response, -1, null, recipientList);
	}
}	

Recipe #5:

The following example shows how to send a message to all users in the Zone.

ExtensionHelper helper;
Zone currentZone;

public void init()
{
	helper = ExtensionHelper.instance();
	currentZone = helper.getZone(this.getOwnerZone);
}

public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
	if (cmd.equals("sum"))
	{
		int x = asObj.getInt("x");
		int y = asObj.getInt("y");
		
		ActionscriptObject response = new ActionscriptObject();
		response.put("_cmd", "sum");
		response.put("sum", x + y);
		
		// Select recipients
		Room currentRoom = currentZone.getRoom(fromRoom);
		
		// Send response: getAllUsersInZone() returns all the SocketChannels for the current zone
		sendResponse(response, -1, null, currentZone.getAllUsersInZone());
	}
}	

 


doc index