Ver código fonte

1、完成ws消息推送

Clevercsc 5 anos atrás
pai
commit
2dfe3e824e

+ 36 - 0
src/main/java/cn/clevercsc/niuniutool/config/CorsConfig.java

@@ -0,0 +1,36 @@
+package cn.clevercsc.niuniutool.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+@Configuration
+public class CorsConfig {
+    @Value("${cors.allowed-origins:}")
+    private String[] allowedOrigins;
+
+    @Bean
+    public CorsFilter corsFilter() {
+        if (allowedOrigins == null || allowedOrigins.length <= 0) {
+            allowedOrigins = new String[]{"*"};
+        }
+
+        CorsConfiguration config = new CorsConfiguration();
+        config.setAllowedOrigins(Arrays.asList(allowedOrigins));
+        config.setAllowedHeaders(Collections.singletonList("*"));
+        config.setAllowedMethods(Collections.singletonList("*"));
+        config.setAllowCredentials(true);
+        config.setMaxAge(60 * 60 * 1000L);
+
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", config);
+
+        return new CorsFilter(source);
+    }
+}

+ 20 - 0
src/main/java/cn/clevercsc/niuniutool/config/WebSocketConfig.java

@@ -0,0 +1,20 @@
+package cn.clevercsc.niuniutool.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+/**
+ * 开启WebSocket支持
+ * @Author: cyg
+ * @Date: 2020/5/28 0028
+ * @Version 1.0
+ */
+@Configuration
+public class WebSocketConfig {
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        return new ServerEndpointExporter();
+    }
+
+}

+ 6 - 4
src/main/java/cn/clevercsc/niuniutool/controller/NiuniuController.java

@@ -3,10 +3,7 @@ package cn.clevercsc.niuniutool.controller;
 import cn.clevercsc.niuniutool.service.NiuniuService;
 import cn.clevercsc.niuniutool.util.HttpResult;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 /**
  * @author csc
@@ -31,4 +28,9 @@ public class NiuniuController {
     public HttpResult pinTuan(){
         return niuniuService.pinTuan();
     }
+
+    @GetMapping("user-info")
+    public HttpResult userInfo(){
+        return niuniuService.userInfo();
+    }
 }

+ 8 - 4
src/main/java/cn/clevercsc/niuniutool/service/NiuniuService.java

@@ -80,8 +80,9 @@ public class NiuniuService {
             UserInfoDto userInfoDto = JSONObject.parseObject(String.valueOf(result.getData()),UserInfoDto.class);
             if (dashboardDto==null){
                 dashboardDto= new DashboardDto();
-                dashboardDto.setBrokerage_price(userInfoDto.getBrokerage_price());
+
             }
+            dashboardDto.setBrokerage_price(userInfoDto.getBrokerage_price());
             log.info("{} 更新用户信息:{}", DateUtils.formatDate(new Date()),JSONObject.toJSONString(result));
             return result;
         } catch (ConnectException e) {
@@ -113,8 +114,9 @@ public class NiuniuService {
 
     @Scheduled(cron = "12 0/2 * * * ? ")
     public void updatePinTuan() {
+        userInfo();
         HttpResult httpResult =  pinTuan();
-        if (pinTuan()==null){
+        if (httpResult==null){
             throw new RuntimeException("更新拼团信息失败");
         }
         PinTuanDto pinTuanDto = JSONObject.parseObject(String.valueOf(httpResult.getData()),PinTuanDto.class);
@@ -123,8 +125,8 @@ public class NiuniuService {
         }
         if (dashboardDto==null){
             dashboardDto= new DashboardDto();
-            dashboardDto.setPinTuanDto(pinTuanDto);
         }
+        dashboardDto.setPinTuanDto(pinTuanDto);
         if (!pinTuanDto.getAlready_pinzhong().equals(lastPintuanDto.getAlready_pinzhong())){
             log.info("你拼中了  {}  个商品,请尽快处理",(pinTuanDto.getAlready_pinzhong()-lastPintuanDto.getAlready_pinzhong()));
             lastPintuanDto=pinTuanDto;
@@ -134,6 +136,8 @@ public class NiuniuService {
 
     @Scheduled(cron = "0/5 * * * * ? ")
     public void sendDashboardWs() {
-        dashboardWs.send(JSONObject.toJSONString(dashboardDto, SerializerFeature.DisableCircularReferenceDetect));
+        if (dashboardDto!=null){
+            dashboardWs.send(JSONObject.toJSONString(dashboardDto, SerializerFeature.DisableCircularReferenceDetect));
+        }
     }
 }

+ 5 - 5
src/main/java/cn/clevercsc/niuniutool/ws/DashboardWs.java

@@ -18,11 +18,12 @@ import java.io.IOException;
 @Component
 @ServerEndpoint("/ws/dashboard")
 public class DashboardWs {
-    Session session;
+    private  static Session session;
 
     @OnOpen
     public void onOpen(Session session) {
-        this.session = session;
+        DashboardWs.session = session;
+        send("ok");
     }
 
     @OnClose
@@ -32,12 +33,11 @@ public class DashboardWs {
 
     public  void send(String data) {
         try {
-            if (session.isOpen()) {
+            if (session!= null) {
                 session.getAsyncRemote().sendText(data);
-            } else {
-                session.close();
             }
         } catch (Exception ignored) {
+            ignored.printStackTrace();
             //ignore this exception
         }
     }