简体   繁体   中英

Multiple Connectors on a Jetty Server

I'm trying to run a Jetty Server that can have a number of people connect to the server and see a list of print outs. I want everybody who connects to see the same values printed out.

For instance, if I have a single list keeping track of the time and I want 5 or so people to be able to go to my website (eg localhost:8080/time) and have them all see what time it is every 30 seconds, how would i set that up?

What I have:

  • I am using Jetty.
  • I created a single server bound to port 8080.
  • I created my own handler that extends AbstractHandler
    • this writes to the screen saying when an event has transpired (ie 30 seconds have passed)
  • If two people connect to this page, they each see a print out every minute (that is it switches back and forth letting each person know when every other event has transpired)
  • If 3 people connect, only two can stay connected and the third just spins getting no output to the screen

I have not set up an Connectors of my own since my attempts to do so have been unsuccessful and i'm not sure how I understand if that is the solution to my problem.

Any help would be much appreciated and if anybody has some idea but needs some clarification on what I am doing I would be glad to give more details.

Thanks!

Handler code:

@Override
public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException
{

httpServletResponse.setContentType("text/html;charset=utf-8");
httpServletResponse.setStatus(HttpServletResponse.SC_OK);

request.setContextPath("/time");
request.setHandled(true);

while (true) {

    synchronized(Main.list) {
         while (!Main.list.isEmpty()) {
              Double time = Main.list.get(0);
              httpServletResponse.getWriter().println("<h1>The time now is " + time + "</h1>"); 
              httpServletResponse.flushBuffer();    
              Main.list.remove(0);
         }
         try {
              Main.list.wait();
         } catch (InterruptedException e) {
              e.printStackTrace();
         }
    }

 }

So the list object is a static ArrayList defined in the Main class that I wake up (ie notify) every 30 seconds. Hopefully this helps someone understand more what I am talking about as i'm not sure what I could change in the handler...

How are you feeding clients into your handler? Browsers have limits to the number of connections are made to to a particular host, perhaps your seeing that.

there is nothing intrinsically wrong that handler code aside from it being a generally odd thing to see in a handler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM