现在的位置: 首页 > 综合 > 正文

4 Annotated Endpoints

2019年09月24日 ⁄ 综合 ⁄ 共 861字 ⁄ 字号 评论关闭


                    官网英文参考:
    

                    中文解析:

                    下面是一个通过注释方式实现的endpoint:
@ServerEndpoint("/echo")
public class EchoEndpoint {
   @OnMessage
   public void onMessage(Session session, String msg) {
      try {
         session.getBasicRemote().sendText(msg);
      } catch (IOException e) { ... }
   }
}
                    注释方式比编码方式实现更加简单,他是自动部署的,通过ServerEndPoint指定了相对路径。也不需要添加一个message handler,因为这个例子是通过 decorator模式,在调用的时候自动注释的Onmessage 方法。

                    下表列出了在 javax.websocket 包中可用的注释,在handler生命周期中可指定的方法。更多信息查看API

Annotation Event Example

OnOpen

Connection opened

@OnOpen
public void open(Session session, 
                 EndpointConfig conf) { }

OnMessage

Message received

@OnMessage
public void message(Session session, 
                    String msg) { }

OnError

Connection error

@OnError
public void error(Session session, 
                  Throwable error) { }

OnClose

Connection closed

@OnClose
public void close(Session session, 
                  CloseReason reason) { }





抱歉!评论已关闭.