我确实有一个关于jetty 8 websocket的奇怪问题。我发现了几个教程,它们几乎显示了相同的代码,但当我试图从本地html页面打开websocket时,却出现了这个错误:
2013-02-05 13:14:03.467:INFO:oejs.Server:jetty-8.1.9.v20130131
2013-02-05 13:14:03.770:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
2013-02-05 13:14:18.002:WARN:oejs.ServletHandler:/echo
java.lang.NullPointerException
at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:236)
at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:382)
at org.eclipse.jetty.websocket.WebSocketServlet.service(WebSocketServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
这是套接字servlet:
public class ExampleWebSocketServlet extends WebSocketServlet {
private static final long serialVersionUID = 1L;
/**
* @see WebSocketServlet#WebSocketServlet()
*/
public ExampleWebSocketServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("OK");
out.close();
System.out.println("OK");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
getServletContext().getNamedDispatcher("default").forward(request, response);
}
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
return new EchoWebSocket();
}
}
和WebSocket(用于简单的Echo)
public class EchoWebSocket implements WebSocket.OnTextMessage {
private Connection con;
@Override
public void onClose(int closeCode, String message) {
}
@Override
public void onOpen(Connection con) {
this.con = con;
try {
con.sendMessage("Server received Web Socket upgrade ...");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String msg) {
System.out.println("Received: "+msg);
try {
con.sendMessage(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //send it back
}
}
我的html页面与其他websocket测试服务器配合良好,例如websocket.org
可能出了什么问题?我在Eclipse上,创建了一个动态web项目,并使用jetty web应用程序运行配置。此外,当码头启动时,我可以看到
/Users/sven.haiges/dev/jetty/jetty-distribution-8.1.9.v20131/lib/jetty-websocket-8.1.9.v10131.jar
在类路径上。
谢谢你的帮助,谢谢!
我在Run Jetty Run插件上也遇到了同样的问题。终于找到了解决方案。运行配置
- 在左侧面板的jettywebapp下选择您的项目名称
- 在jetty选项卡的右侧选择jetty版本8.1
应用/运行这都是
尝试使用Jetty 8.1.8版本。今天,我在通过@WebServlet注释创建一个简单的servlet时遇到了一个问题。jetty服务器没有响应,我也不知道为什么。但当我突然改回8.1.8版本时,一切都很好。也许这也是一个类似的问题。