package org.javaee7.websocket.atmosphere; import org.atmosphere.config.service.Disconnect; import org.atmosphere.config.service.ManagedService; import org.atmosphere.config.service.Ready; import org.atmosphere.cpr.AtmosphereResource; import org.atmosphere.cpr.AtmosphereResourceEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; /** * Simple annotated class that demonstrate the power of Atmosphere. This class supports all transports, support * message length guarantee, heart beat, message cache thanks to the @ManagedAService. * * The client will first try with WebSocket and then fallback using the client's preference. */ @ManagedService(path = "/chat") public class ChatEndpoint { private final Logger logger = LoggerFactory.getLogger(ChatEndpoint.class); /** * * @param r */ @Ready public void onReady(final AtmosphereResource r) { logger.info("Browser {} connected.", r.uuid()); } /** * 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()); } } /** * Simple annotated class that demonstrate how {@link org.atmosphere.config.managed.Encoder} and {@link org.atmosphere.config.managed.Decoder * can be used. * * @param message an instance of {@link Message} * @return * @throws IOException */ @org.atmosphere.config.service.Message(encoders = { JacksonEncoder.class }, decoders = { JacksonDecoder.class }) public Message onMessage(Message message) throws IOException { logger.info("{} just send {}", message.getAuthor(), message.getMessage()); return message; } }