栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合WebSocket实现实时推送消息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合WebSocket实现实时推送消息

引入jar包

  
            org.springframework.boot
            spring-boot-starter-websocket
            2.1.0.RELEASE
        

配置WebSocketConfig

@Configuration
public class WebSocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

核心类,sid为推送服务标识

@Component
@ServerEndpoint("/websocket/{sid}")
@Slf4j
public class WebSocketServer {
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
    private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();

    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;

    //接收sid
    private String sid="";

    
    @onOpen
    public void onOpen(Session session,@PathParam("sid") String sid) {
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addonlineCount();           //在线数加1
        log.info("有服务开始监听:"+sid+",当前在线数为" + getonlineCount());
        this.sid=sid;
    }

    
    @onClose
    public void onClose() {
        webSocketSet.remove(this);  //从set中删除
        subonlineCount();           //在线数减1
        log.info("有一连接关闭!当前在线数为" + getonlineCount());
    }

    
    @onMessage
    public void onMessage(String message, Session session) {
        //log.info("收到来自窗口"+sid+"的信息:"+message);
        if("heart".equals(message)){
            try {
                sendMessage("heartOk");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    
    @onError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }
    
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    
    public static void sendInfo(String message,String sid)  {

        for (WebSocketServer item : webSocketSet) {
            try {
                //为null则全部推送
               if(sid==null) {
                item.sendMessage(message);
                log.info("推送消息到"+item.sid+",推送内容:"+message);
                }else if(item.sid.equals(sid)){
                   item.sendMessage(message);
                   log.info("推送消息到"+item.sid+",推送内容:"+message);
               }
            } catch (IOException e) {
                continue;
            }
        }
    }

    public static synchronized int getonlineCount() {
        return onlineCount;
    }

    public static synchronized void addonlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subonlineCount() {
        WebSocketServer.onlineCount--;
    }


}

创建通用返回类以及User类模拟传输数据

@Data
public class Result {
    private  Integer code;
    private  String msg;
    private Object data;
}
@Data
@AllArgsConstructor
public class User {
    private Integer id;
    private  String name;
    private  String psw;
}

创建定时任务模拟接受mq或者其他消息需要推送的场景此处10秒传递一次

@Component
public class Timetask {
    
    //10秒传递一次
    @Scheduled(cron="*/10 * *  * * ? ")
    public void JqcaseSearch() {
        try {
            Result result = new Result();
            result.setCode(200);
            List userList = Arrays.asList(new User(1,"张三","12345")
                    , new User(2,"李四","54321"));
            result.setData(userList);
            result.setMsg("查询成功!");
            String resultJson= new ObjectMapper().writevalueAsString(result);
            WebSocketServer.sendInfo(resultJson,"111");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

启动项目,使用websocket测试网站测试,会写前端的可以自行写。

websocket在线测试

成功连接拿到正确数据,此处使用111连接使用其他id则拿不到数据!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号