提交 785077ca authored 作者: 张航's avatar 张航

修复了Socket连接占用的问题(数据只能一条一条发送,不能两条同时发送)

上级 8b147486
......@@ -82,22 +82,19 @@ public class SocketResultData implements Serializable {
//public static void main(String[] args) throws InterruptedException {
//
// Timer timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println("timer start");
// }
// }, 0, 1000);
// Thread.sleep(1000);
// timer.cancel();
// timer.cancel();
// timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println("after cancel");
// }
// }, 1000, 2000);
// testTask("Task 1");
// testTask("Task 2");
//}
private static void testTask(String name) throws InterruptedException {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(name);
}
}, 0, 500);
Thread.sleep(2000);
timer.cancel();
}
}
......@@ -154,7 +154,7 @@ public class DownMapService {
System.out.println(result);
}
// 完成标志
this.finished = true;
DownMapService.finished = true;
// 结束定时器
speedTimer.cancel();
scheduleTimer.cancel();
......
......@@ -170,21 +170,6 @@ public class DownLoadMapWebSocket {
private void downLoadMap(SocketResultData socketData, Session session) throws Exception {
// 创建定时任务向前端发送速度\进度
speedTimer = new Timer();
// 每隔三秒汇报一次
speedTimer.schedule(new TimerTask() {
@Override
public void run() {
// 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) {
speedTimer.purge();
}else {
// 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
AppointSending(JSONUtil.toJsonStr(speed));
}
}
}, 1000, 1000);
// 创建定时任务向前端发送进度
scheduleTimer = new Timer();
// 每隔0.2秒汇报一次
......@@ -203,7 +188,23 @@ public class DownLoadMapWebSocket {
AppointSending(JSONUtil.toJsonStr(schedule));
}
}
}, 200, 200);
}, 1000, 1000);
speedTimer = new Timer();
// 每隔三秒汇报一次
speedTimer.schedule(new TimerTask() {
@Override
public void run() {
// 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) {
speedTimer.purge();
}else {
// 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
AppointSending(JSONUtil.toJsonStr(speed));
}
}
}, 2000, 2000);
// 发送执行结果(这里是异步阻塞的,会等待downLoad执行完成)
Map<String, Object> resBody = downMapService.downLoad(socketData.getBody(), session);
......@@ -211,6 +212,7 @@ public class DownLoadMapWebSocket {
",总用时" + resBody.get("totalTime") + ",失败个数" + resBody.get("falidNum") + "(可选择非覆盖重新下载!)";
SocketResultData resultData = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, msg, null);
AppointSending(JSONUtil.toJsonStr(resultData));
DownMapService.isBusy = false;
// 通知下载完成
AppointSending(JSONUtil.toJsonStr(new SocketResultData(OperationTypeEnum.DOWNLOAD_FINISHED)));
}
......@@ -235,8 +237,10 @@ public class DownLoadMapWebSocket {
**/
private void AppointSending(String message) {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
synchronized (session){
session.getBasicRemote().sendText(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
......
......@@ -7,6 +7,6 @@ http.connectTimeout=60000
#从连接池中获取到连接的最长时间
http.connectionRequestTimeout=6000000
#数据传输的最长时间
http.socketTimeout=12000000
http.socketTimeout=1200000
#提交请求前测试连接是否可用
http.staleConnectionCheckEnabled=true
\ No newline at end of file
......@@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>EM MapDownLoader</title>
<title>GM MapDownLoader</title>
<!-- jquery -->
<script src="thirdLib/jq/jquery-3.3.1.min.js"></script>
......@@ -135,15 +135,16 @@
if (result.type == 3) { // 更新下载进度
var schedule = result.message;
console.log(schedule);
if (schedule > 100){
schedule = 100;
schedule = 99;
}
console.log(schedule);
$("#progressBar").width(schedule + "%");
$("#progressBar").html(schedule + "%");
}
if (result.type == 4) { // 下载完成,隐藏进度条
// $("#progressDiv").hide();
$("#progressBar").width(100 + "%");
$("#progressBar").html(100 + "%");
}
if (result.type == 6) { // 连接信息
$("#filePath").val(result.message);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论