这是处理WebSocket消息的类
package com.hy.phoneServer.common;
import com.alibaba.fastjson.JSONObject;
import com.hy.phoneServer.client.MyWebSocketClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static com.hy.phoneServer.common.CommonClass.handleStringNull;
import static com.hy.phoneServer.common.TokenValidate.getLYToken;
@Component
public class WebSocketAPI extends BinaryWebSocketHandler {
//
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private static Boolean Flag = false;//标记,用来判断对方是否接通电话
// 可以通过ApplicationContext来读取配置文件并创建Bean实例
// private static ApplicationContext context;
// @Autowired
// public void setApplicationContext(ApplicationContext context){
// WebSocketAPI.context = context;
// }
private static MyWebSocketClient webSocketClient;
@Autowired
public void setMyWebSocketClient(MyWebSocketClient webSocketClient){
WebSocketAPI.webSocketClient = webSocketClient;
}
private static UrlUtils urlUtils;
@Autowired
public void setUrlUtils(UrlUtils urlUtils){
WebSocketAPI.urlUtils = urlUtils;
}
public static String messgag;
private Logger logger = LoggerFactory.getLogger(Logger.class);
@Override //该方法会在websocket连接成功后被调用
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//WebSocketSession是websocket连接对应的会话
logger.info("WebSocket建立连接了");
Flag = false;
}
@Override //该方法是在websocket收到消息的时候自动调用
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
//message 为收到的消息
byte[] array = message.getPayload().array();
String utf8String = new String(array, StandardCharsets.UTF_8); // 将二进制数据转换为UTF-8字符串
logger.info("WebSocket接受消息:"+utf8String);
}
@Override //该方法是在websocket连接出现异常的时候自动调用的
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
//exception记录了异常信息
errResult();
logger.info("WebSocket连接出现异常了");
//重新连接
// webSocketClient.connect();
Flag = false;
}
@Override //该方法是在websocket连接关闭后自动调用的
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//status 为关闭的状态
CacheManager instance = CacheManager.getInstance();
logger.info(handleStringNull(instance.get("IsCheck")));
errResult();
logger.info(status.toString());
logger.info("webSocketClient地址"+webSocketClient.toString());
MyWebSocketClient myWebSocketClient = new MyWebSocketClient();
myWebSocketClient.connect();
logger.info("WebSocket连接关闭了");
//重新连接
// webSocketClient.connect();这里猜测webSocketClient类是单例,不能从容器去获取这个对象
Flag = false;
}
public String getMessgag() {
return messgag;
}
}
这是连接webSocket的类:
@Component
public class MyWebSocketClient {
/**
* 录音设别地址
*/
static String phoneUrl;
@Value("${common.phoneUrl}")
public void setPhoneUrl(String s){
phoneUrl = s;
}
private Logger logger = LoggerFactory.getLogger(Logger.class);
private WebSocketSession session;
public void connect() {
Map<String, Object> lyToken = getLYToken();
String lytoken = handleStringNull(lyToken.get("Token"));
logger.info("WebSocket客户端进行连接,获取token:"+lytoken);
WebSocketClient client = new StandardWebSocketClient();
try {
session = client.doHandshake(new WebSocketAPI(), "url"+lytoken).get();
boolean open = session.isOpen();
logger.info("当前连接是否成功2:"+open);
// Timer t = new Timer();
// t.scheduleAtFixedRate(new TimerTask() {
// @Override
// public void run() {
// logger.info("进入定时器,当前连接是否成功:"+open);
// if(!open){
// logger.info("断线重连");
// connect();
// }
// }
// },1000,5000);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
我在本地运行的时候,如果进入webSocket连接关闭方法, webSocketClient.connect();重新连接成功,
但是我发布到tomcat的时候, webSocketClient.connect();就不生效了