Browse Source

超能运动会

kk 2 months ago
parent
commit
c64f82e7df

+ 22 - 0
game-business/src/main/java/com/game/business/config/GameFiveConfig.java

@@ -0,0 +1,22 @@
+package com.game.business.config;
+
+import com.game.business.util.Common;
+import com.game.business.websocket.WebSocketConnent;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.stereotype.Component;
+
+@Component
+@EnableAsync
+public class GameFiveConfig implements CommandLineRunner {
+
+    public static WebSocketConnent webSocketConnent;
+
+    @Override
+    @Async
+    public void run(String... args) throws Exception {
+        webSocketConnent = new WebSocketConnent(Common.WS_GAME_FIVE_URL, Common.WS_GAME_FIVE_REMARK_URL, Common.GAME_FIVE_NAME);
+        webSocketConnent.connect();
+    }
+}

+ 5 - 0
game-business/src/main/java/com/game/business/util/Common.java

@@ -14,6 +14,9 @@ public class Common {
     public static final String GAME_THREES_CODE = "3";
     public static final String GAME_THREES_CODE = "3";
     public static final String GAME_FOUR_NAME = "four";
     public static final String GAME_FOUR_NAME = "four";
     public static final String GAME_FOUR_CODE = "4";
     public static final String GAME_FOUR_CODE = "4";
+    public static final String GAME_FIVE_NAME = "five";
+    public static final String GAME_FIVE_CODE = "5";
+
 
 
     public static final String WS_GAME_ONE_URL = "ws://47.115.49.14:8000/ws";
     public static final String WS_GAME_ONE_URL = "ws://47.115.49.14:8000/ws";
     public static final String WS_GAME_ONE_REMARK_URL = "ws://47.238.102.31:8000/ws";
     public static final String WS_GAME_ONE_REMARK_URL = "ws://47.238.102.31:8000/ws";
@@ -23,6 +26,8 @@ public class Common {
     public static final String WS_GAME_THREES_REMARK_URL = "ws://120.76.250.125:8000/ws";
     public static final String WS_GAME_THREES_REMARK_URL = "ws://120.76.250.125:8000/ws";
     public static final String WS_GAME_FOUR_URL = "ws://47.115.38.182:8000/ws";
     public static final String WS_GAME_FOUR_URL = "ws://47.115.38.182:8000/ws";
     public static final String WS_GAME_FOUR_REMARK_URL = "ws://47.115.38.182:8000/ws";
     public static final String WS_GAME_FOUR_REMARK_URL = "ws://47.115.38.182:8000/ws";
+    public static final String WS_GAME_FIVE_URL = "ws://47.115.38.182:8000/ws";
+    public static final String WS_GAME_FIVE_REMARK_URL = "ws://47.115.38.182:8000/ws";
 
 
     public static Map<String, Session> sessionMap = new HashMap<>();
     public static Map<String, Session> sessionMap = new HashMap<>();
 }
 }

+ 51 - 49
game-business/src/main/java/com/game/business/websocket/WebSocketConnent.java

@@ -4,10 +4,7 @@ package com.game.business.websocket;
 import com.game.business.config.GameTwoConfig;
 import com.game.business.config.GameTwoConfig;
 import com.game.business.config.SpringContextSetting;
 import com.game.business.config.SpringContextSetting;
 import com.game.business.util.Common;
 import com.game.business.util.Common;
-import com.game.business.websocket.client.GameFourClient;
-import com.game.business.websocket.client.GameOneClient;
-import com.game.business.websocket.client.GameThreesClient;
-import com.game.business.websocket.client.GameTwoClient;
+import com.game.business.websocket.client.*;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 
 
 import javax.websocket.ContainerProvider;
 import javax.websocket.ContainerProvider;
