{"id":122,"date":"2014-12-31T10:40:21","date_gmt":"2014-12-31T10:40:21","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=122"},"modified":"2015-01-08T11:22:31","modified_gmt":"2015-01-08T11:22:31","slug":"autojoiner-extension","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/autojoiner-extension\/","title":{"rendered":"AutoJoiner Extension"},"content":{"rendered":"<p>In this recipe we will see how we can create a simple Extension that automatically joins\u00a0users in an available Room and creates more Rooms when the seats are all occupied.<!--more--><\/p>\n<p>Detecting which Room has currently available seats from client side is not always accurate\u00a0because by the time a client chooses to join a certain Room other users might have joined concurrently.<\/p>\n<p>To avoid errors and unexpected join failures we can move the joining logic on the server side. Our Extension will scan through the available Rooms, find one that has one or more free spots and join the client. If all Rooms are occupied the Extension will create a new Room and join the user subsequently.<\/p>\n<p>Here is the code of the AutoJoiner extension:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.List;\r\nimport java.util.Random;\r\nimport java.util.concurrent.atomic.AtomicInteger;\r\n\r\nimport com.smartfoxserver.v2.api.CreateRoomSettings;\r\nimport com.smartfoxserver.v2.entities.Room;\r\nimport com.smartfoxserver.v2.entities.User;\r\nimport com.smartfoxserver.v2.entities.data.ISFSObject;\r\nimport com.smartfoxserver.v2.entities.data.SFSObject;\r\nimport com.smartfoxserver.v2.exceptions.SFSCreateRoomException;\r\nimport com.smartfoxserver.v2.exceptions.SFSException;\r\nimport com.smartfoxserver.v2.exceptions.SFSJoinRoomException;\r\nimport com.smartfoxserver.v2.extensions.ExtensionLogLevel;\r\nimport com.smartfoxserver.v2.extensions.SFSExtension;\r\n\r\npublic class AutoJoinerExtension extends SFSExtension\r\n{\r\n\tprivate final String version = &quot;1.0.1&quot;;\r\n\r\n\tprivate final AtomicInteger roomId = new AtomicInteger();\r\n\r\n\t@Override\r\n\tpublic void init()\r\n\t{\r\n\t\ttrace(&quot;Java AutoJoiner: &quot; + version);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void handleClientRequest(String cmd, User user, ISFSObject params)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tif (cmd.equals(&quot;joinMe&quot;))\r\n\t\t\t\tjoinUser(user);\r\n\t\t}\r\n\t\tcatch(Exception err)\r\n\t\t{\r\n\t\t\ttrace(ExtensionLogLevel.ERROR, err.toString());\r\n\t\t}\r\n\t}\r\n\r\n\tprivate void joinUser(User user) throws SFSException\r\n    {\r\n\t    List&lt;Room&gt; rList = getParentZone().getRoomList();\r\n\t    Room theRoom = null;\r\n\r\n\t    for (Room room : rList)\r\n\t    {\r\n\t    \tif (room.isFull())\r\n\t    \t\tcontinue;\r\n\t    \telse\r\n\t    \t{\r\n\t    \t\ttheRoom = room;\r\n\t    \t\tbreak;\r\n\t    \t}\r\n\t    }\r\n\r\n\t    if (theRoom == null)\r\n\t    \ttheRoom = makeNewRoom(user);\r\n\r\n\t    try\r\n\t    {\r\n\t    \tgetApi().joinRoom(user, theRoom);\r\n\t    }\r\n\t    catch (SFSJoinRoomException e)\r\n\t    {\r\n\t    \ttrace(ExtensionLogLevel.ERROR, e.toString());\r\n\t\t}\r\n    }\r\n\r\n\tprivate Room makeNewRoom(User owner) throws SFSCreateRoomException\r\n    {\r\n\t\tRoom room = null;\r\n\r\n\t\tCreateRoomSettings rs = new CreateRoomSettings();\r\n\t\trs.setGame(false);\r\n\t\trs.setDynamic(true);\r\n\t\trs.setName(&quot;ChatRoom_&quot; + roomId.getAndIncrement());\r\n\t\trs.setMaxUsers(50);\r\n\r\n\t\troom = getApi().createRoom(getParentZone(), rs, owner);\r\n\t\treturn room;\r\n    }\r\n}\r\n<\/pre>\n<p>The code will search through all available Rooms for a free slot and in case none is found it will create a new Room with 50 free slots.<\/p>\n<p>From client side all we need to do is sending a &#8220;joinMe&#8221; Extension request, after having logged in. Like this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ Tell the server to join me...\r\nsfs.send(new ExtensionRequest(&quot;joinMe&quot;));\r\n<\/pre>\n<p>where sfs is the client API instance.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this recipe we will see how we can create a simple Extension that automatically joins\u00a0users in an available Room and creates more Rooms when the seats are all occupied.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[31,7],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":2,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}