12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.game.business.websocket;
- import com.game.business.config.GameTwoConfig;
- import com.game.business.config.SpringContextSetting;
- import com.game.business.util.Common;
- import com.game.business.websocket.client.*;
- import org.apache.commons.lang3.StringUtils;
- import javax.websocket.ContainerProvider;
- import java.net.URI;
- public class WebSocketConnent {
- private String WS_URL;
- private String WS_REMARK_URL;
- private String WS_NOW_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_REMARK_URL, String WS_URL_NAME){
- this.WS_URL = WS_URL;
- this.WS_REMARK_URL = WS_REMARK_URL;
- this.WS_URL_NAME = WS_URL_NAME;
- }
- public void connect() throws Exception{
- /*while (true){
- try {
- System.out.println("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");
- if(StringUtils.isBlank(WS_NOW_URL)){
- WS_NOW_URL = WS_URL;
- }else{
- if(WS_NOW_URL.equals(WS_URL)){
- WS_NOW_URL = WS_REMARK_URL;
- }else{
- WS_NOW_URL = WS_URL;
- }
- }
- if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
- GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
- ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
- }
- if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
- GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
- ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
- }
- if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
- GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
- ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
- }
- if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
- GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
- ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
- }
- if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
- GameFiveClient client = SpringContextSetting.getBean(GameFiveClient.class);
- ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
- }
- System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_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.println("连接[" + this.WS_URL_NAME + "]异常," + this.WS_CONNENT_NUM + "秒后尝试重新连接:" + e.getMessage());
- Thread.sleep(this.WS_CONNENT_NUM * 1000);
- }
- }*/
- }
- }
|