The ServerEndpoint
annotation enables you to use URI templates to specify parts of an endpoint deployment URI as application parameters. For example, consider this endpoint:
ServerEndPoint 通过注解方式,用户URI模板可以定位到server中某一个endpoint中。
@ServerEndpoint("/chatrooms/{room-name}") public class ChatEndpoint { ... }
If the endpoint is deployed inside a web application called chatapp
at a local Java EE server in port 8080, clients can connect to the endpoint using any of the following URIs:
如果一个chatapp 的endpoint被部署到web应用中,那客户端可以通过下面的方式连接server endpoint。
http://localhost:8080/chatapp/chatrooms/currentnews http://localhost:8080/chatapp/chatrooms/music http://localhost:8080/chatapp/chatrooms/cars http://localhost:8080/chatapp/chatrooms/technology
Annotated endpoints can receive path parameters as arguments in methods annotated with @OnOpen
, @OnMessage
, and @OnClose
. In this example, the endpoint uses the parameter in the @OnOpen
method to determine which chat room the client wants to join:
路径也可以作为被注解(OnOpen,OnMessage,OnClose)方法的参数。下面这个例子就使path 参数作为方法参数。
@ServerEndpoint("/chatrooms/{room-name}") public class ChatEndpoint { @OnOpen public void open(Session session, EndpointConfig c, @PathParam("room-name") String roomName) { // Add the client to the chat room of their choice ... } }