GameSevenClient.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.game.business.websocket.client;
  2. import com.game.business.config.GameSevenConfig;
  3. import com.game.business.config.GameSixConfig;
  4. import com.game.business.task.AppGameBettingTask;
  5. import com.game.business.util.Common;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import javax.websocket.*;
  10. import java.util.Date;
  11. @Component
  12. @ClientEndpoint
  13. public class GameSevenClient {
  14. @Autowired
  15. private AppGameBettingTask appGameBettingTask;
  16. @OnOpen
  17. public void onOpen(Session session) throws Exception{
  18. System.out.println("game seven 游戏已连接 server");
  19. session.getBasicRemote().sendText("ping");
  20. Common.sessionMap.put(Common.GAME_SIX_CODE, session);
  21. }
  22. @OnClose
  23. public void onClose(Session session) throws Exception{
  24. System.out.println("game seven 游戏已断开 server:" + new Date());
  25. connect();
  26. }
  27. @OnError
  28. public void onError(Session session, Throwable throwable) throws Exception{
  29. System.out.println("game seven 连接异常 [" + throwable.getMessage() + "]");
  30. connect();
  31. }
  32. public void connect(){
  33. synchronized (this){
  34. try {
  35. GameSevenConfig.webSocketConnent.connect();
  36. }catch (Exception e){
  37. System.out.println("连接 game seven [socket] 异常" + e.getMessage());
  38. }
  39. }
  40. }
  41. @OnMessage
  42. public void onMessage(Session session, String message){
  43. try {
  44. if(StringUtils.isBlank(message)){
  45. System.out.println("game seven 数据为空");
  46. return;
  47. }
  48. System.out.println("game seven 接收数据" + message);
  49. if(message.equals("pong")){
  50. return;
  51. }
  52. appGameBettingTask.gameDataTask(message, Common.GAME_SEVEN_CODE);
  53. }catch (Exception e){
  54. e.printStackTrace();
  55. System.out.println("game seven 接收数据异常[" + e.getMessage() + "]");
  56. }
  57. }
  58. }