@ServerEndpoint WebSocketClient区别
时间: 2024-06-19 17:02:22 浏览: 7
@ServerEndpoint和WebSocketClient都是Java EE中用于实现WebSocket的类,但是它们有不同的作用。
@ServerEndpoint是用于在Java EE服务器端实现WebSocket服务的注解,通过@ServerEndpoint注解可以将一个普通的Java类定义成WebSocket服务端,提供给客户端进行连接和通信。
而WebSocketClient则是用于在Java EE客户端实现WebSocket连接的类,通过WebSocketClient可以连接WebSocket服务端,并且进行消息的发送和接收。
简单来说,@ServerEndpoint是用于在Java EE服务器端实现WebSocket服务,而WebSocketClient则是用于在Java EE客户端连接和与WebSocket服务端进行通信。
相关问题
不使用ServerEndpoint开发websocket
在Java中实现WebSocket服务端,不一定需要使用`@ServerEndpoint`注解。可以通过实现`Endpoint`类来自定义WebSocket服务端,以下是一个简单的示例:
```java
import javax.websocket.*;
import javax.websocket.server.ServerEndpointConfig;
import java.io.IOException;
import java.nio.ByteBuffer;
public class MyWebSocketServer extends Endpoint {
private Session session;
@Override
public void onOpen(Session session, EndpointConfig config) {
this.session = session;
System.out.println("Client connected: " + session.getId());
}
@Override
public void onClose(Session session, CloseReason closeReason) {
System.out.println("Client disconnected: " + session.getId());
}
@Override
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
}
@Override
public void onMessage(String message, Session session) throws IOException {
System.out.println("Received message: " + message);
this.session.getBasicRemote().sendText("Hello, Client!");
}
@Override
public void onMessage(ByteBuffer buffer, boolean b) throws IOException {
System.out.println("Received binary message: " + buffer);
this.session.getBasicRemote().sendBinary(buffer);
}
public static void main(String[] args) throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri = "ws://localhost:8080/myendpoint";
MyWebSocketServer endpoint = new MyWebSocketServer();
ServerEndpointConfig config = ServerEndpointConfig.Builder.create(MyWebSocketServer.class, uri).build();
container.connectToServer(endpoint, config);
}
}
```
在上面的示例中,我们实现了`Endpoint`类,重写了`onOpen()`、`onClose()`、`onError()`和`onMessage()`方法,分别在连接建立、连接关闭、发生错误和接收到消息时被调用。为了向客户端发送消息,我们使用了`Session`对象的`getBasicRemote()`方法。
在`main()`方法中,我们使用了`WebSocketContainer`来连接到WebSocket终端点,并将`MyWebSocketServer`实例和`ServerEndpointConfig`对象传递给了`connectToServer()`方法。
在使用以上代码之前,同样需要引入Java WebSocket API的依赖项。注意,以上代码仅适用于端口直接连接的WebSocket。如果需要使用SSL/TLS连接,或者需要进行身份验证等操作,需要进行额外的配置。
WebSocket @OnMessage
The @OnMessage annotation is used in WebSocket programming to indicate that a method in a class should be invoked when a message is received from the client.
Syntax:
```java
@OnMessage
public void methodName(Session session, String message) {
// code to handle received message
}
```
Here, the method marked with @OnMessage takes two parameters: the WebSocket session object and the message received from the client. The method body can contain any logic to process the message.
For example, the following code snippet shows how to implement a WebSocket endpoint that echoes back any messages it receives from the client:
```java
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/echo")
public class EchoEndpoint {
@OnMessage
public void onMessage(Session session, String message) throws Exception {
session.getBasicRemote().sendText(message);
}
}
```
In this example, the WebSocket endpoint is mapped to the "/echo" URI. The onMessage() method simply sends back the received message to the client using the session's getBasicRemote() method.
Note that the @OnMessage annotation can also be used with other data types besides String, such as ByteBuffer, InputStream, and Reader.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)