A custom server side handler named TestHandler is mapped to the topic "/hello/test". Submitting the form will publish your name to the server which will respond with a separate message back to the client to the topic "/server/hello". This example will do an immediate fetch for server based published messages following the form publish. Following is the client side code:
function processHello() { // clear the server messages document.getElementById("serverMessage").innerHTML = ""; // this is to test a custom handler on the server listening for messages on the topic "/hello/test" jmaki.publish('/hello/test', { name : document.getElementById("helloField").value} ); // do an immediate fetch of messages from the server // while this is not required (the polling will pick up the messages) it will make a smoother for the UI jmaki.getExtension("jmaki.communicator").getMessages(); }
The server side code looks like:
package test; import org.jmaki.Handler; import org.jmaki.Message; import org.jmaki.PubSub; import org.json.JSONException; import org.json.JSONObject; public class TestHandler implements Handler { public TestHandler() {} public String processRequest(JSONObject jo) { try { System.out.println("Test Handler was invoked with name: " + jo.getString("name")); PubSub.getInstance().publish(new Message("/server/hello", "Hello " + jo.getString("name"))); } catch (JSONException e) { e.printStackTrace(); } return null; } }
A Pojo of the class UserBean is found in the session scope on the server and mapped to listen to messages on "/User/Test1". Submitting the form will update the bean properties on the server and cause a message from the server confirming the update to be published back to the client with the topic "/UserBean/updated" with the server value. This update will rely on the polling of the server to get the latest updates (default polling is 5 seconds). The client side code is as follows:
function processForm() { // clear the server messages document.getElementById("serverMessage").innerHTML = ""; // the server is listening for the topic "/User/test1" and the requrest will be forwarded // to the server via the "jmaki.communicator" extension. jmaki.publish('/User/test1', { name : document.getElementById("nameField").value, phone : document.getElementById("phoneField").value } ); }
The server side bean is:
package test; import org.jmaki.Message; import org.jmaki.PubSub; public class UserBean { protected String name = null; protected String phone = null; public UserBean() {} public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; System.out.println("UserBean: phone has been set to " + phone); } public void setName(String name) { this.name = name; System.out.println("UserBean: name has been set to " + name); PubSub.getInstance().publish(new Message("/UserBean/updated", this)); } public String getName() { return name; } }