当前位置:   article > 正文

快速搭建springboot websocket客户端_springboot实现websocket客户端

springboot实现websocket客户端

一、前言

WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。

HTML5 定义的 WebSocket 协议,能更好的节省服务器资源和带宽,并且能够更实时地进行通讯。

HTML5 定义的 WebSocket 协议,能更好的节省服务器资源和带宽,并且能够更实时地进行通讯。

浏览器通过 JavaScript 向服务器发出建立 WebSocket 连接的请求,连接建立以后,客户端和服务器端就可以通过 TCP 连接直接交换数据。

二、快速搭建springboot-websocket项目的服务端

1 导入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-websocket</artifactId>
  8. </dependency>

2 创建配置类

  1. @Configuration
  2. public class WebSocketConfig {
  3. @Bean
  4. public ServerEndpointExporter serverEndpointExporter() {
  5. return new ServerEndpointExporter();
  6. }
  7. }

3 创建WebSocketServer服务类 用来接收数据

websocket的常用注解只有这5个

  • @ServerEndpoint注意上下文路径,websocket连接地址

  • @OnMessage只有第一次加载websocket的时候,会调用,生命周期只有一次

  • @OnClose只有关闭websocket链接的时候,会调用,生命周期只有一次

  • @OnMessage每次接收信息的时候,都会调用,调用比较频繁

  • @OnError发生错误的时候调调用

  1. /**
  2. * 类似RequestMapping的地址
  3. * ws://localhost:8080/ws/000001
  4. */
  5. @ServerEndpoint("/ws/{uuid}")
  6. @Component
  7. public class WebSocketServer {
  8. private Session session; //客户端会话
  9. //存放每个客户端的连接会话
  10. public static ConcurrentHashMap<String,WebSocketServer> clients = new ConcurrentHashMap<>();
  11. //开启连接
  12. //存入连接回话中
  13. @OnOpen
  14. public void onOpen(Session session, @PathParam( "uuid") String uuid){
  15. System.out.println("当前的uuid为:"+uuid);
  16. this.session = session;
  17. clients.put(uuid,this);
  18. }
  19. //发送消息
  20. @OnMessage
  21. public void OnMessage(String msg, @PathParam( "uuid") String uuid){
  22. System.out.println("当前的uuid为:"+uuid);
  23. System.out.println("收到消息: "+msg);
  24. }
  25. //关闭连接
  26. @OnClose
  27. public void onClose(@PathParam( "uuid") String uuid){
  28. System.out.println("当前的uuid为:"+uuid);
  29. System.out.println("关闭socket连接"+uuid);
  30. clients.remove(uuid);
  31. }
  32. //发生异常的情况
  33. @OnError
  34. public void onError(Throwable error) {
  35. error.printStackTrace();
  36. }
  37. }

4 启动项目

三、一般都是前端为客户端,后端为服务端这种方式

前端客户端,我不会,需要的自己百度,哈哈哈

四、搭建java-websocket客户端

该案例,只是告诉大家,可以用java搭建客户端

4.1 依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.java-websocket</groupId>
  7. <artifactId>Java-WebSocket</artifactId>
  8. <version>1.3.5</version>
  9. </dependency>

4.2 客户端代码

  1. @Component
  2. public class WebSocketConfig {
  3. @Bean
  4. public WebSocketClient webSocketClient() {
  5. try {
  6. WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:8081/ws/000001"),new Draft_6455()) {
  7. @Override
  8. public void onOpen(ServerHandshake handshakedata) {
  9. System.out.println("ws 连接成功");
  10. }
  11. @Override
  12. public void onMessage(String message) {
  13. System.out.println("ws 收到消息"+message);
  14. }
  15. @Override
  16. public void onClose(int code, String reason, boolean remote) {
  17. System.out.println("ws 退出");
  18. }
  19. @Override
  20. public void onError(Exception ex) {
  21. System.out.println("连接错误"+ex.getMessage());
  22. }
  23. };
  24. webSocketClient.connect();
  25. return webSocketClient;
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. return null;
  30. }
  31. }

