From c597826849a3de463d75fefbc4909d7c8cc19a33 Mon Sep 17 00:00:00 2001
From: yuyang <2265829957@qq.com>
Date: Wed, 11 Aug 2021 16:42:15 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E6=98=AF?=
=?UTF-8?q?=E5=90=A6=E5=9C=A8=E5=85=85=E7=94=B5sql?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../order/controller/WebSocketController.java | 65 +++++++------------
.../mapper/XhpcChargeOrderMapper.xml | 5 +-
2 files changed, 29 insertions(+), 41 deletions(-)
diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/WebSocketController.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/WebSocketController.java
index ab1e0736..5ce7866c 100644
--- a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/WebSocketController.java
+++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/WebSocketController.java
@@ -18,12 +18,13 @@ import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
+
/**
* @author yuyang
* @date 2021/8/9 14:33
*/
@Component
-@ServerEndpoint(value="/websocket/{userId}")
+@ServerEndpoint(value = "/websocket/{userId}")
public class WebSocketController {
/**
@@ -44,14 +45,16 @@ public class WebSocketController {
/**
* 连接建立成功调用的方法
- * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
+ *
+ * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
@OnOpen
- public void onOpen(@PathParam("userId") Long userId,Session session){
- this.userId =userId;
+ public void onOpen(@PathParam("userId") Long userId, Session session) {
+
+ this.userId = userId;
this.session = session;
- System.out.println("userId:"+userId);
- System.out.println("session:"+session);
+ System.out.println("userId:" + userId);
+ System.out.println("session:" + session);
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
@@ -61,41 +64,15 @@ public class WebSocketController {
e.printStackTrace();
}
}
- /**
- * 给指定的人发送消息
- * @param message
- */
- @OnMessage
- public void sendToUser(String message) {
- String sendUserno = message.split("[|]")[1];
- String sendMessage = message.split("[|]")[0];
- try {
- if (webSocketSet.get(sendUserno) != null) {
- webSocketSet.get(sendUserno).sendMessage("用户" + userno + "发来消息:" + "
" + sendMessage);
- } else {
- System.out.println("当前用户不在线");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 获取当前时间
- *
- * @return
- */
- private String getNowTime() {
- Date date = new Date();
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = format.format(date);
- return time;
- }
+
+
/**
* 连接关闭调用的方法
*/
@OnClose
- public void onClose(){
+ public void onClose() {
+
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
@@ -103,15 +80,17 @@ public class WebSocketController {
/**
* 收到客户端消息后调用的方法
+ *
* @param message 客户端发送过来的消息
* @param session 可选的参数
*/
@OnMessage
public void onMessage(String message, Session session) {
- System.out.println("收到:" + this.userId+"的消息");
+
+ System.out.println("收到:" + this.userId + "的消息");
System.out.println("来自客户端的消息:" + message);
try {
- message ="{\n" +
+ message = "{\n" +
" \"msg\": \"操作成功\",\n" +
" \"code\": 200,\n" +
" \"data\": {\n" +
@@ -133,7 +112,7 @@ public class WebSocketController {
ApplicationContext act = ApplicationContextRegister.getApplicationContext();
RedisService redisService = act.getBean(RedisService.class);
Object cacheObject = redisService.getCacheObject("gun:1472583698524602.seqdec");
- System.out.println("cacheObject:"+cacheObject);
+ System.out.println("cacheObject:" + cacheObject);
this.session.getBasicRemote().sendText(message);
@@ -144,24 +123,30 @@ public class WebSocketController {
/**
* 发生错误时调用
+ *
* @param session
* @param error
*/
@OnError
- public void onError(Session session, Throwable error){
+ public void onError(Session session, Throwable error) {
+
System.out.println("发生错误");
error.printStackTrace();
}
public static synchronized int getOnlineCount() {
+
return onlineCount;
}
public static synchronized void addOnlineCount() {
+
WebSocketController.onlineCount++;
}
public static synchronized void subOnlineCount() {
+
WebSocketController.onlineCount--;
}
+
}
diff --git a/xhpc-modules/xhpc-order/src/main/resources/mapper/XhpcChargeOrderMapper.xml b/xhpc-modules/xhpc-order/src/main/resources/mapper/XhpcChargeOrderMapper.xml
index c8b4aaa0..47c36964 100644
--- a/xhpc-modules/xhpc-order/src/main/resources/mapper/XhpcChargeOrderMapper.xml
+++ b/xhpc-modules/xhpc-order/src/main/resources/mapper/XhpcChargeOrderMapper.xml
@@ -87,7 +87,10 @@
where del_flag=0