苍穹外卖--WebSocket-服务器与客户端双向通讯
This commit is contained in:
@@ -117,6 +117,11 @@
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
</dependency>
|
||||
<!--WebSocket依赖:客户端&服务端双向通讯-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.sky.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* WebSocket配置类,用于注册WebSocket的Bean
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfiguration {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public class OrderTask {
|
||||
|
||||
//处理订单超时未支付状态
|
||||
// @Scheduled(cron = "0 * * * * ? ")
|
||||
@Scheduled(cron = "0/5 * * * * ? ")
|
||||
// @Scheduled(cron = "0/5 * * * * ? ")
|
||||
public void procesTimeOutOrder(){
|
||||
log.info("定时任务自动执行,处理支付超时订单:"+ LocalDateTime.now());
|
||||
//每分中查询一下订单,是否有超过15分(下单时间+15分钟 < 当前时间)
|
||||
@@ -42,7 +42,7 @@ public class OrderTask {
|
||||
|
||||
//每日处理“派送中”的订单为已完成,每天凌晨1点执行一次
|
||||
// @Scheduled(cron = "0 0 1 * * ? ")
|
||||
@Scheduled(cron = "0/5 * * * * ? ")
|
||||
// @Scheduled(cron = "0/5 * * * * ? ")
|
||||
public void processPsOrder(){
|
||||
log.info("定时任务自动执行,处理派送中,未完成的订单:"+ LocalDateTime.now());
|
||||
//每天上午1点查询一下,前一天是否有派送中,未完成的订单(下单时间< 当前时间 - 1小时)
|
||||
|
||||
22
sky-server/src/main/java/com/sky/task/WebSocketTask.java
Normal file
22
sky-server/src/main/java/com/sky/task/WebSocketTask.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.sky.task;
|
||||
|
||||
import com.sky.websocket.WebSocketServer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
public class WebSocketTask {
|
||||
@Autowired
|
||||
private WebSocketServer webSocketServer;
|
||||
|
||||
/**
|
||||
* 通过WebSocket每隔5秒向客户端发送消息
|
||||
*/
|
||||
@Scheduled(cron = "0/5 * * * * ?")
|
||||
public void sendMessageToClient() {
|
||||
webSocketServer.sendToAllClient("这是来自服务端的消息:" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.sky.websocket;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.websocket.OnClose;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* WebSocket服务
|
||||
*/
|
||||
@Component
|
||||
@ServerEndpoint("/ws/{sid}")
|
||||
public class WebSocketServer {
|
||||
|
||||
//存放会话对象
|
||||
private static Map<String, Session> sessionMap = new HashMap();
|
||||
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam("sid") String sid) {
|
||||
System.out.println("客户端:" + sid + "建立连接");
|
||||
sessionMap.put(sid, session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*
|
||||
* @param message 客户端发送过来的消息
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message, @PathParam("sid") String sid) {
|
||||
System.out.println("收到来自客户端:" + sid + "的信息:" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭调用的方法
|
||||
*
|
||||
* @param sid
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(@PathParam("sid") String sid) {
|
||||
System.out.println("连接断开:" + sid);
|
||||
sessionMap.remove(sid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public void sendToAllClient(String message) {
|
||||
Collection<Session> sessions = sessionMap.values();
|
||||
for (Session session : sessions) {
|
||||
try {
|
||||
//服务器向客户端发送消息
|
||||
session.getBasicRemote().sendText(message);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user