Java Reference
In-Depth Information
Expanding our client application
It's time to show how you can leverage the WebSocket features in real life. In the previous
chapter, Chapter 7 , Adding Web Services to Your Applications , we created the ticket book-
ing application based on the REST API and AngularJS framework. It was clearly missing
one important feature: the application did not show information concerning ticket pur-
chases of other users. This is a perfect use case for WebSockets!
Since we're just adding a feature to our previous app, we will only describe the changes we
will introduce to it.
In this example, we would like to be able to inform all current users about other purchases.
This means that we have to store information about active sessions. Let's start with the re-
gistry type object, which will serve this purpose. We can use a Singleton session bean
for this task, as shown in the following code:
@Singleton
public class SessionRegistry {
private final Set<Session> sessions = new HashSet<>();
@Lock(LockType.READ)
public Set<Session> getAll() {
return Collections.unmodifiableSet(sessions);
}
@Lock(LockType.WRITE)
public void add(Session session) {
sessions.add(session);
}
@Lock(LockType.WRITE)
public void remove(Session session) {
sessions.remove(session);
}
}
Search WWH ::




Custom Search