websocket推送进度条百分比给前台
2021-03-26 16:26
标签:rest ppi 组件 ping 错误 charset map end arrays 说明:后台springboot项目 前台vue+element-UI 直接放代码: @Component private Session session; private static CopyOnWriteArraySet @OnOpen /** @OnMessage /** } @RestController 进度条进度数据 效果如下 websocket推送进度条百分比给前台 标签:rest ppi 组件 ping 错误 charset map end arrays 原文地址:https://www.cnblogs.com/xuchao0506/p/12638521.html//后台代码
@ServerEndpoint("/websocket")
public class WebSocket {
private String msg = "0";
public void onOpen(Session session) {
this.session = session;
webSockets.add(this);
sendAllMessage(msg);
}
* 关闭调用方法
*/
@OnClose
public void onClose() {
webSockets.remove(this);
}
public void onMessage(String msg) {
}
* 消息广播到前台
*
* @param msg
*/
public void sendAllMessage(String msg) {
for (WebSocket webSocket : webSockets) {
try {
webSocket.session.getBasicRemote().sendText(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}//前台vue
//前台js mounted是vue用来初始化的 methods里定义的是方法//后台调用推送数据
public class ExportTxt {
@Autowired
private WebSocket websocket;
@RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public void test(){
String msg = "";
int a = 0;
for(int i=0;i msg = String.valueOf(a);
websocket.sendAllMessage(msg);
a=a+20;
}
}
}
下一篇:css技巧-清明追思-网站置灰
文章标题:websocket推送进度条百分比给前台
文章链接:http://soscw.com/index.php/essay/68195.html