streaming.html 5.6 KB

1234567891011121314151617181920212223242526272829303132333435
  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"><title>Streaming service</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"></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/spidermonkey/tutorial/streaming.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/spidermonkey/tutorial/"><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>Streaming service</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></div></div><div id="content"><div class="admonitionblock warning"><table><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content"><div class="paragraph"><p>This article covers a deprecated <abbr title="Application Programming Interface">API</abbr>! See <a href="../../jme3/advanced/networking.html">networking</a> for current documentation.</p></div></td></tr></table></div>
  4. <div class="paragraph"><p>The streaming service is meant for situations where you want to transfer files, or other types of data to clients. In this tutorial we&#8217;ll discuss how it works, and how to use it.</p></div>
  5. <div class="paragraph"><p>Let&#8217;s start off with how it works; streaming service uses messages to transfer data. This is done so it doesn&#8217;t block other messages from being sent, while transferring. First a message is sent the describes the stream. The peer can now choose whether to accept or reject the stream. When the peer accepts, the data will be sent. You have to handle this data yourself. At the end of the stream you get the same message as when the stream was offered, to indicate the end of the stream.</p></div>
  6. <div class="paragraph"><p>Let&#8217;s transfer a file to a client:</p></div>
  7. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">StreamingService sService = client.getService(StreamingService.class);
  8. sService.addStreamListener(<span class="local-variable">this</span>);
  9. sService = server.getService(StreamingService.class);
  10. Client receiver = server.getConnectors().get(<span class="integer">0</span>); <span class="comment">// Note that you can't use 'client' here, since it's not a connector.</span>
  11. sService.offerStream(receiver, <span class="keyword">new</span> StreamMessage(), <span class="keyword">new</span> <span class="predefined-type">FileInputStream</span>(<span class="string"><span class="delimiter">&quot;</span><span class="content">test.txt</span><span class="delimiter">&quot;</span></span>)); <span class="comment">// StreamMessage used here as start and end message, but can be anything to describe the stream on the other end.</span></code></pre></div></div>
  12. <div class="paragraph"><p>As you can see, this system uses the Service system. First, we get the client&#8217;s StreamingService, and register ourselves as a listener. Then we get the server&#8217;s version of the StreamingService, from which you can stream things. Then we get a connector client (the first one), and send the file via an InputStream.</p></div>
  13. <div class="paragraph"><p>Now to receive this stuff is simple;</p></div>
  14. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">boolean</span> streamOffered(StreamMessage message) {
  15. <span class="comment">// Here you'd normally check the message what this stream is all about, and</span>
  16. <span class="comment">// base your acception criteria on that.</span>
  17. fileStream = <span class="keyword">new</span> <span class="predefined-type">FileOutputStream</span>(<span class="string"><span class="delimiter">&quot;</span><span class="content">test.txt</span><span class="delimiter">&quot;</span></span>);
  18. <span class="keyword">return</span> <span class="predefined-constant">true</span>; <span class="comment">// Sure, we'll just accept this message.</span>
  19. }
  20. <span class="directive">public</span> <span class="type">void</span> streamDataReceived(StreamDataMessage message) {
  21. fileStream.write(message.getData());
  22. }
  23. <span class="directive">public</span> <span class="type">void</span> streamCompleted(StreamMessage message) {
  24. fileStream.flush();
  25. fileStream.close();
  26. }</code></pre></div></div>
  27. <div class="paragraph"><p>That was all; simple right?</p></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2017-08-13 21:05:16 UTC</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  28. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  29. indexName: 'jmonkeyengine',
  30. inputSelector: '#doc-search',
  31. debug: false // Set debug to true if you want to inspect the dropdown
  32. });</script></body></html>