小工具知识网

当前位置: 首页 > 行业知识 > Java 实现使用 Websocket 发送消息

行业知识

Java 实现使用 Websocket 发送消息

2026-12-06 06:10:00 来源:互联网转载

文章目录

  • 前言
  • 一、websocket 简介
  • 二、websocket 如何使用
    • 1.引入依赖包
    • 2.创建 WebSocketConfig
    • 3.创建 WebSocketServer
    • 4.在线测试
    • 5.模拟消息发送
    • 总结

      前言

      • 需求背景:之前做了个楼栋房间数量出租了多少,需要在数据大屏上实时展示
      • 解决方案:以切面拦截出租接口,当有房间出租时重新查询相关数据,封装好用websocket实时传送前端展示

        提示:以下是本篇文章正文内容,下面案例可供参考

        一、websocket 简介

        WebSocket 是一种在单个TCP连接上进行全双工通信的协议。WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。

        二、websocket 如何使用

        1.引入依赖包

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

        2.创建 WebSocketConfig

        package com.alpari.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.socket.config.annotation.EnableWebSocket;import org.springframework.web.socket.server.standard.ServerEndpointExporter;/** * @author wangrui * @date 2023/11/8 **/@Configuration@EnableWebSocketpublic class WebSocketConfig {    @Bean    public ServerEndpointExporter serverEndpoint() {        return new ServerEndpointExporter();    }    /**     * 注意事项:     * WebSocket启动的时候优先于spring容器,从而导致在WebSocketServer中调用业务Service会报空指针异常     * 所以需要在WebSocketServer中将所需要用到的service给静态初始化一下     * 此处提前注入     *///    @Autowired//    private void setXxx(XxxService xxxService) {//        WebSocketServer.xxxService = xxxService;//    }}

        3.创建 WebSocketServer

        package com.alpari.config;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;import javax.websocket.*;import javax.websocket.server.PathParam;import javax.websocket.server.ServerEndpoint;import java.io.IOException;import java.util.concurrent.CopyOnWriteArraySet;/** * @author wangrui * @date 2023/11/8 * @description: 若在此类中直接调用service方法失败,需要加上:public static XxxService xxxService; 另外在config中注入实例 **/@Slf4j@Component@ServerEndpoint(value = "/websocket/{userId}")public class WebSocketServer {    /**     * 当前连接数     */    private static int onlineCount = 0;    /**     * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象     */    private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();    /**     * 与某个客户端的连接会话,需要通过它来给客户端发送数据     */    private Session session;    /**     * 接收的用户id     */    private String userId;    /**     * 连接建立成功调用的方法     */    @OnOpen    public void onOpen(Session session, @PathParam("userId") String userId) {        this.session = session;        webSocketSet.add(this);        addOnlineCount();        //在线数加1        log.info("有新连接加入!当前在线人数为" + getOnlineCount());        this.userId = userId;        try {            sendMessage("连接成功");        } catch (IOException e) {            log.error("websocket IO异常", e);        }    }    /**     * 连接关闭调用的方法     */    @OnClose    public void onClose() {        webSocketSet.remove(this);        subOnlineCount();               //在线数减1        log.info("有一连接关闭!当前在线人数为" + getOnlineCount());    }    /**     * 收到客户端消息后调用的方法     *     * @param message 客户端发送过来的消息     */    @OnMessage    public void onMessage(String message) {        log.info("来自客户端的消息:" + message);        //群发消息        for (WebSocketServer item : webSocketSet) {            try {                item.sendMessage(message);            } catch (IOException e) {                log.error("发送消息失败", e);            }        }    }    /**     * @param error 错误     */    @OnError    public void onError( Throwable error) {        log.error("发生错误", error);    }    /**     * 发送消息     * @param message 消息     * @throws IOException 异常     */    public void sendMessage(String message) throws IOException {        this.session.getBasicRemote().sendText(message);    }    /**     * 群发自定义消息     */    public static void sendInfo(String message, @PathParam("userId") String userId) {        log.info("消息内容:" + message);        for (WebSocketServer item : webSocketSet) {            try {                //这里可以设定只推送给这个sid的,为null则全部推送                if (userId == null) {                    item.sendMessage(message);                } else if (item.userId.equals(userId)) {                    item.sendMessage(message);                }            } catch (IOException e) {                log.error("消息发送失败", e);            }        }    }    /**     * 获取在线人数     * @return 在线人数     */    public static synchronized int getOnlineCount() {        return onlineCount;    }    /**     * 在线人数加一     */    public static synchronized void addOnlineCount() {        WebSocketServer.onlineCount++;    }    /**     * 在线人数减一     */    public static synchronized void subOnlineCount() {        WebSocketServer.onlineCount--;    }}

        4.在线测试

        在线ws测试地址:http://www.jsons.cn/websocket/

        • 输入测试
          • 后端返回的结果

            5.模拟消息发送

            /** * 切面拦截数据模拟给指定用户或全部用户发消息 */public void sendMessage(String userid) {    // 此处模拟数据大屏推送,以切面方式拦截相关接口,若数据发生变化,则将数据推送,不传id则全部发送    String msg = "模拟数据";    WebSocketServer.sendInfo(msg, userid);    log.info("发送消息成功,消息内容:{},接收人:{}", msg, userid);}

            总结

            当凉意与秋雨结伴;当黄叶与晨露相依;当冷月共霞光一色;当大雁也打点南归,我的祝福伴随着冬至的到来飘然而至:天气凉了,请记得添加衣裳!

上一篇:揭秘冬虫夏草香烟价格走势,最新价格趋势一览无余

下一篇:怎么启动exe服务器?