-
Notifications
You must be signed in to change notification settings - Fork 8
Topic Listener
Lance edited this page Jan 5, 2014
·
1 revision
To receive notifications for connect
and disconnect
events you can contribute a TopicListener
via Tapestry IOC. A container
may contain multiple pushTargets
with multiple topics
, in this case the TopicListener
will receive connect
and disconnect
notifications for each unique topic
.
eg:
/**
* Leave the chat room when the client disconnects
*/
public class ChatTopicListener implements TopicListener {
private static final Pattern ROOM_PATTERN = Pattern.compile("rooms/(.*)/messages");
private final ChatManager chatManager;
public ChatTopicListener(ChatManager chatManager) {
super();
this.chatManager = chatManager;
}
@Override
public void onConnect(AtmosphereResource resource, String topic) {
}
@Override
public void onDisconnect(AtmosphereResource resource, String topic) {
Matcher matcher = ROOM_PATTERN.matcher(topic);
if (matcher.matches()) {
HttpSession session = resource.getRequest().getSession(false);
if (session != null) {
String chatUser = (String) session.getAttribute("CHAT_USER");
if (chatUser != null) {
String room = matcher.group(1);
chatManager.leaveRoom(room, chatUser);
}
}
}
}
}
public class AppModule {
public static void contributeTopicListener(OrderedConfiguration<TopicListener> config) {
config.addInstance("chat", ChatTopicListener.class);
}
}