| .idea/encodings.xml | ●●●●● patch | view | raw | blame | history | |
| .idea/git_toolbox_prj.xml | ●●●●● patch | view | raw | blame | history | |
| .idea/inspectionProfiles/Project_Default.xml | ●●●●● patch | view | raw | blame | history | |
| websocketClient/src/main/java/org/example/config/AsyncConfiguration.java | ●●●●● patch | view | raw | blame | history | |
| websocketSerivce/src/main/java/org/example/websocket/config/AsyncConfiguration.java | ●●●●● patch | view | raw | blame | history | |
| websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java | ●●●●● patch | view | raw | blame | history | |
| websocketSerivce/src/main/resources/application.properties | ●●●●● patch | view | raw | blame | history |
.idea/encodings.xml
@@ -1,6 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8"> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/websocketClient/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/websocketSerivce/src/main/java" charset="UTF-8" /> <file url="PROJECT" charset="UTF-8" /> .idea/git_toolbox_prj.xml
New file @@ -0,0 +1,15 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="GitToolBoxProjectSettings"> <option name="commitMessageIssueKeyValidationOverride"> <BoolValueOverride> <option name="enabled" value="true" /> </BoolValueOverride> </option> <option name="commitMessageValidationEnabledOverride"> <BoolValueOverride> <option name="enabled" value="true" /> </BoolValueOverride> </option> </component> </project> .idea/inspectionProfiles/Project_Default.xml
@@ -1,5 +1,60 @@ <component name="InspectionProjectProfileManager"> <profile version="1.0"> <option name="myName" value="Project Default" /> <inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAbstractClassShouldStartWithAbstractNaming" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidApacheBeanUtilsCopy" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidCallStaticSimpleDateFormat" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidCommentBehindStatement" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidComplexCondition" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidConcurrentCompetitionRandom" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidDoubleOrFloatEqualCompare" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidManuallyCreateThread" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidMissUseOfMathRandom" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidNegationOperator" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidNewDateGetTime" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidPatternCompileInMethod" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidReturnInFinally" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidStartWithDollarAndUnderLineNaming" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaAvoidUseTimer" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaBigDecimalAvoidDoubleConstructor" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaBooleanPropertyShouldNotStartWithIs" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaClassCastExceptionWithSubListToArrayList" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaClassCastExceptionWithToArray" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaClassMustHaveAuthor" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaClassNamingShouldBeCamel" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaCollectionInitShouldAssignCapacity" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaCommentsMustBeJavadocFormat" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaConcurrentExceptionWithModifyOriginSubList" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaConstantFieldShouldBeUpperCase" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaCountDownShouldInFinally" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaDontModifyInForeachCircle" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaEnumConstantsMustHaveComment" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaExceptionClassShouldEndWithException" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaIbatisMethodQueryForList" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaLockShouldWithTryFinally" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaLowerCamelCaseVariableNaming" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaMethodReturnWrapperType" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaMethodTooLong" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaPackageNaming" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaPojoMustOverrideToString" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaPojoMustUsePrimitiveField" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaPojoNoDefaultValue" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaRemoveCommentedCode" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaServiceOrDaoClassShouldEndWithImpl" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaSneakyThrowsWithoutExceptionType" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaStringConcat" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaSwitchExpression" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaSwitchStatement" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaTestClassShouldEndWithTestNaming" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaThreadLocalShouldRemove" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaThreadPoolCreation" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaThreadShouldSetName" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaTransactionMustHaveRollback" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaUndefineMagicConstant" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaUnsupportedExceptionWithModifyAsList" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaUseQuietReferenceNotation" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="AlibabaUseRightCaseForDateFormat" enabled="true" level="WARNING" enabled_by_default="true" /> </profile> </component> websocketClient/src/main/java/org/example/config/AsyncConfiguration.java
New file @@ -0,0 +1,28 @@ package org.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; /** * @program: dabaogp * @description: * @create: 2024-06-25 16:37 **/ @Configuration public class AsyncConfiguration { @Bean(name = "threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(50); // 核心线程数 executor.setMaxPoolSize(100); // 最大线程数 executor.setQueueCapacity(300); // 队列容量 executor.setKeepAliveSeconds(60); // 线程空闲时的存活时间为60秒 executor.setThreadNamePrefix("MyThread-"); // 线程名称的前缀 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 使用 CallerRunsPolicy 拒绝策略 return executor; } } websocketSerivce/src/main/java/org/example/websocket/config/AsyncConfiguration.java
New file @@ -0,0 +1,28 @@ package org.example.websocket.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; /** * @program: dabaogp * @description: * @create: 2024-06-25 16:37 **/ @Configuration public class AsyncConfiguration { @Bean(name = "threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); // 核心线程数 executor.setMaxPoolSize(100); // 最大线程数 executor.setQueueCapacity(300); // 队列容量 executor.setKeepAliveSeconds(60); // 线程空闲时的存活时间为60秒 executor.setThreadNamePrefix("MyThread-"); // 线程名称的前缀 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 使用 CallerRunsPolicy 拒绝策略 return executor; } } websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java
@@ -4,6 +4,9 @@ import com.google.gson.reflect.TypeToken; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -12,9 +15,12 @@ import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.lang.reflect.Type; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @ClassDescription: websocket服务端 @@ -28,7 +34,10 @@ private Session session; private static AtomicInteger onlineCount = new AtomicInteger(0); private static CopyOnWriteArrayList<WsServer> wsServers = new CopyOnWriteArrayList<>(); private static CopyOnWriteArraySet<WsServer> wsServers = new CopyOnWriteArraySet<>(); @Autowired @Qualifier("threadPoolTaskExecutor") private ThreadPoolTaskExecutor threadPoolTaskExecutor; @OnOpen public void onOpen(Session session) { @@ -51,41 +60,60 @@ log.info("服务端断开连接,当前连接的客户端数量为:{}", count); } @OnMessage public void sendMessage(String message) throws IOException { private Map<String, Lock> sessionLocks = new ConcurrentHashMap<>(); private Lock getSessionLock(String sessionId) { sessionLocks.putIfAbsent(sessionId, new ReentrantLock()); return sessionLocks.get(sessionId); } public void sendMessageToAll(String message) { Map<String, Object> map = jsonToMap(message); if(map.get("pid").equals("00000001")){ if (map.get("pid").equals("00000001")) { System.out.println(message); } try { if (session.isOpen()) { this.session.getBasicRemote().sendText(message); List<Future<?>> futures = new ArrayList<>(); wsServers.forEach(ws -> { Future<?> future = threadPoolTaskExecutor.submit(() -> { Session session = ws.session; if (session != null && session.isOpen()) { Lock sessionLock = getSessionLock(session.getId()); sessionLock.lock(); try { synchronized (session){ session.getBasicRemote().sendText(message); } } catch (Exception e) { log.error("发送消息时出现异常: " + e.getMessage()); } finally { sessionLock.unlock(); } } else { log.error("会话不存在或已关闭,无法发送消息"); } }); futures.add(future); }); //等待所有任务执行完成 for (Future<?> future : futures) { try { future.get(); } catch (InterruptedException | ExecutionException e) { log.error("发送消息时出现异常: " + e.getMessage()); } } } catch (IOException e) { throw new IOException("消息发送失败", e); } catch (Exception e) { log.error("发送消息时出现异常: " + e.getMessage()); } } public void sendMessageToAll(String message) throws IOException { for (WsServer wsServer : wsServers) { wsServer.sendMessage(message); } } @PostMapping("/send2AllC") public void sendMessageToAll1(@RequestBody String message) throws IOException { CopyOnWriteArrayList<WsServer> ws = wsServers; for (WsServer wsServer : ws){ wsServer.sendMessage(message); } } public static Map<String, Object> jsonToMap(String json) { Gson gson = new Gson(); Type type = new TypeToken<Map<String, Object>>(){}.getType(); Type type = new TypeToken<Map<String, Object>>() { }.getType(); return gson.fromJson(json, type); } } websocketSerivce/src/main/resources/application.properties
@@ -5,11 +5,11 @@ #2. ??ifconfig????vsftpd????? #3. ???? ftp://?? ftp.server.ip=23.224.235.226 ftp.server.ip=103.215.76.36 ftp.user=ftp_stock ftp.pass=123456 ftp.address =/www/wwwroot/ftp_stock/ ftp.server.http.prefix=https://img.essencefunds.com/ ftp.server.http.prefix=https://img.112100.net/ #ftp.server.ip=47.56.200.145 #ftp.user=ofsuccess #ftp.pass=fdymdM34HHsS8iB6