WebSocketConnent.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.game.business.websocket;
  2. import com.game.business.config.GameTwoConfig;
  3. import com.game.business.config.SpringContextSetting;
  4. import com.game.business.util.Common;
  5. import com.game.business.websocket.client.*;
  6. import org.apache.commons.lang3.StringUtils;
  7. import javax.websocket.ContainerProvider;
  8. import java.net.URI;
  9. public class WebSocketConnent {
  10. private String WS_URL;
  11. private String WS_REMARK_URL;
  12. private String WS_NOW_URL;
  13. private String WS_URL_NAME;
  14. private int WS_CONNENT_NUM = 0;
  15. private int WS_CONNENT_MAX_NUM = 10;
  16. public WebSocketConnent(String WS_URL, String WS_REMARK_URL, String WS_URL_NAME){
  17. this.WS_URL = WS_URL;
  18. this.WS_REMARK_URL = WS_REMARK_URL;
  19. this.WS_URL_NAME = WS_URL_NAME;
  20. }
  21. public void connect() throws Exception{
  22. /*while (true){
  23. try {
  24. System.out.println("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");
  25. if(StringUtils.isBlank(WS_NOW_URL)){
  26. WS_NOW_URL = WS_URL;
  27. }else{
  28. if(WS_NOW_URL.equals(WS_URL)){
  29. WS_NOW_URL = WS_REMARK_URL;
  30. }else{
  31. WS_NOW_URL = WS_URL;
  32. }
  33. }
  34. if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
  35. GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
  36. ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
  37. }
  38. if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
  39. GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
  40. ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
  41. }
  42. if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
  43. GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
  44. ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
  45. }
  46. if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
  47. GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
  48. ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
  49. }
  50. if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
  51. GameFiveClient client = SpringContextSetting.getBean(GameFiveClient.class);
  52. ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
  53. }
  54. System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_URL + "]");
  55. this.WS_CONNENT_NUM = 0;
  56. break;
  57. }catch (Exception e){
  58. if(this.WS_CONNENT_MAX_NUM > this.WS_CONNENT_NUM){
  59. this.WS_CONNENT_NUM ++;
  60. }
  61. System.out.println("连接[" + this.WS_URL_NAME + "]异常," + this.WS_CONNENT_NUM + "秒后尝试重新连接:" + e.getMessage());
  62. Thread.sleep(this.WS_CONNENT_NUM * 1000);
  63. }
  64. }*/
  65. }
  66. }