@@ -31,50 +28,55 @@ public class WebSocketConnent {
     }
     }
 
 
     public void connect() throws Exception{
     public void connect() throws Exception{
-       /* while (true){
-            try {
-                System.out.println("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");
-
-                if(StringUtils.isBlank(WS_NOW_URL)){
-                    WS_NOW_URL = WS_URL;
-                }else{
-                    if(WS_NOW_URL.equals(WS_URL)){
-                        WS_NOW_URL = WS_REMARK_URL;
-                    }else{
-                        WS_NOW_URL = WS_URL;
-                    }
-                }
-
-                if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
-                    GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
-                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
-                    GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
-                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
-                    GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
-                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
-                    GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
-                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_URL + "]");
-                this.WS_CONNENT_NUM = 0;
-                break;
-            }catch (Exception e){
-                if(this.WS_CONNENT_MAX_NUM > this.WS_CONNENT_NUM){
-                    this.WS_CONNENT_NUM ++;
-                }
-                System.out.println("连接[" + this.WS_URL_NAME + "]异常," + this.WS_CONNENT_NUM + "秒后尝试重新连接:" + e.getMessage());
-                Thread.sleep(this.WS_CONNENT_NUM * 1000);
-            }
-        }*/
+//        while (true){
+//            try {
+//                System.out.println("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");
+//
+//                if(StringUtils.isBlank(WS_NOW_URL)){
+//                    WS_NOW_URL = WS_URL;
+//                }else{
+//                    if(WS_NOW_URL.equals(WS_URL)){
+//                        WS_NOW_URL = WS_REMARK_URL;
+//                    }else{
+//                        WS_NOW_URL = WS_URL;
+//                    }
+//                }
+//
+//                if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
+//                    GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
+//                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+//                }
+//
+//                if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
+//                    GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
+//                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+//                }
+//
+//                if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
+//                    GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
+//                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+//                }
+//
+//                if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
+//                    GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
+//                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+//                }
+//
+//                if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
+//                    GameFiveClient client = SpringContextSetting.getBean(GameFiveClient.class);
+//                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+//                }
+//
+//                System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_URL + "]");
+//                this.WS_CONNENT_NUM = 0;
+//                break;
+//            }catch (Exception e){
+//                if(this.WS_CONNENT_MAX_NUM > this.WS_CONNENT_NUM){
+//                    this.WS_CONNENT_NUM ++;
+//                }
+//                System.out.println("连接[" + this.WS_URL_NAME + "]异常," + this.WS_CONNENT_NUM + "秒后尝试重新连接:" + e.getMessage());
+//                Thread.sleep(this.WS_CONNENT_NUM * 1000);
+//            }
+//        }
     }
     }
 }
 }

+ 71 - 0
game-business/src/main/java/com/game/business/websocket/client/GameFiveClient.java

@@ -0,0 +1,71 @@
+package com.game.business.websocket.client;
+
+import com.game.business.config.GameFiveConfig;
+import com.game.business.config.GameFourConfig;
+import com.game.business.task.AppGameBettingTask;
+import com.game.business.util.Common;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import java.util.Date;
+
+
+@Component
+@ClientEndpoint
+public class GameFiveClient {
+
+    @Autowired
+    private AppGameBettingTask appGameBettingTask;
+
+    @OnOpen
+    public void onOpen(Session session) throws Exception{
+        System.out.println("game five 游戏已连接 server");
+        session.getBasicRemote().sendText("ping");
+        Common.sessionMap.put(Common.GAME_FIVE_CODE, session);
+    }
+
+    @OnClose
+    public void onClose(Session session) throws Exception{
+        System.out.println("game five 游戏已断开 server:" + new Date());
+        connect();
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) throws Exception{
+        System.out.println("game five 连接异常 [" + throwable.getMessage() + "]");
+        connect();
+    }
+
+    public void connect(){
+        synchronized (this){
+            try {
+                GameFiveConfig.webSocketConnent.connect();
+            }catch (Exception e){
+                System.out.println("连接 game five [socket] 异常" + e.getMessage());
+            }
+        }
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message){
+        try {
+            if(StringUtils.isBlank(message)){
+                System.out.println("game five 数据为空");
+                return;
+            }
+
+            System.out.println("game five 接收数据" + message);
+
+            if(message.equals("pong")){
+                return;
+            }
+
+            appGameBettingTask.gameDataTask(message, Common.GAME_FIVE_CODE);
+        }catch (Exception e){
+            e.printStackTrace();
+            System.out.println("game five 接收数据异常[" + e.getMessage() + "]");
+        }
+    }
+}