title: WebSocket and Leap Motion
body: <p>I'm coding a little application in Java with Eclipse for use my Leap Motion device like a mouse. All is working when a run from Eclipse, is fast, is responsive and not freeze, crash or others.</p>
  
  <p>I export the Java application to a Runnable <code>JAR</code>, I run it with <code>javaw -jar LeapMotionController.jar</code> </p>
  
  <p>All is slow, reading and writing in the XML for configurations, establishing a connection to local web socket service.</p>
  
  <p>The application has 3 Classes, </p>
  
  <ol>
  <li>LeapMotion </li>
  <li>SysTray</li>
  <li>and Settings</li>
  </ol>
  
  <p>The LeapMotion class is the main application, from this I start the Systray class and after I start the run command for initializing the connection.</p>
  
  <pre><code>SysTray.chgIcon("warn");
  container = ContainerProvider.getWebSocketContainer();
  container.setDefaultMaxBinaryMessageBufferSize(1024);
  container.setDefaultMaxTextMessageBufferSize(1024);
  session=container.connectToServer(LeapMotion.class, URI.create("ws://localhost:6437/v7.json"));
  session.setMaxBinaryMessageBufferSize(1024);
  session.setMaxTextMessageBufferSize(1024);
  wait4TerminateSignal();
  </code></pre>
  
  <blockquote>
    <p>The wait4TerminateSignal is,</p>
  </blockquote>
  
  <pre><code>private static void  wait4TerminateSignal() {
      synchronized(waitLock) {
          try {
              for (;;) {
                  waitLock.wait();
                  if(session==null) {
                      waitLock.notify();
                      break;
                  }
              }
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The Systray is showed after 1 second, after the read of the XML file for setup variables. The doc is loaded in variable for reuse (limit the read of the direct file).
  After 5-10 secondes, I receive the first answer of the websocket, the handshake with info about the device version....</p>
  
  <p>After, all message are queued and make a big buffer, I have tried two value (10 and 1024), one time is helped, after is gone very very slow.</p>
  
  <p>In the task manager, I use 120MB of memory (of 32GB), cpu never go over 1.5% (I have 8 cores, 16 threads at 3.2GHz). The javaw process have 50-60 threads used.</p>
  
  <p>I have also added a limit for drop activity. Is use a increment and when reach the value, work the message, and the increment is reset to 0.</p>
  
  <p>You need to know about the Leap Motion Web Socket;
   - Make average of 110 messages per second, I can't control the amount
   - Not need to send a message to get a reply
   - Only output in JSon format</p>
  
  <p>I have tested and searched about;
   - How optimise the Java (not find a real help)
   - Another way for use WebSocket, other need a message for get reply (chat)
   - use Elevator.exe (google it) for make the java in Admin right, not make change
   - Manually increase the process priority (little above, and Real Time), it's make no change
   - Not have found information about the buffer, and the code is one found on <a href="https://dzone.com/articles/sample-java-web-socket-client" rel="nofollow noreferrer">https://dzone.com/articles/sample-java-web-socket-client</a> (second part of code)</p>
  
  <p>If I have miss information or want more details, leave me a comment.</p>
  
  <p>I'm looking to increase the performance. With the amount of message and data I receive with the device, I can support queue drop or similar solution.</p>
  
  <p>Thank in advance for your help.</p>
  
  <p>(sorry for my poor English)</p>