From 5c81eb3a998883c8f02cd7001f48fc21e99919ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=AE=97=E9=91=AB?= Date: Fri, 24 Nov 2023 00:05:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 - .../playword/controller/VoiceController.java | 1 - .../playword/controller/WebSocketServer.java | 254 +++++++++--------- src/main/resources/application-dev.yml | 2 +- src/main/resources/application-prod.yml | 10 +- src/main/resources/application.yml | 4 +- .../java/com/qymlxin/playword/TestMain.java | 20 +- 7 files changed, 154 insertions(+), 142 deletions(-) diff --git a/pom.xml b/pom.xml index 83a87cf..77568bd 100644 --- a/pom.xml +++ b/pom.xml @@ -83,11 +83,6 @@ 4.5.13 - - com.alibaba - fastjson - 1.2.28 - net.sf.json-lib diff --git a/src/main/java/com/qymlxin/playword/controller/VoiceController.java b/src/main/java/com/qymlxin/playword/controller/VoiceController.java index 5031071..9156e0b 100644 --- a/src/main/java/com/qymlxin/playword/controller/VoiceController.java +++ b/src/main/java/com/qymlxin/playword/controller/VoiceController.java @@ -31,7 +31,6 @@ public class VoiceController { ) throws IOException { // File file = new File(FILE_PATH + FILE_SEPARATOR + voiceType + FILE_SEPARATOR + fileName + ".mp3"); File file = new File(FILE_PATH + FILE_SEPARATOR + voiceType + FILE_SEPARATOR + fileName); - // System.out.println(file.getPath()); if(file.exists()) { String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if(mimeType == null) { diff --git a/src/main/java/com/qymlxin/playword/controller/WebSocketServer.java b/src/main/java/com/qymlxin/playword/controller/WebSocketServer.java index 6672f6f..eb9516f 100644 --- a/src/main/java/com/qymlxin/playword/controller/WebSocketServer.java +++ b/src/main/java/com/qymlxin/playword/controller/WebSocketServer.java @@ -1,126 +1,128 @@ -package com.qymlxin.playword.controller; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; - -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.io.IOException; -import java.util.concurrent.ConcurrentHashMap; - -@Component -@Slf4j -@ServerEndpoint("/api/pushMessage/{openId}") -public class WebSocketServer { - - // 静态变量:存储在线人数,存取需要加锁,保证线程安全 - private static int onlineCount = 0; - // concurrent包的线程安全Map,用来存放每个客户端对应的WebSocket对象。 - private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>(); - // 与某个客户端的连接会话,需要通过它来给客户端发送数据 - private Session session; - // 接受userId - private String openId; - - @OnOpen - public void onOpen(Session session, @PathParam("openId") String openId) { - this.session = session; - this.openId = openId; - if(webSocketMap.containsKey(openId)) { - webSocketMap.remove(openId); - webSocketMap.put(openId, this); - } else { - webSocketMap.put(openId, this); - addOnlineCount(); - } - log.info("用户连接:"+openId+",当前在线人数为:" + getOnlineCount()); - sendMessage("连接成功"); - } - - @OnMessage - public void onMessage(String message, Session session) { - log.info("用户消息:"+openId+",报文:"+message); - //可以群发消息 - //消息保存到数据库、redis - if(!StringUtils.isEmpty(message)) { - try { - //解析发送的报文 - JSONObject jsonObject = JSON.parseObject(message); - //追加发送人(防止串改) - jsonObject.put("fromUserId",this.openId); - String toUserId=jsonObject.getString("toUserId"); - //传送给对应toUserId用户的websocket - if (!StringUtils.isEmpty(toUserId) && webSocketMap.containsKey(toUserId)) { - webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString()); - } else { - //否则不在这个服务器上,发送到mysql或者redis - log.error("请求的userId:"+toUserId+"不在该服务器上"); - } - } catch (Exception e){ - e.printStackTrace(); - } - } - } - - @OnClose - public void onClose() { - if(webSocketMap.containsKey(openId)) { - webSocketMap.remove(openId); - subOnlineCount(); - } - log.info("用户退出:"+openId+",当前在线人数为:" + getOnlineCount()); - } - - /** - * 实现服务 - * 器主动推送 - */ - public void sendMessage(String message) { - try { - this.session.getBasicRemote().sendText(message); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - *发送自定 - *义消息 - **/ - public static void sendInfo(String message, String userId) { - log.info("发送消息到:"+userId+",报文:"+message); - if(StringUtils.isEmpty(userId) && webSocketMap.containsKey(userId)){ - webSocketMap.get(userId).sendMessage(message); - }else{ - log.error("用户"+userId+",不在线!"); - } - } - - /** - * 获取此时的在线人数 - */ - public static synchronized int getOnlineCount() { - return onlineCount; - } - - /** - * 在线人数加一 - */ - public static synchronized void addOnlineCount() { - WebSocketServer.onlineCount++; - } - - /** - * 在线人数减一 - */ - public static synchronized void subOnlineCount() { - WebSocketServer.onlineCount--; - } -} +//package com.qymlxin.playword.controller; +// +//import com.alibaba.fastjson.JSON; +//import com.alibaba.fastjson.JSONObject; +//import com.fasterxml.jackson.databind.ObjectMapper; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.stereotype.Component; +//import org.springframework.util.StringUtils; +// +//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.io.IOException; +//import java.util.concurrent.ConcurrentHashMap; +// +//@Component +//@Slf4j +//@ServerEndpoint("/api/pushMessage/{openId}") +//public class WebSocketServer { +// +// // 静态变量:存储在线人数,存取需要加锁,保证线程安全 +// private static int onlineCount = 0; +// // concurrent包的线程安全Map,用来存放每个客户端对应的WebSocket对象。 +// private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>(); +// // 与某个客户端的连接会话,需要通过它来给客户端发送数据 +// private Session session; +// // 接受userId +// private String openId; +// +// @OnOpen +// public void onOpen(Session session, @PathParam("openId") String openId) { +// this.session = session; +// this.openId = openId; +// if(webSocketMap.containsKey(openId)) { +// webSocketMap.remove(openId); +// webSocketMap.put(openId, this); +// } else { +// webSocketMap.put(openId, this); +// addOnlineCount(); +// } +// log.info("用户连接:"+openId+",当前在线人数为:" + getOnlineCount()); +// sendMessage("连接成功"); +// } +// +// @OnMessage +// public void onMessage(String message, Session session) { +// log.info("用户消息:"+openId+",报文:"+message); +// //可以群发消息 +// //消息保存到数据库、redis +// if(!StringUtils.isEmpty(message)) { +// try { +// //解析发送的报文 +//// ObjectMapper objectMapper = new ObjectMapper(); +// JSONObject jsonObject = JSON.parseObject(message); +// //追加发送人(防止串改) +// jsonObject.put("fromUserId",this.openId); +// String toUserId=jsonObject.getString("toUserId"); +// //传送给对应toUserId用户的websocket +// if (!StringUtils.isEmpty(toUserId) && webSocketMap.containsKey(toUserId)) { +// webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString()); +// } else { +// //否则不在这个服务器上,发送到mysql或者redis +// log.error("请求的userId:"+toUserId+"不在该服务器上"); +// } +// } catch (Exception e){ +// e.printStackTrace(); +// } +// } +// } +// +// @OnClose +// public void onClose() { +// if(webSocketMap.containsKey(openId)) { +// webSocketMap.remove(openId); +// subOnlineCount(); +// } +// log.info("用户退出:"+openId+",当前在线人数为:" + getOnlineCount()); +// } +// +// /** +// * 实现服务 +// * 器主动推送 +// */ +// public void sendMessage(String message) { +// try { +// this.session.getBasicRemote().sendText(message); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// +// /** +// *发送自定 +// *义消息 +// **/ +// public static void sendInfo(String message, String userId) { +// log.info("发送消息到:"+userId+",报文:"+message); +// if(StringUtils.isEmpty(userId) && webSocketMap.containsKey(userId)){ +// webSocketMap.get(userId).sendMessage(message); +// }else{ +// log.error("用户"+userId+",不在线!"); +// } +// } +// +// /** +// * 获取此时的在线人数 +// */ +// public static synchronized int getOnlineCount() { +// return onlineCount; +// } +// +// /** +// * 在线人数加一 +// */ +// public static synchronized void addOnlineCount() { +// WebSocketServer.onlineCount++; +// } +// +// /** +// * 在线人数减一 +// */ +// public static synchronized void subOnlineCount() { +// WebSocketServer.onlineCount--; +// } +//} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index e1bb1cb..2fb2be7 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -2,7 +2,7 @@ server: port: 9191 resource: voice: - path: E:\voice\ + path: E:\pycharm_project_factory\project\audio\zip spring: datasource: url: jdbc:mysql://localhost:3306/playword.world?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 4ca14f8..3ddcf8f 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -2,13 +2,13 @@ server: port: 9191 resource: voice: - path: /home/playword/voice + path: /www/wwwroot/PlayWord.World/voice spring: datasource: - url: jdbc:mysql://qymlxin.cn:3306/PlayWord.World?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true + url: jdbc:mysql://localhost:3306/PlayWord.World?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true driver-class-name: com.mysql.jdbc.Driver - username: root - password: 123456 + username: PlayWord.Developer + password: i-Play2022 type: com.alibaba.druid.pool.DruidDataSource druid: # 初始化时建立物理连接的个数 @@ -78,7 +78,7 @@ wechat: # appid: wxb2cc5b16a923cefa # secret: 9279820e27607ab5ad0f5421bc851df0 appid: wxe61bb1627297c4f5 - secret: c6fe0954309f085f02fc4de73a1f3b8f + secret: 7ffa14ff044c1d4f053576d680fd508e login-url: https://api.weixin.qq.com/sns/jscode2session login-grant-type: authorization_code diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 24b5510..b83bbd9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,4 @@ spring: profiles: -# active: ${env:prod} - active: dev \ No newline at end of file + active: ${env:prod} +# active: dev \ No newline at end of file diff --git a/src/test/java/com/qymlxin/playword/TestMain.java b/src/test/java/com/qymlxin/playword/TestMain.java index b378f67..b4426c3 100644 --- a/src/test/java/com/qymlxin/playword/TestMain.java +++ b/src/test/java/com/qymlxin/playword/TestMain.java @@ -1,13 +1,29 @@ package com.qymlxin.playword; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; +import java.util.HashMap; public class TestMain { - public static void main(String[] args) { - + public static void main(String[] args) throws Exception { + HashMap hashMap = new HashMap<>(); +// hashMap.put("test1", "value1"); +// hashMap.put("test2", "value1"); +// hashMap.put("test3", "value1"); +// hashMap.put("test4", "value1"); +// hashMap.put("test5", "value1"); +// hashMap.put("test6", "value1"); + String str = "{\"test4\":\"value1\",\"test5\":\"value1\",\"test2\":\"value1\",\"test3\":\"value1\",\"test6\":\"value1\",\"test1\":\"value1\"}"; + ObjectMapper objectMapper = new ObjectMapper(); + HashMap hashMap1 = objectMapper.readValue(str, HashMap.class); + System.out.println(hashMap1); } } -- Gitee