1、 定义进度条DTO
@Data
public class ProcessBarDto {
private Double percent;
private Double max;
private Double min;
private String bgColor;
private String unit;
private Boolean isDrag;
private String processBarMsg;
private String robotNo;
}
2、 建立连接 创建会话ID (RobotNO)
@Component
@Slf4j
@ServerEndpoint(value = "/WSServer/{robotNo}",
encoders = {MessageEncoder.class},
decoders = {MessageDecoder.class})
public class WebSocketServer implements IWebSocketServer {
@OnOpen
public void onOpen(Session session, @PathParam("robotNo") String robotNo) {
this.robotNo = robotNo;
this.session = session;
if (webSocketMap.containsKey(robotNo)) {
webSocketMap.remove(robotNo);
webSocketMap.put(robotNo, this);
} else {
webSocketMap.put(robotNo, this);
}
}
}
3、 后端处理,向会话ID(推送处理进度)
progressBar.progressBarSendMsg(processBar,robotNo )
4、 前端监听会话消息展示进度条内容
@Component
@Slf4j
@ServerEndpoint(value = "/WSServer/{robotNo}",
encoders = {MessageEncoder.class},
decoders = {MessageDecoder.class})
public class WebSocketServer implements IWebSocketServer {
@OnMessage(maxMessageSize = 3000000)
public void onMessage(WebSocketMsgDto message, Session session) {
long start = System.currentTimeMillis();
log.info("客户端:" + robotNo + ",报文:" + GsonUtil.gson.toJson(message));
try {
if (message == null) {
throw new RuntimeException("消息为空");
}
} catch (Exception e) {
e.printStackTrace();
}
}