From the course: WebSocket Programming with Java EE
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Lifecycle logic
- [Instructor] Okay, so we're still in the ChatServerEndpoint class, and now we want to add logic for the remaining life cycle methods. So first, let's take a look at the onMessage method. This method receives the message from the client, and in our example we also want to broadcast the message to all connected clients. So if you're a member, all sessions are stored in the list in the room instance. And to send a message to a client, the client session is required. This is because within the session object is an instance of the remote endpoint class that represents the client as is able to send messages to the client. And to broadcast a message, we must retrieve a list of all the sessions for a given room and iterate over those sessions and send the same message to all those clients. Remember that we've stored the room name in the session object, and we have a convenience method that retrieves the room name. So within the onMessage method, we're going to extract the room from the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
WebSocket methodologies1m 52s
-
(Locked)
WebSocket functionality1m 56s
-
(Locked)
Annotation methodology2m 38s
-
Implement WebSocket lifecycles6m 37s
-
(Locked)
Configure the session5m 32s
-
(Locked)
Lifecycle logic4m 1s
-
(Locked)
Programmatic methodology1m 16s
-
(Locked)
WebSocket client API1m 37s
-
(Locked)
Code a client API6m 54s
-
(Locked)
✓ Challenge: Code programmatic endpoint31s
-
(Locked)
✓ Solution: Code programmatic endpoint1m 9s
-
(Locked)
-
-
-
-
-