소스 검색

超能运动会

kk 7 달 전
부모
커밋
71e0889eee

+ 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();
+    }
+}

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

@@ -14,6 +14,8 @@ public class Common {
     public static final String GAME_THREES_CODE = "3";
     public static final String GAME_FOUR_NAME = "four";
     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://114.132.100.104:8000/ws";
     public static final String WS_GAME_ONE_REMARK_URL = "ws://114.132.100.104:8000/ws";
@@ -23,6 +25,8 @@ public class Common {
     public static final String WS_GAME_THREES_REMARK_URL = "ws://43.154.162.11:8000/ws";
     public static final String WS_GAME_FOUR_URL = "ws://43.154.162.11:8000/ws";
     public static final String WS_GAME_FOUR_REMARK_URL = "ws://43.154.162.11:8000/ws";
+    public static final String WS_GAME_FIVE_URL = "ws://43.154.162.11:8000/ws";
+    public static final String WS_GAME_FIVE_REMARK_URL = "ws://43.154.162.11:8000/ws";
 
     public static Map<String, Session> sessionMap = new HashMap<>();
 }

+ 6 - 4
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.SpringContextSetting;
 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 javax.websocket.ContainerProvider;
@@ -65,6 +62,11 @@ public class WebSocketConnent {
                     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;

+ 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() + "]");
+        }
+    }
+}