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

Lifecycle logic

From the course: WebSocket Programming with Java EE

Start my 1-month free trial

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…

Contents