headless_server.html 5.8 KB

1234567891011121314151617181920212223242526
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><meta name="keywords" content="server, spidermonkey, headless, network, documentation"><title>jME3 Headless Server</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/advanced/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/headless_server.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>jME3 Headless Server</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#what-does-headless-mean">What Does Headless Mean?</a></li><li><a href="#client-code">Client Code</a></li><li><a href="#headless-server-code">Headless Server Code</a></li><li><a href="#next-steps">Next steps</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>When adding multiplayer to your game, you may find that your server needs to know about game state (e.g. where are players, objects? Was that a direct hit? etc.) You can code all this up yourself, but there&#8217;s an easier way.</p></div>
  4. <div class="paragraph"><p>It&#8217;s very easy to change your current (client) game to function as a server as well.</p></div></div></div>
  5. <div class="sect1"><h2 id="what-does-headless-mean">What Does Headless Mean?</h2><div class="sectionbody"><div class="paragraph"><p>A headless server…</p></div>
  6. <div class="ulist"><ul><li><p>does not display any output – no window opens, no audio plays, no graphics are rendered.</p></li><li><p>ignores all input – no input handling.</p></li><li><p>keeps game state – you can attach to, transform, and save the rootNode, although the scene is not displayed.</p></li><li><p>calls the <code>simpleUpdate()</code> loop – you can run tests and trigger events as usual.</p></li></ul></div></div></div>
  7. <div class="sect1"><h2 id="client-code">Client Code</h2><div class="sectionbody"><div class="paragraph"><p>First, let&#8217;s take a look at the default way of creating a new game (in its simplest form):</p></div>
  8. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="directive">static</span> <span class="type">void</span> main(<span class="predefined-type">String</span><span class="type">[]</span> args) {
  9. Application app = <span class="keyword">new</span> Main();
  10. app.start();
  11. }</code></pre></div></div></div></div>
  12. <div class="sect1"><h2 id="headless-server-code">Headless Server Code</h2><div class="sectionbody"><div class="paragraph"><p>Now, with a simple change you can start your game in Headless mode. This means that all input and audio/visual output will be ignored. That&#8217;s a good thing for a server.</p></div>
  13. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">import</span> <span class="include">com.jme3.system.JmeContext</span>;
  14. <span class="keyword">import</span> <span class="include">com.jme3.system.JmeContext.Type</span>;
  15. <span class="directive">public</span> <span class="directive">static</span> <span class="type">void</span> main(<span class="predefined-type">String</span><span class="type">[]</span> args) {
  16. Application app = <span class="keyword">new</span> Main();
  17. app.start(JmeContext.Type.Headless);
  18. }</code></pre></div></div></div></div>
  19. <div class="sect1"><h2 id="next-steps">Next steps</h2><div class="sectionbody"><div class="paragraph"><p>Okay, so you can now start your game in a headless 'server mode', where to go from here?</p></div>
  20. <div class="ulist"><ul><li><p>Parse <code>String[] args</code> from the <code>main</code>-method to enable server mode on demand (e.g. start your server like <code>java -jar mygame.jar –server</code>.</p></li><li><p>Integrate <a href="../../jme3/advanced/networking.html">SpiderMonkey</a>, to provide game updates to the server over a network.</p></li><li><p>Only execute code that&#8217;s needed. (E.g. place all rendering code inside an <code>if (servermode)</code>-block) (or <code>if (!servermode)</code> for the client).</p></li><li><p>Add decent <a href="../../jme3/advanced/logging.html">logging</a> so your server actually makes sense.</p></li></ul></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  21. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  22. indexName: 'jmonkeyengine',
  23. inputSelector: '#doc-search',
  24. debug: false // Set debug to true if you want to inspect the dropdown
  25. });</script></body></html>