Accepting path parameters
Just as in RESTful services, we can accept path parameters through the URL to which the client tries to connect. Path parameters are useful in case you have multiple contexts of the same service, for example, a chatroom service.
To use path parameters, write a {PARAM_NAME}
expression in the URL pattern, as follows:
@ServerEndpoint("/rooms/{roomName}/{username}") public class RoomsEndpoint { @OnOpen public void onOpen(Session session, EndpointConfig endpointConfig, @PathParam("roomName") String roomName, @PathParam("username") String username) { System.out.println("room " + roomName); System.out.println("user " + username); } }
As you can see, we have included {roomName}
and {username}
as two path parameters that should be passed by the user. To connect to this endpoint, you need to use a URL such as the following:
Example URL > ws://localhost:9354/ch10_websockets/rooms/room1/user1
The values of roomName...