简体   繁体   English

Jetty服务器上的多个连接器

[英]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. 我正在尝试运行一个可以让许多人连接到服务器并查看打印输出列表的Jetty服务器。 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? 例如,如果我只有一个列表来跟踪时间,并且我希望5个人左右可以访问我的网站(例如localhost:8080 / time),让他们都每30秒看到一次,我将如何设置?

What I have: 我有的:

  • I am using Jetty. 我正在使用码头。
  • I created a single server bound to port 8080. 我创建了一个绑定到端口8080的服务器。
  • I created my own handler that extends AbstractHandler 我创建了自己的用于扩展AbstractHandler的处理程序
    • this writes to the screen saying when an event has transpired (ie 30 seconds have passed) 这会写到屏幕上,说明发生了什么事件(即已过去30秒)
  • 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 如果3个人连接,则只有2个人可以保持连接,而第3个人只是旋转而没有输出到屏幕

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. 因此,列表对象是在Main类中定义的静态ArrayList,我每30秒唤醒一次(即通知一次)。 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 处理程序代码除了在处理程序中看到通常是奇怪的东西外,没有任何本质上的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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