{"id":692,"date":"2017-05-23T09:57:31","date_gmt":"2017-05-23T09:57:31","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=692"},"modified":"2017-05-23T10:05:46","modified_gmt":"2017-05-23T10:05:46","slug":"kotlin-and-smartfoxserver-2x","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/kotlin-and-smartfoxserver-2x\/","title":{"rendered":"Kotlin and SmartFoxServer 2X"},"content":{"rendered":"<p>You may have heard of the <a title=\"Kotlin language website\" href=\"https:\/\/kotlinlang.org\/\" target=\"_blank\">Kotlin<\/a> language before, but recently it has seen a big spike in popularity thanks to Google officially adopting it\u00a0for Android development, alongside Java and C++.<\/p>\n<p>Kotlin is an interesting statically typed language, developed by the good folks at <a title=\"Jetbrains website\" href=\"https:\/\/www.jetbrains.com\/\" target=\"_blank\">Jetbrains<\/a>,\u00a0that shares many similarities with the likes of Scala and Swift.\u00a0In a sense, it could be thought of the equivalent of Swift for the Java platform: a more modern, concise and safer language that runs in the JVM, fully interoperable with Java.<\/p>\n<p>If that&#8217;s not enough, Kotlin also aims at compiling for other\u00a0targets beyond the JVM, such as Javascript and native platforms and seeks to support\u00a0asynchronous programming\u00a0via coroutines and non blocking I\/O.<\/p>\n<p>In the context of <strong>using Kotlin with SmartFoxServer<\/strong> nothing would be easier: the integration on both server side and client side is painless and it works out of the box with any SFS2X release.<\/p>\n<p>Let&#8217;s take a closer look, shall we?<!--more--><\/p>\n<h2>\u00bb Server side<\/h2>\n<p>So what does it take exactly to write a server side Extension with Kotlin? The easiest way to do it is via<a title=\"Download IntelliJ \" href=\"https:\/\/www.jetbrains.com\/idea\/download\/#section=mac\" target=\"_blank\"> Jetbrain&#8217;s IntelliJ<\/a>, a commercial multi-language IDE that also comes as a &#8220;Community Edition&#8221;, free of charge.<\/p>\n<p>This is very good news for anyone interested in experimenting with Kotlin; so we downloaded a copy, created a new Kotlin Project and added the basic jar files (sfs2x.jar, sfs2x-core.jar) to it.<\/p>\n<p><a href=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs.png\"><img loading=\"lazy\" class=\" size-full wp-image-693 aligncenter\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs.png\" alt=\"setting libraries\" width=\"1225\" height=\"326\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs.png 1225w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs-300x80.png 300w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs-1024x273.png 1024w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-libs-624x166.png 624w\" sizes=\"(max-width: 1225px) 100vw, 1225px\" \/><\/a><\/p>\n<p>This is done by right-clicking the project name \u00bb Open Module Settings \u00bb Libraries. From there you can click the plus sign (+) at the top of the second column and navigate to your\u00a0jar files folder (i.e. SFS2X\/lib\/).<\/p>\n<p>Nothing new up to this point besides maybe learning how to use IntelliJ, if you&#8217;re not familiar with it. Next comes the interesting part: writing some actual Kotlin code.<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\npackage sfs2x.kt\r\n\r\nimport com.smartfoxserver.v2.core.ISFSEvent\r\nimport com.smartfoxserver.v2.core.SFSEventType\r\nimport com.smartfoxserver.v2.extensions.BaseServerEventHandler\r\nimport com.smartfoxserver.v2.extensions.SFSExtension\r\n\r\nclass EventHandler : BaseServerEventHandler()\r\n{\r\n    override fun handleServerEvent(event: ISFSEvent?)\r\n    {\r\n        trace(&quot;Event received &gt;&gt; &quot; + event?.type)\r\n    }\r\n}\r\n\r\nclass KotlinTestExtension : SFSExtension()\r\n{\r\n    override  fun init()\r\n    {\r\n        trace(&quot;Kotlin Extension running in Zone: &quot; + parentZone.toString())\r\n\r\n        \/\/ Add server event handler\r\n        addEventHandler(SFSEventType.SERVER_READY, EventHandler())\r\n    }\r\n\r\n    override fun destroy()\r\n    {\r\n        super.destroy()\r\n\r\n        trace(&quot;Extension shutting down&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p>At the top we have our standard package declaration, just like in Java. Next a few imports and finally we\u00a0encounter the first new concept: multiple classes in the same file!<br \/>\nKotlin, as many recent languages, doesn&#8217;t force you in the Java&#8217;s one-class-per-file philosophy, so we can put two (or more) package\u00a0level classes in the same source file.<\/p>\n<p>Our main Extension class, <strong>KotlinTestExtension<\/strong>, extends the base <strong>SFSExtension<\/strong> class using the colon (:) notation, like in C#\/C++. Then we proceed with overriding the <strong>init()<\/strong> and <strong>destroy() <\/strong>methods, declared with the Kotlin <strong>fun<\/strong> keyword, short for <em>function<\/em>\u00a0of course.<\/p>\n<p>Notice how the Java\u00a0<strong>getParentZone()<\/strong>\u00a0Extension method becomes <strong>parentZone<\/strong>\u00a0(no method invocation): this is because in Kotlin there are no getters and setters, <a title=\"Kotlin properties\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/properties.html\">but only properties<\/a>. This in turn means that all java beans exposed in SFS2X SDK will auto-complete to their property names, rather than their getter\/setter names.<\/p>\n<p>Also note how instantiating the EventHandler class does not require the &#8220;new&#8221; keyword, akin to languages such as Python.<\/p>\n<p>Another bit worth of notice is\u00a0this line\u00a0from the <strong>EventHandler<\/strong> class:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\ntrace(&quot;Event received &gt;&gt; &quot; + event?.type)\r\n<\/pre>\n<p>If you&#8217;re familiar with languages such as Groovy or Swift you&#8217;ll recognize it as the <a title=\"Null safe operator in Kotlin\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html\">null safe operator<\/a>, which is exactly what this is. A useful compile-time help to avoid those nasty null pointer exceptions.<\/p>\n<p>Finally we can generate our jar file\u00a0and deploy it to SmartFoxServer. \u00a0To do so, let&#8217;s open the Module settings again via right-clicking the project name \u00bb Open Module Settings \u00bb Artifacts.<\/p>\n<p><a href=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-ext-deploy.png\"><img loading=\"lazy\" class=\" size-full wp-image-701 aligncenter\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-ext-deploy.png\" alt=\"kot-ext-deploy\" width=\"1022\" height=\"272\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-ext-deploy.png 1022w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-ext-deploy-300x80.png 300w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/05\/kot-ext-deploy-624x166.png 624w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/a><\/p>\n<p>Here we can choose the name and path where we want to deploy the jar file and also include\/exclude specific libraries packaged with\u00a0the jar. This is important because by default IntelliJ will include the two SFS2X libraries we&#8217;ve added as part of the final jar.<\/p>\n<p><strong>We don&#8217;t want this<\/strong>,<strong>\u00a0<\/strong>because those libraries are already part of the SFS2X runtime and we just use them as dependencies but\u00a0we don&#8217;t need them included in our final jar. Make sure to select and remove sfs2x.jar and sfs2x-core.jar from the list, apply the changes and finally we\u00a0can hit the <strong>Build \u00bb Build artifacts<\/strong> option to deploy the code.<\/p>\n<p>Finally we can open the SFS2X AdminTool, navigate to the Zone Configurator, add the Extension to our Zone of choice and voil\u00e0.<\/p>\n<h2>\u00bb Next steps and resources<\/h2>\n<p>To learn more about Kotlin and its capabilities we highly recommend several interesting resources:<\/p>\n<ul>\n<li><a title=\"Google I\/O 17 Intro to Kotlin\" href=\"https:\/\/www.youtube.com\/watch?v=X1RVYt2QKQE\" target=\"_blank\">Google I\/O &#8217;17 Introduction to Kotlin<\/a> (YT video)<\/li>\n<li><a title=\"Official Kotlin website\" href=\"https:\/\/kotlinlang.org\" target=\"_blank\">Official Kotlin language website<\/a><\/li>\n<li><a title=\"Try Kotlin online\" href=\"https:\/\/try.kotlinlang.org\/#\/Examples\/Hello,%20world!\/Simplest%20version\/Simplest%20version.kt\" target=\"_blank\">Try Kotlin online<\/a>\u00a0(try Kotlin without installing anything)<\/li>\n<li><a title=\"Kotlin reference\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/\" target=\"_blank\">The Kotlin reference<\/a> (also available as downloadable PDF)<\/li>\n<\/ul>\n<p>In the next installment we will take a look at using Kotlin on the client side with SFS2X.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may have heard of the Kotlin language before, but recently it has seen a big spike in popularity thanks to Google officially adopting it\u00a0for Android development, alongside Java and C++. Kotlin is an interesting statically typed language, developed by the good folks at Jetbrains,\u00a0that shares many similarities with the likes of Scala and Swift.\u00a0In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/692"}],"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=692"}],"version-history":[{"count":19,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/692\/revisions"}],"predecessor-version":[{"id":713,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/692\/revisions\/713"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}