我对jetty 8 websockets有一个奇怪的问题。我发现几个教程几乎显示相同的代码,但当我尝试从本地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)
这是Socket 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 webapp运行配置。此外,当码头启动时,我可以看到
/Users/sven.haiges/dev/jetty/jetty-distribution-8.1.9.v20130131/lib/jetty-websocket-8.1.9.v20130131.jar
在类路径上。
任何帮助表示感谢,谢谢!
答案 0 :(得分:2)
我遇到了Run Jetty Run插件的同样问题。终于找到解决方案 去运行配置
申请/运行 这都是
答案 1 :(得分:1)
尝试使用Jetty 8.1.8版本。 今天,我遇到了一个问题,即通过@WebServlet注释创建一个简单的servlet。码头服务器没有响应,我无法弄清楚原因。但当我突然改回8.1.8版本时,一切正常。 也许这是一个类似的问题。