Java Reference
In-Depth Information
02:21:35,437 INFO [org.wildfly.extension.undertow] (MSC
service thread 1-7) JBAS017534: Registered web context:
/ticket-agency-websockets
The full URL of the endpoint is
ws://localhost:8080/ticket-agency-web-
sockets/hello
, which is just a concatenation of the server and application address
with an endpoint path on an appropriate protocol.
The second used annotation
@OnOpen
defines the endpoint behavior when the connection
from the client is opened. It's not the only behavior-related annotation of the WebSocket
endpoint. Let's look to the following table:
Annotation Description
The connection is open. With this annotation, we can use the
Session
and
EndpointConfig
parameters. The first para-
meter represents the connection to the user and allows further communication. The second one provides some client-related
information.
@OnOpen
@OnMessage
This annotation is executed when a message from the client is being received. In such a method, you can just have
Session
and for example, the
String
parameter, where the
String
parameter represents the received message.
There are bad times when an error occurs. With this annotation, you can retrieve a
Throwable
object apart from standard
Session
.
@OnError
When the connection is closed, it is possible to get some data concerning this event in the form of the
CloseReason
type
object.
@OnClose
There is one more interesting line in our
HelloEndpoint
. Using the
Session
object,
it is possible to communicate with the client. This clearly shows that in WebSockets, two-
directional communication is easily possible. In this example, we decided to respond to a
connected user synchronously (
getBasicRemote()
) with just a text message
Hi!
(
sendText (String)
). Of course, it's also possible to communicate asynchronously
and send, for example, sending binary messages using your own binary bandwidth saving
protocol. We will present some of these processes in the next example.











































