我对码头 8.1.10.v20130312 和 WebSocket 有疑问。这是代码:
web.xml
<listener>
<listener-class>ru.websocket.StartupListener</listener-class>
</listener>
启动监听器.java
public class StartupListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
Server server = new Server(8081);
EventHandler handler = new EventHandler();
server.setHandler(handler);
try {
server.start();
System.out.println("Server started");
} catch (Exception e) {
e.printStackTrace();
}
}
}
事件处理程序.java
public class EventHandler extends WebSocketHandler {
public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
return new EventWebSocket();
}
}
EventWebSocket.java
public class EventWebSocket implements WebSocket {
public void onClose(int arg0, String arg1) {
System.out.println("closed");
}
public void onOpen(Connection arg0) {
System.out.println("opened");
}
}
当我想从 javascript 连接时,我收到一条错误消息
2013-07-15 18:02:02.433:WARN:oejs.AbstractHttpConnection:/
java.lang.NullPointerException
at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:238)
at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:396)
at org.eclipse.jetty.websocket.WebSocketHandler.handle(WebSocketHandler.java:43)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:368)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:942)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1004)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:662)
关于构建,我使用 maven,java 版本 1.6.31,jetty 版本 8.1.10.v20130312。 重要 我想在端口 8080 (StartupListener) 上从 Jetty 使用端口 8081 启动新的 Jetty 服务器。
感谢您的回答。