Esempio n. 1
0
  /** Transport connected. {@link IOTransport} calls this when a connection is established. */
  public void transportConnected() {

    setState(STATE_READY);
    if (reconnectTask != null) {
      reconnectTask.cancel();
      reconnectTask = null;
    }
    resetTimeout();
    synchronized (outputBuffer) {
      if (transport.canSendBulk()) {
        ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
        this.outputBuffer = new ConcurrentLinkedQueue<String>();
        try {
          // DEBUG
          String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
          log.debug("Bulk start:");
          for (String text : texts) {
            log.debug("> " + text);
          }
          log.debug("Bulk end");
          // DEBUG END
          transport.sendBulk(texts);
        } catch (IOException e) {
          this.outputBuffer = outputBuffer;
        }
      } else {
        String text;
        while ((text = outputBuffer.poll()) != null) {
          sendPlain(text);
        }
      }
      this.keepAliveInQueue = false;
    }
  }
Esempio n. 2
0
  /** Invalidates an {@link IOTransport}, used for forced reconnecting. */
  private void invalidateTransport() {

    if (transport != null) {
      transport.invalidate();
    }
    transport = null;
  }
Esempio n. 3
0
  /**
   * Sends a plain message to the {@link IOTransport}.
   *
   * @param text the Text to be send.
   */
  private void sendPlain(String text) {

    synchronized (outputBuffer) {
      if (getState() == STATE_READY) {
        try {
          log.debug("> " + text);
          transport.send(text);
        } catch (Exception e) {
          log.debug("IOEx: saving");
          outputBuffer.add(text);
        }
      } else {
        outputBuffer.add(text);
      }
    }
  }
Esempio n. 4
0
  /** Cleanup. IOConnection is not usable after calling this. */
  private void cleanup() {

    setState(STATE_INVALID);
    if (transport != null) {
      transport.disconnect();
    }
    sockets.clear();
    synchronized (connections) {
      List<IOConnection> con = connections.get(urlStr);
      if (con != null && con.size() > 1) {
        con.remove(this);
      } else {
        connections.remove(urlStr);
      }
    }
    log.debug("cleanup");
    backgroundTimer.cancel();
  }
Esempio n. 5
0
  /** Connect transport */
  private void connectTransport() {

    if (getState() == STATE_INVALID) {
      return;
    }
    setState(STATE_CONNECTING);
    if (protocols.contains(WebSocketTransport.TRANSPORT_NAME)) {
      transport =
          WebSocketTransport.create(this.url.getProtocol() + "://" + this.url.getAuthority(), this);
    } else if (protocols.contains(XhrTransport.TRANSPORT_NAME)) {
      transport =
          XhrTransport.create(this.url.getProtocol() + "://" + this.url.getAuthority(), this);
    } else {
      error(
          new SocketIOException(
              "Server supports no available transports. You should reconfigure the server to support a available transport"));
      return;
    }
    transport.connect();
  }
Esempio n. 6
0
 /**
  * Returns the name of the used transport
  *
  * @return the name of the currently used transport
  */
 public String getTransport() {
   IOTransport transport = this.connection.getTransport();
   return transport != null ? transport.getName() : null;
 }