Browse Source

websocket

kk 9 months ago
parent
commit
ef2748db38

+ 5 - 0
game-admin/pom.xml

@@ -37,6 +37,11 @@
             <version>1.6.2</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
+
          <!-- Mysql驱动包 -->
         <dependency>
             <groupId>mysql</groupId>

+ 22 - 0
game-admin/src/main/java/com/game/web/core/config/GameOneConfig.java

@@ -0,0 +1,22 @@
+package com.game.web.core.config;
+
+import com.game.web.util.Common;
+import com.game.web.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 GameOneConfig implements CommandLineRunner {
+
+    public static WebSocketConnent webSocketConnent;
+
+    @Override
+    @Async
+    public void run(String... args) throws Exception {
+        webSocketConnent = new WebSocketConnent(Common.WS_GAME_URL, Common.GANME_ONE_NAME);
+        webSocketConnent.connect();
+    }
+}

+ 25 - 0
game-admin/src/main/java/com/game/web/core/config/SpringContextSetting.java

@@ -0,0 +1,25 @@
+package com.game.web.core.config;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+/**
+ * spring 上下文工具
+ */
+
+@Component
+public class SpringContextSetting implements ApplicationContextAware {
+
+    public static  ApplicationContext applicationContext;
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        SpringContextSetting.applicationContext = applicationContext;
+    }
+
+    public static <T> T getBean(Class<T> clazz) {
+        return applicationContext.getBean(clazz);
+    }
+}

+ 22 - 0
game-admin/src/main/java/com/game/web/core/config/WebSocketConfig.java

@@ -0,0 +1,22 @@
+package com.game.web.core.config;
+
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+public class WebSocketConfig {
+
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter(){
+        return new ServerEndpointExporter();
+    }
+
+    @Bean
+    public RestTemplate restTemplate(RestTemplateBuilder builder){
+        return builder.build();
+    }
+
+}

+ 11 - 0
game-admin/src/main/java/com/game/web/util/Common.java

@@ -0,0 +1,11 @@
+package com.game.web.util;
+
+public class Common {
+
+    public static final String GANME_ONE_NAME = "";
+    public static final String GAME_ONE_CODE = "";
+    public static final String GANME_TWO_NAME = "";
+    public static final String GANME_THREES_NAME = "";
+
+    public static final String WS_GAME_URL = "";
+}

+ 43 - 0
game-admin/src/main/java/com/game/web/websocket/WebSocketConnent.java

@@ -0,0 +1,43 @@
+package com.game.web.websocket;
+
+import com.game.web.core.config.SpringContextSetting;
+import com.game.web.util.Common;
+import com.game.web.websocket.client.GameOneClient;
+
+import javax.websocket.ContainerProvider;
+import java.net.URI;
+
+public class WebSocketConnent {
+
+    private String WS_URL;
+    private String WS_URL_NAME;
+    private int WS_CONNENT_NUM = 0;
+    private int WS_CONNENT_MAX_NUM = 10;
+
+    public WebSocketConnent(String WS_URL, String WS_URL_NAME){
+        this.WS_URL = WS_URL;
+        this.WS_URL_NAME = WS_URL_NAME;
+    }
+
+    public void connect() throws Exception{
+        while (true){
+            try {
+                System.out.printf("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");
+                if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
+                    GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
+                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_URL));
+                }
+
+                System.out.printf(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_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.printf("连接[" + this.WS_URL_NAME + "]异常," + this.WS_CONNENT_NUM + "秒后尝试重新连接:" + e.getMessage());
+                Thread.sleep(this.WS_CONNENT_NUM * 1000);
+            }
+        }
+    }
+}

+ 68 - 0
game-admin/src/main/java/com/game/web/websocket/client/GameOneClient.java

@@ -0,0 +1,68 @@
+package com.game.web.websocket.client;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.game.web.core.config.GameOneConfig;
+import com.game.web.util.Common;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+
+@Component
+@ClientEndpoint
+public class GameOneClient {
+
+    @OnOpen
+    public void onOpen(Session session) throws Exception{
+        System.out.printf("game one 游戏已连接 server");
+        // 发送游戏编码
+        session.getBasicRemote().sendText("{\"code\":\"" + Common.GAME_ONE_CODE + "\"}");
+    }
+
+    @OnClose
+    public void onClose(Session session) throws Exception{
+        System.out.printf("game on 游戏已断开 server");
+        connect();
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) throws Exception{
+        System.out.printf("game one 连接异常 [" + throwable.getMessage() + "]");
+        connect();
+    }
+
+    public void connect(){
+        synchronized (this){
+            try {
+                GameOneConfig.webSocketConnent.connect();
+            }catch (Exception e){
+                System.out.printf("连接 game on [socket] 异常" + e.getMessage());
+            }
+        }
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message){
+        try {
+            if(StringUtils.isBlank(message)){
+                System.out.printf("game one 数据为空");
+                return;
+            }
+
+            System.out.printf("game one 接收数据[" + message + "]");
+
+            JSONObject jsonObject = JSONObject.parseObject(message);
+            String code = jsonObject.getString("code");
+
+            if(StringUtils.isBlank(code) || !code.equals(Common.GAME_ONE_CODE)){
+                System.out.printf("game one 接收数据错误[" + message + "]");
+                return;
+            }
+
+            // 数据入库
+        }catch (Exception e){
+            e.printStackTrace();
+            System.out.printf("game one 接收数据一场[" + e.getMessage() + "]");
+        }
+    }
+}