添加到ServerConfiguration的所有HttpHandler都将被共享 所有听众
是否有另一种方法将不同的处理程序绑定到不同的端口?或者我必须多次实例化HttpServer
?
答案 0 :(得分:1)
您可以将其绑定到PortRange
,即多个端口
NetworkListener(String name, String host, PortRange portRange)
documented
或
HttpServer httpServer = new HttpServer();
NetworkListener networkListener1 = new NetworkListener("sample-listener1", "localhost", 8888);
NetworkListener networkListener2 = new NetworkListener("sample-listener2", "localhost", 8889);
httpServer.addListener(networkListener1);
httpServer.addListener(networkListener2);
这样,您可以为不同的端口添加多个处理程序
或(没试过,但最有可能工作)
HttpServer server = HttpServer.createSimpleServer();
server.getServerConfiguration().addHttpHandler(handler1);
server.getServerConfiguration().addHttpHandler(handler2);
server.start()