Java Reference
In-Depth Information
Figure 9-1. Event bus sending
By registering handlers at addresses and sending messages to them, it's possible to build up
very sophisticated and/or decoupled sets of services that react in an entirely nonblocking
fashion. Note that within our design, we share no state.
Vert.x's event bus lets us send a variety of types of message over it, but all of them will be
wrapped in a Message object. Point-to-point messaging is available through the Message ob-
jects themselves; they may hold a reply handler for the sender of the Message object. Be-
cause in this case we want the actual body of the message—that is, the text itself—we just
called the body method. We'll send this text message to the receiving user's chat client, im-
plemented by writing the message down the TCP connection.
When our application wants to send a message from one user to another, it sends that mes-
sage to the address that represents the other user ( Example 9-4 ) . Again, this is that user's
username.
Example 9-4. Sending chat messages
eventBus . send ( user , name . get () + '>' + message );
Let's extend this very basic chat server to broadcast messages and followers. There are two
new commands that we need to implement in order for this to work:
▪ An exclamation mark representing the broadcast command, which sends all of its follow-
ing text to any following users. For example, if bob typed “!hello followers”, then all of
his followers would receive “bob>hello followers”.
▪ The follow command, which follows a specified user suffixed to the command, as in
“follow bob”.
Once we've parsed out the commands, we're going to implement the broadcastMessage
and followUser methods, which correspond to each of these commands.
Search WWH ::




Custom Search