Bladeren bron

游戏Seven

kk 1 maand geleden
bovenliggende
commit
286ec4de0a

+ 10 - 0
game-business/src/main/java/com/game/business/task/WebSokcetTask.java

@@ -80,5 +80,15 @@ public class WebSokcetTask {
                 session.getBasicRemote().sendText("ping");
             }
         }
+
+        if(Common.sessionMap.containsKey(Common.GAME_SEVEN_CODE)){
+            Session session = Common.sessionMap.get(Common.GAME_SEVEN_CODE);
+            if(!session.isOpen()){
+                Common.sessionMap.remove(Common.GAME_SEVEN_CODE);
+                session.close();
+            }else{
+                session.getBasicRemote().sendText("ping");
+            }
+        }
     }
 }

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

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

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