Skip to content

Instantly share code, notes, and snippets.

@jfarcand
Created November 12, 2014 21:34
Show Gist options
  • Save jfarcand/083d4d8c10e1ca03026f to your computer and use it in GitHub Desktop.
Save jfarcand/083d4d8c10e1ca03026f to your computer and use it in GitHub Desktop.
@ManagedService(path = "/chat", atmosphereConfig = MAX_INACTIVE + "=120000")
public class InjectChat {
private final Logger logger = LoggerFactory.getLogger(InjectChat.class);
// Normally Encoder and Decoder are managed by the @Message annotation, but for this sample we inject them instead.
@Inject
private Decoder<String, Message> decoder;
@Inject
private Encoder<Message, String> encoder;
@Inject
BroadcasterFactory factory;
/**
* Invoked when the connection as been fully established and suspended, e.g ready for receiving messages.
*
* @param r
*/
@Ready
public void onReady(final AtmosphereResource r) {
logger.info("Browser {} connected.", r.uuid());
logger.info("Injected Factory {} connected.", factory.getClass().getName());
}
/**
* 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());
}
}
@org.atmosphere.config.service.Message
public String onMessage(String s) throws IOException {
Message message = decoder.decode(s);
logger.info("{} just send {}", message.getAuthor(), message.getMessage());
return encoder.encode(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment