I'm new to SFS and JSON and I was trying to make a simple method to return the triples of the values sent on an array but for some reason it's failing (the JSON object). Here my method
Code: Select all
public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom) {
trace("The command -> " + cmd + " was invoked by user -> " + u.getName());
if (cmd.equals("triple")) {
// Main response object
JSONObject response = new JSONObject();
// Set the command name for the response
response.put("_cmd", "triple");
// Cycle through all items
JSONArray arr = jso.getJSONArray("values");
// Nested array object for return
JSONArray returnArray = new JSONArray();
for (int i = 0; i < arr.length(); i++) {
// Trace to server console
System.out.println("Item " + i + " = " + arr.getInt(i));
returnArray.put(3*arr.getInt(i));
}
// Add returnArray to main response object
response.put("values", returnArray);
// Prepare a list of recipients and put the user that requested the command
LinkedList recipients = new LinkedList();
recipients.add(u.getChannel());
// Send data to client
sendResponse(response, fromRoom, u, recipients);
}
}