Ejemplo n.º 1
0
 /**
  * Invoked when the client disconnect or when an unexpected closing of the underlying connection
  * happens.
  *
  * @param event
  */
 @Disconnect
 public void onDisconnect(AtmosphereResourceEvent event) {
   if (event.isCancelled()) {
     logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
   } else if (event.isClosedByClient()) {
     logger.info("Browser {} closed the connection", event.getResource().uuid());
   }
 }
Ejemplo n.º 2
0
 @Override
 public void onDisconnect(final AtmosphereResourceEvent event) {
   try {
     if (event.isCancelled() || event.isClosedByClient()) {
       System.out.println("onDisconnect:: cancelled/closed");
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 3
0
  @Override
  public void onStateChange(AtmosphereResourceEvent event) throws IOException {
    AtmosphereResource resource = event.getResource();

    String id = resource.uuid();
    if (event.isCancelled()) {
      callWithUi(resource, disconnectCallback);
    } else if (event.isResuming()) {
      // A connection that was suspended earlier was resumed (committed to
      // the client.) Should only happen if the transport is JSONP or
      // long-polling.
      getLogger().log(Level.FINER, "Resuming request for resource {0}", id);
    } else {
      // A message was broadcast to this resource and should be sent to
      // the client. We don't do any actual broadcasting, in the sense of
      // sending to multiple recipients; any UIDL message is specific to a
      // single client.
      getLogger().log(Level.FINER, "Writing message to resource {0}", id);

      Writer writer = resource.getResponse().getWriter();
      writer.write(event.getMessage().toString());

      switch (resource.transport()) {
        case WEBSOCKET:
          break;
        case SSE:
        case STREAMING:
          writer.flush();
          break;
        case JSONP:
        case LONG_POLLING:
          resource.resume();
          break;
        default:
          getLogger().log(Level.SEVERE, "Unknown transport {0}", resource.transport());
      }
    }
  }
  /**
   * Write the {@link AtmosphereResourceEvent#getMessage()} back to the client using the {@link
   * org.atmosphere.cpr.AtmosphereResponse#getOutputStream()} or {@link
   * org.atmosphere.cpr.AtmosphereResponse#getWriter()}. If a {@link org.atmosphere.cpr.Serializer}
   * is defined, it will be invoked and the writ operation will be delegated to to it.
   *
   * <p>By default, this method will try to use {@link
   * org.atmosphere.cpr.AtmosphereResponse#getWriter()}.
   *
   * @param event the {@link AtmosphereResourceEvent#getMessage()}
   * @throws java.io.IOException
   */
  public void onStateChange(AtmosphereResourceEvent event) throws IOException {

    Object message = event.getMessage();
    AtmosphereResponse r = event.getResource().getResponse();
    if (message == null || event.isCancelled() || event.getResource().getRequest().destroyed())
      return;

    if (event.getResource().getSerializer() != null) {
      try {
        event
            .getResource()
            .getSerializer()
            .write(event.getResource().getResponse().getOutputStream(), message);
      } catch (Throwable ex) {
        logger.warn("Serializer exception: message: " + message, ex);
        throw new IOException(ex);
      }
    } else {
      boolean isUsingStream =
          (Boolean) event.getResource().getRequest().getAttribute(PROPERTY_USE_STREAM);

      if (!isUsingStream) {
        try {
          r.getWriter();
        } catch (IllegalStateException e) {
          isUsingStream = true;
        }
      }

      if (message instanceof List) {
        for (String s : (List<String>) message) {
          if (isUsingStream) {
            r.getOutputStream().write(s.getBytes(r.getCharacterEncoding()));
            r.getOutputStream().flush();
          } else {
            r.getWriter().write(s);
            r.getWriter().flush();
          }
        }
      } else {
        if (isUsingStream) {
          r.getOutputStream().write(message.toString().getBytes(r.getCharacterEncoding()));
          r.getOutputStream().flush();
        } else {
          r.getWriter().write(message.toString());
          r.getWriter().flush();
        }
      }

      Boolean resumeOnBroadcast = event.getResource().resumeOnBroadcast();
      if (!resumeOnBroadcast) {
        // For legacy reason, check the attribute as well
        Object o =
            event.getResource().getRequest().getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
        if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
          resumeOnBroadcast = Boolean.class.cast(o);
        }
      }

      if (resumeOnBroadcast != null && resumeOnBroadcast) {
        event.getResource().resume();
      }
    }
  }