From df86ed661f5bc4322acb6f5fdb73443d874e1438 Mon Sep 17 00:00:00 2001 From: wangliwen Date: Tue, 26 Jul 2022 17:03:38 +0800 Subject: [PATCH] =?UTF-8?q?websocket=E5=AE=9A=E6=97=B6=E5=BF=83=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../renren/websocket/task/HeartRateTask.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 renren-admin/src/main/java/io/renren/websocket/task/HeartRateTask.java diff --git a/renren-admin/src/main/java/io/renren/websocket/task/HeartRateTask.java b/renren-admin/src/main/java/io/renren/websocket/task/HeartRateTask.java new file mode 100644 index 00000000..58e95139 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/websocket/task/HeartRateTask.java @@ -0,0 +1,35 @@ +package io.renren.websocket.task; + +import io.renren.modules.job.task.ITask; +import io.renren.websocket.WebSocketServer; +import io.renren.websocket.data.MessageData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + + +/** + * 与websocket客户端定时心跳 + */ +@Component("heartRateTask") +public class HeartRateTask implements ITask { + private static final Logger logger = LoggerFactory.getLogger(HeartRateTask.class); + + @Autowired + private WebSocketServer webSocketServer; + + /** + * 执行定时任务接口 + * + * @param params 参数,多参数使用JSON数据 + */ + @Override + public void run(String params) { + logger.info("websocket心跳"); + MessageData message = new MessageData().msg("heartRate:" + LocalDateTime.now()); + webSocketServer.sendMessageAll(message); + } +}