headless_server.adoc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. = jME3 Headless Server
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: server, spidermonkey, headless, network, documentation
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. 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's an easier way.
  10. It's very easy to change your current (client) game to function as a server as well.
  11. == What Does Headless Mean?
  12. A headless server…
  13. * does not display any output – no window opens, no audio plays, no graphics are rendered.
  14. * ignores all input – no input handling.
  15. * keeps game state – you can attach to, transform, and save the rootNode, although the scene is not displayed.
  16. * calls the `simpleUpdate()` loop – you can run tests and trigger events as usual.
  17. == Client Code
  18. First, let's take a look at the default way of creating a new game (in its simplest form):
  19. [source,java]
  20. ----
  21. public static void main(String[] args) {
  22. Application app = new Main();
  23. app.start();
  24. }
  25. ----
  26. == Headless Server Code
  27. 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's a good thing for a server.
  28. [source,java]
  29. ----
  30. import com.jme3.system.JmeContext;
  31. import com.jme3.system.JmeContext.Type;
  32. public static void main(String[] args) {
  33. Application app = new Main();
  34. app.start(JmeContext.Type.Headless);
  35. }
  36. ----
  37. == Next steps
  38. Okay, so you can now start your game in a headless 'server mode', where to go from here?
  39. * Parse `String[] args` from the `main`-method to enable server mode on demand (e.g. start your server like `java -jar mygame.jar –server`.
  40. * Integrate <<jme3/advanced/networking#,SpiderMonkey>>, to provide game updates to the server over a network.
  41. * Only execute code that's needed. (E.g. place all rendering code inside an `if (servermode)`-block) (or `if (!servermode)` for the client).
  42. * Add decent <<jme3/advanced/logging#,logging>> so your server actually makes sense.