一、ServerEndpoint註解作用
在Java EE 7規範中,WebSocket是一種新的協議,它允許服務器與客戶端之間進行雙向通信。而ServerEndpoint註解就是用來標記一個類是WebSocket服務器端點的。
簡單來說,ServerEndpoint註解的作用是將一個java類標記為WebSocket服務器端點,從而使得客戶端可以連接到服務器並且進行WebSocket通信。
二、ServerEndpoint註解前台無法連接
當我們使用ServerEndpoint註解標記一個java類後,我們可以通過瀏覽器進行WebSocket連接測試。但是一旦部署到實際環境中並開啟SSL,我們就會遇到前台無法連接的問題。
這是因為在使用ServerEndpoint註解時,如果服務器需要SSL協議,那麼就需要在註解中指定wss協議以及TLS協議版本。https的端口多是443,而wss的端口號通常為8443。
@ServerEndpoint(value="/websocket", configurator = WebSocketConfig.class)
public class MyWebSocket {
}
在web.xml中配置端口信息:
javax.websocket.server.https.port
8443
三、ServerEndpoint註解中的屬性
ServerEndpoint註解中有很多屬性,其中最常用的是value、decoders和encoders。
value:用於指定WebSocket服務器端點的URI,它必須以”/”開頭。例如,如果我們想要將MyEndpoint類標記為URI為”/myendpoint”的WebSocket服務器端點,則可以使用以下代碼:
@ServerEndpoint("/myendpoint")
public class MyEndpoint {}
decoders:用於指定需要在WebSocket服務器端點上使用的解碼器(用於將接收到的消息解碼為對象)的列表。例如,如果我們想要在MyEndpoint類上使用Base64解碼器,則可以使用以下代碼:
@ServerEndpoint(
value="/myendpoint",
decoders={Base64Decoder.class}
)
public class MyEndpoint {}
encoders:用於指定需要在WebSocket服務器端點上使用的編碼器(用於將發送的對象編碼為消息)的列表。例如,如果我們想要在MyEndpoint類上使用 Base64編碼器,則可以使用以下代碼:
@ServerEndpoint(
value="/myendpoint",
encoders={Base64Encoder.class}
)
public class MyEndpoint {}
四、@ServerEndpoint註解選取
在實際應用中,我們通常需要用到ServerEndpoint中的一些屬性或者方法,下面是使用枚舉類型、編碼和解碼器等註解。
1、使用枚舉類型
在Java EE 7規範中,共引入了CompletableFuture和CompletionStage,這是Java關於並發編程的一項重大革命。同時,CompletableFuture和CompletionStage也支持WebSocket編程。
@ServerEndpoint(value = "/hello")
public class HelloServer {
@OnOpen
public void onOpen(Session session) throws Exception {
CompletableFuture future = CompletableFuture.supplyAsync(() -> "Hello from CompletableFuture.");
future.thenAcceptAsync((message) -> {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
2、編解碼器
在WebSocket通信中,我們需要將Java對象序列化為JSON字符串或者其他格式的字符串,然後才能在網絡上傳輸。而我們可以使用編碼器和解碼器來自動實現Java對象到字符串的轉換。
編碼器將Java對象轉換為字符串,而解碼器則將字符串轉換為Java對象。我們可以自定義編碼器和解碼器,也可以使用WebSocket API提供的現成的編碼器和解碼器。
@ServerEndpoint(value = "/greeting")
public class GreetingServer {
@OnMessage
public void onMessage(Session session, String name) throws IOException, EncodeException {
session.getBasicRemote().sendObject(new GreetingResponse("Hello, " + name + "!"));
}
}
public class GreetingResponseEncoder implements Encoder.Text {
@Override
public String encode(final GreetingResponse greetingResponse) {
return new Gson().toJson(greetingResponse);
}
}
public class GreetingResponseDecoder implements Decoder.Text {
@Override
public GreetingResponse decode(final String greetingResponseJson) {
return new Gson().fromJson(greetingResponseJson, GreetingResponse.class);
}
@Override
public boolean willDecode(final String s) {
return true;
}
@Override
public void init(final EndpointConfig endpointConfig) {
}
@Override
public void destroy() {
}
}
3、路徑參數
路徑參數可以用來獲取連接到服務器端點的WebSocket客戶端的一些信息,例如連接到服務器端點的IP地址、端口號等。我們可以通過@PathParam註解將這些信息注入到我們的WebSocket服務器端點類中。
@ServerEndpoint(value = "/user/{id}")
public class UserServerEndpoint {
@OnOpen
public void onOpen(Session session, @PathParam("id") long id) {
System.out.println("User " + id + " connected to the server.");
}
}
五、總結
本文對ServerEndpoint註解的作用、前台無法連接問題、常見屬性以及@ServerEndpoint註解相關應用場景進行了詳細闡述。希望對讀者對WebScoket的應用有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/297245.html
微信掃一掃
支付寶掃一掃