{"id":1345,"date":"2020-04-29T08:02:50","date_gmt":"2020-04-29T08:02:50","guid":{"rendered":"https:\/\/smartfoxserver.com\/blog\/?p=1345"},"modified":"2020-04-29T08:03:26","modified_gmt":"2020-04-29T08:03:26","slug":"sfs2x-and-websocket-for-html5-and-unity","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/sfs2x-and-websocket-for-html5-and-unity\/","title":{"rendered":"SFS2X and Websocket for HTML5 and Unity."},"content":{"rendered":"\n<p>Websocket has become very popular thanks to the ubiquity of HTML5 technologies on desktop, mobile and even consoles. Additionally game engines such as Unity, with its WebGL build, has pushed the boundaries of what can be achieved in web-based games allowing many developers to release their titles for the browser.<\/p>\n\n\n\n<p>In this new article we&#8217;re going to review a series of steps to set up Websocket in SmartFoxServer 2X and avoid potential pitfalls, especially when testing locally.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3>\u00bb The basics<\/h3>\n\n\n\n<p>SmartFoxServer 2X provides a custom socket protocol (TCP and UDP) that works with any native application (e.g. iOS, Android, Windows, PS4 etc&#8230;), via the relative client API, with the exception of browser-based games.<\/p>\n\n\n\n<p>This is because browsers don&#8217;t allow custom TCP\/UDP socket communication for security and compatibility reasons. Instead, the new <a rel=\"noreferrer noopener\" aria-label=\"Websocket protocol (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/WebSocket\" target=\"_blank\">Websocket protocol<\/a> has been introduced in browsers as part of the HTML5 standard. It can coexist with the HTTP protocol on the same ports so that traffic is not blocked by firewalls, proxies and the like.<\/p>\n\n\n\n<p>Websocket is supported by SmartFoxServer 2X via the <a rel=\"noreferrer noopener\" aria-label=\"Javascript API (opens in a new tab)\" href=\"https:\/\/www.smartfoxserver.com\/download#p=client\" target=\"_blank\">Javascript API<\/a> for HTML5 clients or the <a rel=\"noreferrer noopener\" aria-label=\"Unity\/WebGL API (opens in a new tab)\" href=\"https:\/\/www.smartfoxserver.com\/download#p=client\" target=\"_blank\">Unity\/WebGL API<\/a>, for WebGL builds.<\/p>\n\n\n\n<p><strong>NOTE<\/strong>: In this article we will often refer to Unity WebGL as this is the build name used in Unity for browser-based exports. Unity WebGL uses Websocket as the network technology for multiplayer communication.<\/p>\n\n\n\n<p>Activating Websocket in SmartFoxServer 2X is requires only a few steps:<\/p>\n\n\n\n<ul><li>Access the SFS2X AdminTool<\/li><li>Open the Server Configurator<\/li><li>Activate the <em>Enable WS\/WSS <\/em>option<\/li><li>Submit and restart SFS2X<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"625\" src=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-1024x625.png\" alt=\"\" class=\"wp-image-1348\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-1024x625.png 1024w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-300x183.png 300w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-768x468.png 768w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-1536x937.png 1536w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02-624x381.png 624w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-27-alle-16.55.02.png 1636w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3>\u00bb Testing locally<\/h3>\n\n\n\n<p>Now that Websocket is active we can try to connect with a client to the local server and start experimenting. To get going quickly you can download some of our examples, for instance the <a rel=\"noreferrer noopener\" aria-label=\"HTML5 Example pack (opens in a new tab)\" href=\"https:\/\/www.smartfoxserver.com\/download#p=examples\" target=\"_blank\">HTML5 Example pack<\/a> contains a wide series of tutorials with different levels of complexity (<a rel=\"noreferrer noopener\" aria-label=\"with relative online tutorials (opens in a new tab)\" href=\"http:\/\/docs2x.smartfoxserver.com\/ExamplesJS\/introduction\" target=\"_blank\">with relative online tutorials<\/a>).<\/p>\n\n\n\n<p>Every example is already configured to connect to the <em>localhost<\/em> on TCP port 8080, so no set up is required. You can double-click or drag and drop the html file in your browser and you&#8217;re good to go.<\/p>\n\n\n\n<p>However for <a rel=\"noreferrer noopener\" aria-label=\"Unity-based examples (opens in a new tab)\" href=\"https:\/\/www.smartfoxserver.com\/download#p=examples\" target=\"_blank\">Unity-based examples<\/a> you will need one extra step. After building for WebGL, if you drag and drop one of the examples in your favorite browser a warning like this will show up:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"191\" src=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-1024x191.png\" alt=\"\" class=\"wp-image-1352\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-1024x191.png 1024w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-300x56.png 300w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-768x144.png 768w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-1536x287.png 1536w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18-624x117.png 624w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-28-alle-13.09.18.png 1658w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>What this means is that Unity WebGL examples need to be served from a web-server (for security reasons), so in order to fulfill this requirement you will need to move the exported folder under <em>SFS2X\/www\/ROOT\/ExampleName<\/em><\/p>\n\n\n\n<p>Once this is done you can safely point the browser to <em>http:\/\/localhost:8080\/ExampleName<\/em> where <em>ExampleName<\/em> is replaced by the actual name of the exported WebGL example. <\/p>\n\n\n\n<h3>\u00bb Secure Websocket (WSS)<\/h3>\n\n\n\n<p>The Websocket protocol comes with a more secure, encrypted variant which is the equivalent of HTTPS, called in short <strong>WSS<\/strong>. This is activated by deploying a valid SSL certificate in SmartFoxServer 2X, which enables both HTTPS and WSS over the chosen TCP port (more on this in the next sections).<\/p>\n\n\n\n<h4>Testing WSS locally<\/h4>\n\n\n\n<p>Local testing with WSS (and HTTPS) is very tricky and usually unrecommended , although there may be some <a rel=\"noreferrer noopener\" aria-label=\"solutions (opens in a new tab)\" href=\"https:\/\/letsencrypt.org\/docs\/certificates-for-localhost\/\" target=\"_blank\">workarounds<\/a>. The main reason for this is that  SSL certificates can only be issued for internet domains, and &#8220;localhost&#8221; is not one of those. Our suggestion in this case is to test locally by setting the client to standard Websocket and switching it to WSS when in production.<\/p>\n\n\n\n<p>This is an example of how to switch the client between the two modes (WS and WSS) in Javascript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(function () {\n    \/\/ Use a flag to easily switch cryptography on and off\n    var useEncryption = true;\n     \n    \/\/ Create configuration object\n    var config = {};\n    config.host = \"mydomain.com\";\n    config.port = useEncryption ? 8443 : 8080;\n    config.useSSL = useEncryption;\n    config.zone = \"BasicExamples\";\n     \n    \/\/ Create the SmartFox client instance\n    var sfs = new SFS2X.SmartFox(config);\n     \n    \/\/ Add connection event listener\n    sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);\n     \n    \/\/ Connect to SmartFoxServer 2X\n    sfs.connect();\n     \n    \/\/ Handle connection event\n    function onConnection(evtParams)\n    {\n        console.log(evtParams.success);\n    }\n})();<\/code><\/pre>\n\n\n\n<p>And this is the same example for Unity\/WebGL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ConnectionExample : MonoBehaviour\n{\n    private SmartFox sfs;\n     \n    \/\/ Use a flag to easily switch cryptography on and off\n    private bool useEncryption = true;\n     \n    void Start()\n    {\n        \/\/ Create the SmartFox client instance\n        sfs = new SmartFox(useEncryption ? UseWebSocket.WSS : UseWebSocket.WS);\n         \n        \/\/ Add connection event listener\n        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);\n         \n        \/\/ Create configuration object\n        ConfigData config = new ConfigData();\n        config.host = \"mydomain.com\";\n        config.port = useEncryption ? 8443 : 8080;\n        config.zone = \"BasicExamples\";\n         \n        \/\/ Connect\n        sfs.Connect(config);\n    }\n     \n    \/\/ Handle connection event\n    private void OnConnection(BaseEvent evt)\n    {\n        Debug.Log(evt.Params&#91;\"success\"]);\n    }\n}<\/code><\/pre>\n\n\n\n<h4>Testing remotely<\/h4>\n\n\n\n<p>Before you can use WSS you will need to deploy a <strong>valid SSL certificate<\/strong> in SmartFoxServer 2X. This operations requires a number of steps:<\/p>\n\n\n\n<ul><li>Acquiring an internet domain (if you don&#8217;t already have one)<\/li><li>Acquiring an SSL certificate for such domain<\/li><li>Deploying the certificate on the SFS2X instance running on your domain<\/li><\/ul>\n\n\n\n<p>We describe this process step by step in a <a rel=\"noreferrer noopener\" aria-label=\"detailed guide (opens in a new tab)\" href=\"http:\/\/docs2x.smartfoxserver.com\/GettingStarted\/cryptography\" target=\"_blank\">detailed guide<\/a> in our documentation.<\/p>\n\n\n\n<p>Once you have completed the setup you will be able to connect your client to the game server via a secure Websocket connection.<\/p>\n\n\n\n<h3>\u00bb Unity WebGL notes<\/h3>\n\n\n\n<p>Finally  a couple of notes for Unity developers getting started with WebGL and SFS2X. When starting a new Unity project for the browser you will need to <strong>add multiple DLLs<\/strong> from our Unity API distribution to support running the code in the Unity Editor and building for WebGL.<\/p>\n\n\n\n<p>Before getting started make sure to <a href=\"http:\/\/docs2x.smartfoxserver.com\/GettingStarted\/client-api-csharp\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"follow our documentation (opens in a new tab)\">follow our documentation<\/a> to set up your environment correctly.<\/p>\n\n\n\n<h3>\u00bb Further discussions<\/h3>\n\n\n\n<p>If you have any more questions or doubts make sure to join the conversation in our <a rel=\"noreferrer noopener\" aria-label=\"support forums (opens in a new tab)\" href=\"https:\/\/www.smartfoxserver.com\/forums\/index.php\" target=\"_blank\">support forums<\/a> and let us know your feedback.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Websocket has become very popular thanks to the ubiquity of HTML5 technologies on desktop, mobile and even consoles. Additionally game engines such as Unity, with its WebGL build, has pushed the boundaries of what can be achieved in web-based games allowing many developers to release their titles for the browser. In this new article we&#8217;re [&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":[122,120,42,119,118,121],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1345"}],"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=1345"}],"version-history":[{"count":20,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1399,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions\/1399"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}