You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.4 KiB
Java
125 lines
3.4 KiB
Java
package com.glxp.api.service.sync;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.glxp.api.common.res.BaseResponse;
|
|
import com.glxp.api.constant.SocketMsgType;
|
|
import com.glxp.api.entity.sync.SocketMsgEntity;
|
|
import org.java_websocket.WebSocket;
|
|
import org.java_websocket.client.WebSocketClient;
|
|
import org.java_websocket.framing.Framedata;
|
|
import org.java_websocket.handshake.ServerHandshake;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
|
|
public class SpsWebSocketClient extends WebSocketClient {
|
|
|
|
private String excptMessage;
|
|
|
|
Logger log = LoggerFactory.getLogger(SpsWebSocketClient.class);
|
|
|
|
private static List<SpsWebSocketClient> list = new ArrayList<>();
|
|
SocketMsgService socketMsgService;
|
|
|
|
public void setSocketMsgService(SocketMsgService socketMsgService) {
|
|
this.socketMsgService = socketMsgService;
|
|
}
|
|
|
|
public SpsWebSocketClient(String serverUri) throws URISyntaxException {
|
|
super(new URI(serverUri));
|
|
this.setConnectionLostTimeout(0);
|
|
if (list.isEmpty()) {
|
|
return;
|
|
}
|
|
for (SpsWebSocketClient client : list) {
|
|
client.close();
|
|
}
|
|
list.clear();
|
|
list.add(this);
|
|
}
|
|
|
|
@Override
|
|
public void onOpen(ServerHandshake serverHandshake) {
|
|
log.info("在线日志socket连接成功");
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onMessage(String s) {
|
|
log.info("收到消息:" + s);
|
|
if (StrUtil.isNotEmpty(s)) {
|
|
SocketMsgEntity socketMsgEntity = JSONObject.parseObject(s, SocketMsgEntity.class);
|
|
//收到更新下载数据则赋值由外部处理,多个任务只处理一次避免重复处理
|
|
if (socketMsgEntity.getType().equals(SocketMsgType.DL_ALL_DATA)) {
|
|
this.excptMessage = s;
|
|
}else if (socketMsgEntity.getType().equals(SocketMsgType.STAT_DATA)){
|
|
BaseResponse statData = socketMsgService.getStatData(s);
|
|
this.sendMessage(statData.toString());
|
|
} else {
|
|
//通知类消息则需转发
|
|
socketMsgService.dealNoticeMsg(socketMsgEntity);
|
|
}
|
|
} else {
|
|
log.error("无法识别消息!");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onClose(int i, String s, boolean b) {
|
|
log.info("在线日志socket断开");
|
|
}
|
|
|
|
@Override
|
|
public void onError(Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
@Override
|
|
public void onWebsocketPing(WebSocket conn, Framedata f) {
|
|
try {
|
|
Thread.sleep(1000 * 5);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
send("---pong---");
|
|
}
|
|
|
|
@Deprecated
|
|
public static void destroy() {
|
|
if (list.isEmpty())
|
|
return;
|
|
for (SpsWebSocketClient client : list) {
|
|
client.close();
|
|
}
|
|
list.clear();
|
|
|
|
}
|
|
|
|
//发送消息
|
|
public void sendMessage(String message) {
|
|
this.send(message);
|
|
System.out.println("已发送消息:" + message);
|
|
}
|
|
|
|
//获取接收到的信息
|
|
public String getExcptMessage() {
|
|
if (excptMessage != null) {
|
|
String message = new String(excptMessage);
|
|
excptMessage = null;
|
|
return message;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
}
|
|
|