Java 通过Websocket 实现消息推送

本文介绍了如何在Spring Boot中利用WebSocket协议实现实时通信,包括配置WebSocket的启动类、SecurityConfig过滤,以及如何通过ServerEndpoint和WebSocket服务类处理连接、消息发送和接收。
摘要由CSDN通过智能技术生成

一、WebSocket?

WebSocket协议是基于的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。

二、实践:

注:主启动类需要加上@EnableScheduling,否则不生效。

还有需要 SecurityConfig 类配置过滤掉

  【导入maven】

<!--WebSocket -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

【创建WeSocket的配置类】

package com.dataanalysis.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    /**
     * 如果使用Springboot默认内置的tomcat容器,则必须注入ServerEndpoint的bean;
     * 如果使用外置的web容器,则不需要提供ServerEndpointExporter,下面的注入可以注解掉
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }
}

【Spring注入service】

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.text.ParseException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j  //用于日志
@ServerEndpoint(value = "/websocket/xxx/xxx")   //将该类定义为一个webSocket的服务端
@Component  //实例化到spring容器,泛指各种组件,不需要归类的时候,需要加上。在websocket必加
public class testWebSocket{


    //开始
    public static TestService testService ;  //前文有讲过,需要注入【跳转在上面】

    @Autowired
    public void setSenderService(TestService testService){
        testWebSocket.testService= testService;
    }
    //结束

   
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);


    /** 记录当前在线连接数 */
    private static AtomicInteger onlineCount = new AtomicInteger(0);

    /** 存放所有在线的客户端 */
    private static Map<String, Session> clients = new ConcurrentHashMap<>();
    private static Map<String, String> clientParmas = new ConcurrentHashMap<>();

    @PostConstruct
    public  void init(){
        //新建定时线程池
        Task task = new Task();
        //用于定时发送
        scheduledExecutorService.scheduleAtFixedRate(task,1,10, TimeUnit.SECONDS);
    }


    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        onlineCount.incrementAndGet(); // 在线数加1
        clients.put(session.getId(), session);
    }


    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {
        onlineCount.decrementAndGet(); // 在线数减1
        clients.remove(session.getId());
        log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());
    }


    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }


     /**
      * 服务端发送消息给客户端
      */
    private void sendMessage(String message, Session toSession) {
        try {
            log.info("服务端给客户端[{}]发送消息[{}]", toSession.getId(), message);
            toSession.getBasicRemote().sendText(message);
        } catch (Exception e) {
            log.error("服务端发送消息给客户端失败:{}", e);
        }
    }





     /**
      * 收到客户端消息后调用的方法
      *
      * @param message
      * 客户端发送过来的消息
      */
    @OnMessage
    public void onMessage(String message, Session session) throws ParseException {
        log.info("服务端收到客户端[{}]的消息[{}]", session.getId(), message);
        this.params = message;
        clientParmas.put(session.getId(),message);
        if(!StringUtils.isEmpty(message)){
            //前端传输过来是一个base64的字符,转换成一个map
           String deStr = new String(Base64.getDecoder().decode(parmas));
           Map<Object,Object> map = JSON.parseObject(deStr,Map.class);

            //具体业务编写。
            List<String> list = ....;
            //然后推送回前端
           sendMessage(JSON.toJSONString(list), session);
            
        }
    }


  public void sendMessageAll(String message) throws IOException {
        for (Session item : clients.values()) {
            item.getAsyncRemote().sendText(message);
        }
    }

    public static void broadcastAllUsers(String message) {

        try {
            for (Session item: clients.values()) {
                item.getAsyncRemote().sendText(message);
            }
        } catch (Exception e) {
            log.error("广播消息失败!!!");
        }
    }


    //定时自动推送数据
    class Task implements Runnable {
        @Override
        public void run() {
            clients.keySet().forEach(key -> {
                Session toSession = clients.get(key);
                if (toSession != null) {
                    String parmas = clientParmas.get(toSession.getId());
                    if (!StringUtils.isEmpty(parmas)) {
                        String deStr = new String(Base64.getDecoder().decode(parmas));
                        Map<Object,Object> map = JSON.parseObject(deStr,Map.class);
                       //具体业务编写。
                        List<String> list = ....;
                        //然后推送回前端
                        sendMessage(JSON.toJSONString(list), toSession);
                    }
                }
            });

        }
    }

}

注:也是边学边写,如有错误请指出,有不懂的,可留言,互相交流学习。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java WebSocket是一种在Web应用程序之间实现双向通信的技术。实现WebSocket推送消息的方式有多种。其中一种方式是通过使用JavaWebSocket API来实现一个WebSocket服务器,并在服务器端使用WebSocket连接将消息推送到客户端。 根据引用中的代码示例,可以看到在Java中使用WebSocket推送消息的步骤。首先,在Controller类中创建一个用于处理WebSocket连接的方法,如@GetMapping("/socket/{cid}")。这个方法接受一个路径参数cid,表示连接的客户端ID。然后,在推送数据的接口中,使用WebSocketServer.sendInfo(message,cid)方法将消息发送给指定的客户端。 另外,根据引用中的代码示例,还可以通过创建一个WebSocketService类来发送消息。在该类中,使用WebSocketService的sendMessage方法来发送消息给客户端。 综上所述,要在Java实现WebSocket推送消息,可以通过创建一个WebSocket服务器或使用WebSocketService类来发送消息给客户端。具体的实现方式可以根据具体需求和代码示例进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java消息推送 WebSocket通信](https://download.csdn.net/download/lj88811498/12453985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java WebSocket 实现消息推送](https://blog.csdn.net/qq_32091599/article/details/106357195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [JavaWebSocket实现消息推送【保姆来了!】](https://blog.csdn.net/weixin_53059814/article/details/126905593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值