前段代码如下:
function connect() {
try {
if ("WebSocket" in window) {
var socket = new SockJS(path + "/examSocket");
stompClient = Stomp.over(socket);
stompClient.connect({cyryzj: cyryzj, ksid: ksid}, function (frame) {
console.log('连接成功: ' + frame);
stompClient.subscribe('/exam/subscribe/' + cyryzj, function (frame) {
console.log('订阅成功: ' + frame);
}/*, {'cyryzj': cyryzj}*/);
}, function errorCallBack(error) {
console.log('连接失败【' + error + '】');
reconnect();
});
} else {
alert("您的浏览器不支持Websocket通信协议,请使用Chrome或者Firefox浏览器!");
return;
}
} catch (e) {
console.log('connect exception', e)
reconnect();
}
}
function send(ksid) {
let time = $(".time").text();
//console.log('crontabRefreshTTL:time', time)
stompClient.send("/crontabRefreshExamTime/" + ksid + "/" + cyryzj + "/" + time, {});
}
java代码如下:
@MessageMapping("/crontabRefreshExamTime/{ksid}/{cyryzj}/{time}") // 获取客户端的推送信息
public String crontabRefreshExamTime(@DestinationVariable("cyryzj") final String glzj,
@DestinationVariable("ksid") final String ksid,
@DestinationVariable("time") final String time) {
//log.info("从业人员主键:" + cyryzj + " 考试ID:" + ksid);
ExamKsxx examKsxx = (ExamKsxx) redisUtils.hget("ksxx", ksid);
if (Objects.isNull(examKsxx)) {
examKsxx = examKsxxService.getOne(new QueryWrapper<ExamKsxx>().lambda().eq(ExamKsxx::getLsh, ksid).eq(ExamKsxx::getSfyx, OLD_DEL_FLAG));
if (Objects.nonNull(examKsxx)) {
// 根据考卷时长决定当前考试信息在redis的逾期时长
long expireTimeLong = examKsxx.getKsjssj().getTime() - examKsxx.getKskssj().getTime();
long expireTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(expireTimeLong);
redisUtils.hset("ksxx", examKsxx.getLsh(), examKsxx, expireTimeSeconds);
}
}
String kslsh = examKsxx.getLsh();
String item = kslsh + ":" + glzj;
// 将考试时间更新
Date date = (Date) redisUtils.hget(CURR_EXAM_EXPIRE_TIME, item);
if (Objects.nonNull(date)) {
String formatDate = DateUtil.format(date, DateUtil.PATTERN_DATE);
String clientTime = Func.toStr(time);
String refreshExpire = formatDate + " " + clientTime;
Date time2 = tools.util.DateUtil.parse(refreshExpire, PATTERN_DATETIME);
redisUtils.hset(CURR_EXAM_EXPIRE_TIME, item, time2);
}
return null;
}
错误如下:
ERROR: Servlet.service() for servlet [dispatcherServlet] in context with path [/sysgl] threw exception [Request processing failed; nested exception is org.springframework.web.socket.sockjs.
SockJsException: Uncaught failure in SockJS request, uri=http://xxxxx/sysgl/examSocket/464/thlqjmnl/xhr_send?t=1633698025096;
nested exception is org.springframework.web.socket.sockjs.SockJsMessageDeliveryException: Failed to deliver message(s) [SEND
destination:/crontabRefreshExamTime/d7e2e214e190462e8b905fe4517d4b2f/d27cffe3e28e463984a789478b712529/00:56:45
] for session thlqjmnl; nested exception is org.springframework.web.socket.sockjs.SockJsMessageDeliveryException: Failed to deliver message(s) [SEND
destination:/crontabRefreshExamTime/d7e2e214e190462e8b905fe4517d4b2f/d27cffe3e28e463984a789478b712529/00:56:45
] for session thlqjmnl: Session closed] with root cause
org.springframework.web.socket.sockjs.SockJsMessageDeliveryException: Failed to deliver message(s) [SEND
destination:/crontabRefreshExamTime/d7e2e214e190462e8b905fe4517d4b2f/d27cffe3e28e463984a789478b712529/00:56:45
] for session thlqjmnl: Session closed
我在前端会setInterval调用send()方法,去做操作,每5s执行一次.