title: WebSocket Slow - Java and JavaScript
body: <p>I am dealing with encoding of a minecraft plugin.
  But now I have the following problem, my websocket server respond very very slow.</p>
  
  <p>Here is my WebSocketClass (For the plugin)</p>
  
  <p>// socket server class</p>
  
  <pre><code>package me.mickerd.pcoc;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.net.UnknownHostException;
  import java.util.Collection;
  
  import org.bukkit.Bukkit;
  import org.java_websocket.WebSocket;
  import org.java_websocket.WebSocketImpl;
  import org.java_websocket.handshake.ClientHandshake;
  import org.java_websocket.server.WebSocketServer;
  
  public class WebsocketServer extends WebSocketServer {
  public static WebsocketServer s;
  
  public WebsocketServer(int port) throws UnknownHostException {
      super(new InetSocketAddress(port));
  }
  
  public WebsocketServer(InetSocketAddress address) {
      super(address);
  }
  
  @Override
  public void onOpen(WebSocket conn, ClientHandshake handshake) {
      WebsocketSessionManager.getSessionManager().openSession(conn.getRemoteSocketAddress().getAddress().getHostAddress());
      Bukkit.getLogger().info(conn.getRemoteSocketAddress().getAddress().getHostName() + " has connected to the Websocket server!");
  }
  
  @Override
  public void onClose(WebSocket conn, int code, String reason, boolean remote) {
      WebsocketSessionManager.getSessionManager().endSession(conn.getRemoteSocketAddress().getAddress().getHostAddress());
      Bukkit.getLogger().info(conn + " has disconnected form the Websocket server");
  }
  
  @Override
  public void onMessage(WebSocket conn, String message) {
      Bukkit.getLogger().info("Recieve Websocket packet - " + conn + ":" + message);
      if (message.split(":")[0].equalsIgnoreCase("name")) {
          WebsocketSessionManager.getSessionManager().addSessionUsername(conn.getRemoteSocketAddress().getAddress().getHostAddress(), message.split(":")[1]);
      }
  }
  
  public static void runServer() throws InterruptedException, IOException {
      WebSocketImpl.DEBUG = true;
      int port = 8887;
      s = new WebsocketServer(port);
      s.start();
      Bukkit.getLogger().info("Websocket server started on port: " + s.getPort());
  }
  
  @Override
  public void onError(WebSocket conn, Exception ex) {
      ex.printStackTrace();
      if (conn != null) {
          // some errors like port binding failed may not be assignable to a specific websocket
      }
  }
  
  public void sendToAll(String data) {
      Collection&lt;WebSocket&gt; con = connections();
      synchronized (con) {
          for (WebSocket c : con) {
              c.send(data);
          }
      }
  }
  
  public void sendData(WebsocketSession session, String data) {
      Collection&lt;WebSocket&gt; con = connections();
      synchronized (con) {
          for (WebSocket c : con) {
              if (c.getRemoteSocketAddress().getAddress().getHostAddress().equalsIgnoreCase(session.getHost())) {
                  Bukkit.getLogger().info("Send data packet: " + data);
                  c.send(data);
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>and this is my receiver in Javascript:</p>
  
  <pre><code>var sound = null;
  var name = window.location
  document.session.name.value = name
  
  var text = document.session.name.value
  
  var ws = new WebSocket("ws://" + window.location.hostname + ":8887/");
  
  ws.onopen = function () {
          ws.send("name:" + delineate(text));
  document.getElementById("title").innerHTML = "Welcome on the music server. Please hold this window open!";
  
  };
  
  ws.onmessage = function (evt) {
  function loadScript(url, callback)
  {
      // Adding the script tag to the head as suggested before
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = url;
  
      // Then bind the event to the callback function.
      // There are several events for cross browser compatibility.
      script.onreadystatechange = callback;
      script.onload = callback;
  
      // Fire the loading
      head.appendChild(script);
  }
  if(evt.data == "stop"){
    sound.fadeOut(0, 3700);
  } else {
  sound = new Howl({
    urls: ['music/' + evt.data + '.ogg']
  }).play();
  console.log("playing music");
  };
  }
  
  ws.onclose = function () {
      alert("Closed!");
  };
  
  ws.onerror = function (err) {
      alert("Error: " + err);
  };
  
  function delineate(str) {
      theleft = str.indexOf("=") + 1;
      theright = str.lastIndexOf("&amp;");
      return (str.substring(theleft, theright));
  }
  </code></pre>
  
  <p>The reacts very slow, but other things on the server are incredible fast!</p>
  
  <p>Can someone help?</p>