从Web服务器到客户端的数据传输速率和数量

时间:2012-10-06 10:06:54

标签: java javascript http webserver client

我正在编写一个在tomcat上运行的Web应用程序。我需要传输大约25 kb的数据(从服务器到客户端每20 ms传输一次所有双/长值。)尝试使用服务器发送的事件,因为我只需要一个从服务器到客户端的单向通信通道。

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

请建议我在这里谈论的内容是否合理,是否可以实现。

我是网络开发的新手。

1 个答案:

答案 0 :(得分:1)

为什么不亲自尝试 - 你会发现localhost和实际网络之间存在很大差异:

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";
    }
}

的index.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>