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