03.WebSocket API - JAVA client / server API

WebSocket API - JAVA client / server API

Specification contained in JavaEE7, in the package javax.websocket, comprising API client and server API, the server API completely dependent on client API, just add some basic features thereof, it is only necessary to import the server that is dependent can. ( A WebSocket only the Java API specification, a specific implementation need web container, JavaEE-server or framework )

First, the client API

1) The client API is based on the abstract class or interface

  1. ContainerProvider
    • GetWebSocketContainer provide static method to obtain the underlying WebSocket client implementation
  2. WebSocketContainer
    • It provides access to all WebSocket client characteristics
    • 4 overloaded ConnectToServer; receives a the URL , and for connecting the remote terminal initialization handshake returns a Session, may also receive one of the following types ( must contain no reference constructor )
      • It marked @ClientEndpointany type of POJO
      • It marked @ClientEndpointany type of POJOClass<?>
      • EndpointExamples of classes and ClientEndpointConfiginstances
      • Class<? extends Endpoint>And ClientEndpointConfigexamples
  3. RemoteEndpoint
  4. Session
    • Closed session and other important functions

2) EndPoint and @ClientEndpoint

  1. The WebSocket Endpoint abstract class has three methods, onOpen, onClose, onError, they will occur when those events triggered; here focuses on the use @ClientEndpoint
  2. @ClientEndpoint class can have an (optional) a labeling @OnOpen @OnClose @OnErrormethod
  3. @ClientEndpoint Endpoint label class and inherits the class can specify one or more marked @OnMessageway, the remote terminal transmits for receiving text / binary message; annotations by using classes and methods, the choice of having large specific method parameters flexibility.

3) @ClientEndpoint marked class annotation annotation method signature

  1. @OnOpenTagging method

    • Optional Session
    • EndpointConfig optional
  2. @OnCloseTagging method

    • Optional Session
    • CloseReason optional
  3. @OnErrorTagging method

    • Optional Session
    • Throwable must
  4. @OnMessage

    • Optional Session
    • Mandatory parameters: composition of any one of the following parameters
      • String: means for receiving the complete text message.
      • String, Boolean: means for block mode receive a text message, and the last one is set to true in the boolean value.
      • Java Native type or native wrapper type: Full text messages for receiving and converted to that type.
      • Java.io.Reader objects: blocking flow in the manner of receiving text messages.
      • byte [] or java.nio.ByteBuffer: means for receiving a full binary message.
      • byte [] or ByteBuffer, boolean value: binary message received in block form.
      • java.io.InputStream: blocking flow in a manner to receive binary messages.
      • PongMessage Object: custom heartbeat response processing.
      • Any Java object: Some terminals have registered as corresponding to the drive, which will result into a corresponding claim type, message text or binary type must match register to the decoder
        • Decoder.Text
        • Decoder.Binary
        • Decoder.TextStream
        • Decoder.BinaryStream

Open, close, and error time, a terminal can have a corresponding label class methods; for @OnMessage, up to three message processing method: processing a text, a binary process, a heartbeat message pong process.

Second, the server-side API

ServerContainer inherited WebSocketContainer, it adds registered ServerEndpointConfig instances programmatically marked @ServerEndpoint methods and classes. In the servlet environment, calls ServletContext.getAttribute("javax.websocket.server.ServerContainer")can get ServerContainer instance, in a stand-alone environment, the need to achieve a specific instruction to get ServerContainer of WebSocket.

However, in the actual development, the above description can not care, do not need to get ServerContainer, just use the @ServerEndpointannotation server terminal type can , WebSocket Implementation Notes can scan the class, and automatically select and register a terminal server. Container creates a corresponding instance of the class at the terminal each time it receives WebSocket connection, destruction of the instance after connection is closed.

1) @ServerEndpoint annotation server class

  1. Use @ServerEndpoint, need to specify where the familiar value, a response indicating the URL corresponding to the program
@ServerEndpoint("/game/{var}")
public class GameServer { ... }
  1. Can be @OnOpen @OnClose @OnError @OnMessageused as a method of labeling @PathParam("var")acquired path parameter

Event handler method in a terminal server will work as event handling methods in the same client terminal. The difference between the server and the client only when the handshake. After completion of the handshake to establish a connection, the server and the client will become the endpoints, and is fully peer terminal has the same capabilities and responsibilities .

Reproduced in: https: //www.jianshu.com/p/95d8bf92a5e1

Guess you like

Origin blog.csdn.net/weixin_34040079/article/details/91204904