If everything is OK, you now have your jwebsocket up and running as soon as your webapp is started
Create your first C2S (client to server) RPC
Let's make the simplest RPC call : we want the server to return the time.
In your server folder, create a new class named RpcTime wich extends BaseRPCCallable, and create a getTime() method, such as:
package testpanel.server.jwebsocket.rpc; import org.jwebsocket.plugins.rpc.BaseRPCCallable; public class RpcTime extends BaseRPCCallable { public static long getTime() { return System.currentTimeMillis(); } }
that's all !
BaseRPCCallable is the Abstract class for rpc class. It provides advanced method used by the websocket during initialization.
If needed, you can also use a BaseConnectorRPCCallable or implement your own class using the RPCCallable Interface.
Get more information about this classes on part 4
Adding the rights to the users.
Jwebsocket works with Roles and Right.
Every user has a Role, and every Role has Rights.
Calling a rpc method is a right, so you'll have to grant access to your getTime method.
Open the jWebsocket.xml file in your /conf folder and add the following right in the <rights></rights> section:
<right> <ns>org.jwebsocket.plugins.rpc</ns> <id>testpanel.server.jwebsocket.rpc.RpcTime</id> <description>A sample Rpc demo: get the server Time</description> </right>
Now you have to add the right to the roles:
In the <roles></roles>tag, add the following right (in the <rights>tag) for the role you want:
<right>org.jwebsocket.plugins.rpc.testpanel.server.jwebsocket.rpc.RpcTime</right>
So now your xml file looks like, for instance:
<rights> [.... All other rights....] <!-- configuration of rpc plug-in specific rights --> <right> <ns>org.jwebsocket.plugins.rpc</ns> <id>rpc</id> <description>Allow Remote Procedure Calls (RPC) to server</description> </right> <right> <ns>org.jwebsocket.plugins.rpc</ns> <id>rrpc</id> <description>Allow Reverse Remote Procedure Calls (RRPC) to other clients</description> </right> <!-- list all granted methods for RPC --> <right> <ns>org.jwebsocket.plugins.rpc</ns> <id>testpanel.server.jwebsocket.rpc.RpcTime</id> <description>A sample Rpc demo: get the server Time</description> </right> </rights> <roles> <role> <id>Guest</id> <description>Anonymous users (not registered)</description> <rights> <!-- list of rights for "Guest" role (ns + . + id) --> <right>org.jwebsocket.plugins.system.broadcast</right> <right>org.jwebsocket.plugins.system.send</right> <right>org.jwebsocket.plugins.chat.broadcast</right> <right>org.jwebsocket.plugins.rpc.rpc</right> <right>org.jwebsocket.plugins.rpc.rrpc</right> <right>org.jwebsocket.plugins.rpc.org.jwebsocket.rpc.sample.SampleRPCLibrary.sampleOverloadRPC(int)</right> </rights> </role> [.... All other roles....] </roles>
(you will find more informations about the org.jwebsocket.plugins.rpc.rpc and org.jwebsocket.plugins.rpc.rrpc right in the part4.
Calling the method from the client.
Now, we just have to call the Rpc method from the client:
lWSC = new jws.jWebSocketJSONClient(); var getTimeFromServer = function () { var lRes = lWSC.rpc( "testpanel.server.jwebsocket.rpc.RpcTime", "getTime", null, //parameter of the called method, here null because we don't have any arguments { spawnThread: false, OnResponse: function( aToken ) { if( aToken.code != 0 ) { alert( "RPC Error: " + aToken.msg ); } else { var lRes = aToken.result; alert( "server time: " + lRes ); } } } ); } var lRes = lWSC.open( jws.JWS_SERVER_URL;); getTimeFromServer ();
Done :)