4.3 发送消息的代码

  1. @SpringBootApplication
  2. @RestController
  3. public class SpringbootWebsocketClientApplication {
  4. @Autowired
  5. private WebSocketClient webSocketClient;
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringbootWebsocketClientApplication.class, args);
  8. }
  9. @RequestMapping("/get")
  10. public String send(){
  11. webSocketClient.send("我是ws客户端,你好!!!");
  12. return "发送成功";
  13. }
  14. }

4.4 结果

4.5 服务端收到消息

  1. 当前的uuid为:000001
  2. 收到消息: 我是ws客户端,你好!!!

websocket传递头信息,协议头token的前后端解决方案

  1. js websocket 传递token

websocket协议在握手阶段借用了HTTP的协议,但是在JavaScript websocketAPI中并没有修改请求头的方法。

1.1 基于协议头

websocket请求头中可以包含Sec-WebSocket-Protocol这个属性,该属性是一个自定义的子协议。它从客户端发送到服务器并返回从服务器到客户端确认子协议。我们可以利用这个属性添加token。

  1. var token='fasdfadfasdfa'
  2. var ws = new WebSocket("ws://" + url+ "/webSocketServer",[token]);
  1. 后台取出websocket协议头的参数

2.1 取出token

token = ((HttpServletRequest) servletRequest).getHeader("Sec-WebSocket-Protocol");

2.2 注意大坑

如果传递了token参数,后端响应的时候,也必须带上这个token响应!否则前端接收不到数据!

可以采用servlet的过滤器来做

  1. @Order(1)
  2. @Component
  3. @WebFilter(filterName = "WebsocketFilter", urlPatterns = "/home/*")
  4. public class WebsocketFilter implements Filter {
  5. @Override
  6. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  7. HttpServletResponse response = (HttpServletResponse) servletResponse;
  8. String token = ((HttpServletRequest) servletRequest).getHeader("Sec-WebSocket-Protocol");
  9. response.setHeader("Sec-WebSocket-Protocol",token);
  10. filterChain.doFilter(servletRequest, servletResponse);
  11. }
  12. @Override
  13. public void destroy() {
  14. }
  15. }

六、结尾

  1. websocket最伟大之处在于服务器和客户端可以在给定的时间范围内的任意时刻,相互推送信息。 浏览器和服务器只需要要做一个握手的动作,在建立连接之后,服务器可以主动传送数据给客户端,客户端也可以随时向服务器发送数据。
  2. 第一、WebSocket是HTML5中的协议,支持持久连接;而Http协议不支持持久连接。
  3. 第二、首先,Websocket是一个持久化的协议,相对于HTTP这种非持久的协议来说
  4. HTTP的生命周期通过 Request 来界定,也就是一个 Request 一个 Response ,那么在 HTTP1.0 中,这次HTTP请求就结束了。
  5. 在HTTP1.1中进行了改进,使得有一个keep-alive,也就是说,在一个HTTP连接中,可以发送多个Request,接收多个Response。但是请记住 Request = Response , 在HTTP中永远是这样,也就是说一个request只能有一个response。而且这个response也是被动的,不能主动发起。
  6. 第三、传统的http请求,其并发能力都是依赖同时发起多个TCP连接访问服务器实现的(因此并发数受限于浏览器允许的并发连接数),而websocket则允许我们在一条ws连接上同时并发多个请求,即在A请求发出后A响应还未到达,就可以继续发出B请求。由于TCP的慢启动特性(新连接速度上来是需要时间的),以及连接本身的握手损耗,都使得websocket协议的这一特性有很大的效率提升。
  7. 第四、http协议的头部太大,且每个请求携带的几百上千字节的头部大部分是重复的,很多时候可能响应都远没有请求中的header空间大。如此多无效的内容传递是因为无法利用上一条请求内容,websocket则因为复用长连接而没有这一问题。
  8. 第五、当需要实现客户端刷新消息时,传统方案往往通过定时ajax请求实现,实际上对多数用户多数时间下这些请求都是无意义了,并且非常占用资源,websocket资源占用就小很多
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/178262
推荐阅读
相关标签
  

闽ICP备14008679号