Created
November 12, 2014 21:34
-
-
Save jfarcand/083d4d8c10e1ca03026f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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