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