Bladeren bron

游戏SIX

kk 2 maanden geleden
bovenliggende
commit
d60529f220

+ 22 - 0
game-business/src/main/java/com/game/business/config/GameSixConfig.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 GameSixConfig implements CommandLineRunner {
+
+    public static WebSocketConnent webSocketConnent;
+
+    @Override
+    @Async
+    public void run(String... args) throws Exception {
+        webSocketConnent = new WebSocketConnent(Common.WS_GAME_SIX_URL, Common.WS_GAME_SIX_REMARK_URL, Common.GAME_SIX_NAME);
+        webSocketConnent.connect();
+    }
+}

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

@@ -17,6 +17,9 @@ public class Common {
     public static final String GAME_FIVE_NAME = "five";
     public static final String GAME_FIVE_CODE = "5";
 
+    public static final String GAME_SIX_NAME = "six";
+    public static final String GAME_SIX_CODE = "6";
+
 
 //    public static final String WS_GAME_ONE_URL = "ws://47.115.59.17:8000/ws";
 //    public static final String WS_GAME_ONE_REMARK_URL = "ws://47.115.59.17:8000/ws";
@@ -45,5 +48,8 @@ public class Common {
     public static final String WS_GAME_FIVE_URL = "ws://175.178.245.120:8000/ws";
     public static final String WS_GAME_FIVE_REMARK_URL = "ws://119.29.4.157:8000/ws";
 
+    public static final String WS_GAME_SIX_URL = "ws://120.79.41.13:8000/ws";
+    public static final String WS_GAME_SIX_REMARK_URL = "ws://120.79.41.13:8000/ws";
+
     public static Map<String, Session> sessionMap = new HashMap<>();
 }

+ 5 - 0
game-business/src/main/java/com/game/business/websocket/WebSocketConnent.java

@@ -74,6 +74,11 @@ public class WebSocketConnent {
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                 }
 
+                if(Common.GAME_SIX_NAME.equals(this.WS_URL_NAME)){
+                    GameSixClient client = SpringContextSetting.getBean(GameSixClient.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;

+ 70 - 0
game-business/src/main/java/com/game/business/websocket/client/GameSixClient.java

@@ -0,0 +1,70 @@
+package com.game.business.websocket.client;
+
+import com.game.business.config.GameSixConfig;
+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 GameSixClient {
+
+    @Autowired
+    private AppGameBettingTask appGameBettingTask;
+
+    @OnOpen
+    public void onOpen(Session session) throws Exception{
+        System.out.println("game six 游戏已连接 server");
+        session.getBasicRemote().sendText("ping");
+        Common.sessionMap.put(Common.GAME_SIX_CODE, session);
+    }
+
+    @OnClose
+    public void onClose(Session session) throws Exception{
+        System.out.println("game six 游戏已断开 server:" + new Date());
+        connect();
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) throws Exception{
+        System.out.println("game six 连接异常 [" + throwable.getMessage() + "]");
+        connect();
+    }
+
+    public void connect(){
+        synchronized (this){
+            try {
+                GameSixConfig.webSocketConnent.connect();
+            }catch (Exception e){
+                System.out.println("连接 game six [socket] 异常" + e.getMessage());
+            }
+        }
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message){
+        try {
+            if(StringUtils.isBlank(message)){
+                System.out.println("game six 数据为空");
+                return;
+            }
+
+            System.out.println("game six 接收数据" + message);
+
+            if(message.equals("pong")){
+                return;
+            }
+
+            appGameBettingTask.gameDataTask(message, Common.GAME_SIX_CODE);
+        }catch (Exception e){
+            e.printStackTrace();
+            System.out.println("game six 接收数据异常[" + e.getMessage() + "]");
+        }
+    }
+}