雪夜丶夏沫 2022-04-06 21:19 采纳率: 50%
浏览 226
已结题

websocket+springboot+定时任务,完成Echarts数据看板实时推送数据

问题遇到的现象和发生背景

公司要求将实时数据通过websocket传递到前端进行渲染,本人浏览了很长时间博客,发现并不理想
1.一个html页面怎么连接到java服务的,是通过java服务的什么注解
2.如果一个html页面连接到服务器,我怎么才能将这个html页面与我准备的数据结构对应上
3.定时任务我想让他在连接建立之后触发,断开连接后停止
4.定时任务生成的数据怎么才能定点发送给某个html页面

问题相关代码,请勿粘贴截图
@ServerEndpoint(value = "/socket/one")
@Component
@Slf4j
public class WebSocket {

    private static Map<String, Session> clients = new ConcurrentHashMap<>();
    private static List<VideoData> data = new ArrayList<>();
    private static List<List<VideoData>> datum = new ArrayList<>();
    private Session session;
    private static int onlineCount = 0;
    @Autowired
    private SafetyService safetyService;

    @OnOpen
    public void onOpen(Session session) throws InterruptedException {
        System.out.println("open connection:" + session.getId());
        this.session = session;
        if(clients.containsKey(session.getId())){
            clients.remove(session.getId());
        }else{
            //在线数加1
            addOnlineCount();
        }
        //加入set中
        clients.put(session.getId(), session);
        try {
            sendMessage("连接成功",session);
        } catch (Exception e) {
            log.error("用户:"+session.getId()+",网络异常!!!!!!");
        }
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("close connection:" + session.getId());
        if(clients.containsKey(session.getId())){
            clients.remove(session.getId());
            //从set中删除
            subOnlineCount();
        }
        clients.remove(session.getId());
    }

    // message为摄像头编号
    @OnMessage
    public void onMessage(String message, Session session) throws InterruptedException {
        System.out.println(session.getId() + ": " + message);
        if(StringUtils.isBlank(message)){
            sendMessage("摄像头为空",session);
        }
        while (true) {
                if(!session.isOpen()) {
                    break;
                }
            Thread.sleep(5000);
        }

    }

    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
    }

    public void sendMessage(String message,Session session) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    public static synchronized void addOnlineCount() {
        WebSocket.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocket.onlineCount--;
    }

}

运行结果及报错内容
我的解答思路和尝试过的方法

我尝试将定时任务写入websocket,但是控制不了触发条件

我想要达到的结果

解决上面的问题

  • 写回答

4条回答 默认 最新

  • 关注

    定时任务生成的数据放到数据库,前端可以用setInterval方法定时访问后端接口返回的数据。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • 未聞花名丶 2022-04-06 21:30
    关注

    主要是通过ws协议来做的,websocket相当于前后端保持长连接

    评论
  • 有问必答小助手 2022-04-06 22:58
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
  • 波波老师 Java领域新星创作者 2022-04-07 12:18
    关注

    你应该是想问怎么实现点对点通信(单播)。
    给你提供一个思路:可以利用广播的形式实现伪单播,也就是说,消息我给每个html都发一遍,但获取消息时要判断我当前这个html对应的用户名跟消息中的用户名是否一致,如果不一致则丢弃

    另外,websocket肯定是可以实现单播的,因为我很久之前就做过一个springboot+websocket+dubbo的网页聊天系统(毕设)。
    你看一下这篇文章。https://blog.csdn.net/hry2015/article/details/81123549

    img

    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月14日
  • 已采纳回答 4月6日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
  • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
  • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
  • ¥15 在ubuntu中无法连接到远程服务器传输文件
  • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
  • ¥15 Oracle触发器字段变化时插入指定值
  • ¥15 docker无法进入容器内部
  • ¥15 qt https 依赖openssl 静态库
  • ¥15 python flask 报错
  • ¥15 改个密码引发的项目启动问题