我对jetty 8.1.10.v20130312和WebSocket有疑问。这是代码:
web.xml
<listener>
<listener-class>ru.websocket.StartupListener</listener-class>
</listener>
StartupListener.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();
}
}
}
EventHandler.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.v220130312。重要我想从端口8080(StartupListener)上的Jetty启动新的端口为8081的Jetty服务器。
谢谢你的回答。
org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:238)
处的java.lang.NullPointerException
表示您。。。
- 尝试使用来自非Jetty服务器(如Tomcat)的JettyWebSocket代码
- 或者您试图从非HTTP连接(如SPDY)升级
这是因为在WebSocket握手的升级过程中找不到核心HTTP连接,这是Jetty WebSocket服务器代码运行的要求。
您是否使用RunjettyRun eclipse插件来运行您的web应用程序?
如果是,请检查"运行配置",
将"选择Jetty版本"下拉列表框设置为Jetty 8,例如"Jetty 8.1.2.v20120308"
您只能在Jetty 8或更高版本上运行websocket web应用程序。