Springboot实现WebSocket客户端
WebSocket客户端1.新建一个maven项目命名为websocket_client1.1 pom文件如下:1.2 新增application.yml文件1.3 新增启动类2. WebSocket客户端核心代码实现2.1 新增WebSocketClient实现类2.2 启动类中添加初始化MyWebSocketClient代码,讲MyWebSocketClient交给Spring容器管理2...
·
WebSocket客户端
在前面的博客中 SpringBoot中的WebSocket讲述了如何搭建基于Springboot的websocket服务端,但是测试的时候是直接用浏览器中的在线测试工具测试的,如何直接写代码测试呢,本篇文章就讲一下如何搭建websocket客户端。
1. 新建一个maven项目命名为websocket_client
1.1 完善pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.owner</groupId>
<artifactId>websocket_client</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--WebSocket核心依赖包-->
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
</dependency>
</dependencies>
</project>
1.2 新增application.yml文件
server:
port: 8002
# 设置服务名
spring:
application:
name: websocket-client
1.3 新增启动类
@SpringBootApplication
public class WebSocketApplication {
public static void main(String[] args){
SpringApplication.run(WebSocketApplication.class, args);
}
}
2. WebSocket客户端核心代码实现
2.1 新增WebSocketClient实现类
public class MyWebSocketClient extends WebSocketClient {
private static final Logger LOGGER = LoggerFactory.getLogger(MyWebSocketClient.class);
public MyWebSocketClient(URI serverUri) {
super(serverUri);
}
@Override
public void onOpen(ServerHandshake arg0) {
// TODO Auto-generated method stub
LOGGER.info("------ MyWebSocket onOpen ------");
}
@Override
public void onClose(int arg0, String arg1, boolean arg2) {
// TODO Auto-generated method stub
LOGGER.info("------ MyWebSocket onClose ------{}",arg1);
}
@Override
public void onError(Exception arg0) {
// TODO Auto-generated method stub
LOGGER.info("------ MyWebSocket onError ------{}",arg0);
}
@Override
public void onMessage(String arg0) {
// TODO Auto-generated method stub
LOGGER.info("-------- 接收到服务端数据: " + arg0 + "--------");
}
}
2.2 启动类中添加初始化MyWebSocketClient代码,将MyWebSocketClient交给Spring容器管理
@Bean
public WebSocketClient webSocketClient() {
try {
MyWebSocketClient webSocketClient = new MyWebSocketClient(new URI("ws://127.0.0.1:8001/websocket/12/owner"));
webSocketClient.connect();
return webSocketClient;
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
2.3 添加一个WebSocketController类,用发送请求的方式控制WebSocket客户端与服务端通讯
@RestController
public class WebSocketController {
@Autowired
private WebSocketClient webSocketClient;
@RequestMapping("subscribe")
public String subscribe() {
// webSocketClient.connect();
webSocketClient.send("hello sever,i want subscribe data A");
return "发送订阅成功!!!";
}
}
3. 测试
首先启动websocket_server,确定websocket服务端正常运行,然后启动websocket_client。
在浏览器中访问http://127.0.0.1:8002/subscribe
再来看一下客户端的日志
上面一条日志是客户端启动时连接服务端成功的日志,第二条是发送订阅信息的日志,服务端返回订阅成功
服务端日志
可以看到服务端正常收到了订阅请求
更多推荐
已为社区贡献1条内容
所有评论(0)