Java Reference
In-Depth Information
Listing 14.17. Message handlers from ActionBazaar
BulletinService
This listing shows two methods from the
BulletinService
WebSocket endpoint
. The first method takes a
String
message and a WebSocket session as parameters .
This method essentially handles the bulletin board chat functionality. The second method is
the more interesting of the two—it returns a
CommandResult
that will be converted
to JSON by the
CommandResultEncoder
class. It also accepts the client type that's a
variable from the URI . Finally, it accepts a
BulletinCommand
object that's decoded
from JSON using the
BulletinCommandDecoder
.
As you can see from the example, processing messages is straightforward. The annotated
API is much more flexible in terms of the parameters. Using the programmatic approach
with the
MessageHandler
interface, it's not possible to get path parameters or a copy
of the
Session
object.
@OnError
Error handling is very important for a WebSocket endpoint. At some point a connection to
a remote client will be lost or there will be a bug in the logic. The
@OnError
annotation
provides a mechanism for handling these unexpected situations. The annotation is defined
as follows: