gpt4 book ai didi

java - 从网络服务器到客户端的数据传输速率和数据量

转载 作者:行者123 更新时间:2023-12-01 15:07:41 25 4
gpt4 key购买 nike

我正在编写一个在 tomcat 上运行的网络应用程序。我需要传输大约 25 kb 的数据(每 20 毫秒从服务器到客户端传输所有 double /长整型值。)尝试为此使用服务器发送的事件,因为我只需要从服务器到客户端的一种单向通信 channel 。

在服务器端使用java,在客户端使用java脚本。

请提出我在这里所说的是否有意义以及是否可以实现的建议。

我是网络开发新手。

最佳答案

你为什么不自己尝试一下 - 你会发现本地主机和实际网络之间有很大的区别:

Servlet(生成 25kb 大消息):

import java.io.IOException;
import java.io.Writer;
import java.util.Random;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "EventServlet", urlPatterns = {"/EventServlet"}, asyncSupported = true)
public class EventServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/event-stream; charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Transfer-Encoding", "Chunked");
response.flushBuffer();
final AsyncContext ctx = request.startAsync();
ctx.setTimeout(0);

ctx.start(new Runnable() {
@Override
public void run() {

try {

Writer out = ctx.getResponse().getWriter();
for (;;) {
out.write("event: simpleEvent\n");
out.write("data:" + randomString(25000) + "\n\n");
ctx.getResponse().flushBuffer();
}

} catch (Exception e) {
e.printStackTrace();
} finally {
ctx.complete();
}

}
});

}
private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static Random rnd = new Random();

public static String randomString(int len) {
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
sb.append(AB.charAt(rnd.nextInt(AB.length())));
}
return sb.toString();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}
}

索引.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<div>Number of Messages</div>
<div id="out">

</div>
<div>Messages per Second:</div>

<div id="mean">

</div>

</body>

<script type="text/javascript">

var startTime = new Date().getTime();
var counter = 0;

var eventSource = new EventSource("EventServlet");

eventSource.addEventListener('simpleEvent', function(event){
counter++;
document.getElementById("out").innerHTML=counter;

var seconds = (new Date().getTime()-startTime)/1000;
document.getElementById("mean").innerHTML=counter/seconds;
},false);

</script>
</html>

关于java - 从网络服务器到客户端的数据传输速率和数据量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com