我正在尝试在我正在研究的嵌入式Jetty应用程序中测试WebSocket支持。我的目标是将数据从服务器流式传输到浏览器。我没有全力以赴,因为我刚开始设置WebSocket servlet / handler。
我看到的问题是Chrome无法连接到WebSocket处理程序:
与'ws://127.0.0.1:8081 / stream'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:404 (匿名)@ ws-test.js:1
HTTP 404来自Jetty,因为它没有注册“/ stream”。
对于我的生活,我无法弄清楚如何使用指定的URL设置WebSocketServlet或WebSocketHandler。我已经阅读了所有可以找到的示例和教程,但是很多都不是嵌入式Jetty,或者是旧的。在任何一种情况下我都愿意错。
我将从一些代码开始。这是我的服务器上下文和处理程序的主要Jetty设置:
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setWelcomeFiles(new String[] { "index.htm" });
resource_handler.setResourceBase('./www');
ServletHandler servletHandler = new ServletHandler();
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, servletHandler, new DefaultHandler() });
server.setHandler(handlers);
// Add the /test servlet mapping
servletHandler.addServletWithMapping(TestServlet.class, "/test/*");
// Add websocket handler
handlers.addHandler(StreamingHandler.getServlet("/stream"));
server.start();
这是扩展WebSocketHandler的StreamingHandler类。请注意,我正在尝试设置WebSocketHandler的上下文路径。关键是WebSocket处理与http://127.0.0.1:8081/stream的通信:
@WebServlet
public class StreamingHandler extends WebSocketHandler
{
public static ContextHandler getServlet(String url) {
ContextHandler ctxHandler = new ContextHandler();
ctxHandler.setContextPath(url);
ctxHandler.setHandler(new StreamingHandler());
return ctxHandler;
}
protected StreamingHandler() {
super();
}
@Override
public void configure(WebSocketServletFactory factory)
{
factory.getPolicy().setIdleTimeout(10000);
factory.register(StreamingSocket.class);
}
}
这是我的基本WebSocket类:
@WebSocket
public class StreamingSocket {
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello!");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("Message: " + message);
}
}
最后有点便宜的Javascript。我不会包含基本的HTML。它只是引用了这个JS:
var ws = new WebSocket("ws://127.0.0.1:8081/stream");
ws.onopen = function() {
alert("Opened!");
ws.send("Hello Server");
};
ws.onmessage = function (evt) {
alert("Message: " + evt.data);
};
ws.onclose = function() {
alert("Closed!");
};
ws.onerror = function(err) {
alert("Error: " + err);
};
任何提示都表示赞赏。感谢。
答案 0 :(得分:4)
在经典形式中,由于Joakim Erdfelt(感谢!)在另一个SO问题中回答了Jetty食谱的链接,我已经解决了这个问题:https://stackoverflow.com/a/34008707/924177
基本上我在这里看一下示例WebSocketServerViaFilter示例: https://github.com/jetty-project/embedded-jetty-cookbook/blob/master/src/main/java/org/eclipse/jetty/cookbook/websocket/WebSocketServerViaFilter.java
我从未见过这种用法,而且这本食谱很新,但它确实有效。这是我的新主服务器代码(请注意,如果您希望提供资源,则必须添加DefaultServlet):
Path webRootPath = new File(www).toPath().toRealPath();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(new PathResource(webRootPath));
context.setWelcomeFiles(new String[] { "index.html" });
server.setHandler(context);
// Add the websocket filter
WebSocketUpgradeFilter wsfilter = WebSocketUpgradeFilter.configureContext(context);
wsfilter.getFactory().getPolicy().setIdleTimeout(5000);
wsfilter.addMapping(new ServletPathSpec("/stream"), new StreamingSocketCreator());
// Add the /test servlet mapping
ServletHolder holderTest = new ServletHolder("test", TestServlet.class);
holderTest.setInitParameter("dirAllowed","true");
context.addServlet(holderTest,"/test/*");
// NOTE! If you don't add the DefaultServlet, your
// resources won't get served!
ServletHolder holderDefault = new ServletHolder("default", DefaultServlet.class);
holderDefault.setInitParameter("dirAllowed", "true");
context.addServlet(holderDefault, "/");
server.start();
server.join();
然后我创建了一个WebSocketCreator实现。这似乎是一个不必要的步骤,但它的API如何工作:
public class StreamingSocketCreator implements WebSocketCreator
{
@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
{
return new PacketStreamingSocket();
}
}
适用相同的WebSocket代码:
@WebSocket
public class StreamingSocket {
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello!");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("Message: " + message);
}
最后,我的JavaScript代码可以找到WebSocket上下文路径!
var ws = new WebSocket("ws://127.0.0.1:8081/stream");
ws.onopen = function() {
alert("Opened!");
ws.send("Hello Server");
};
ws.onmessage = function (evt) {
alert("Message: " + evt.data);
};
ws.onclose = function() {
alert("Closed!");
};
ws.onerror = function(err) {
alert("Error: " + err);
};
这一切都在一起工作。现在我可以测试出服务器 - >客户端数据流。谢谢,我希望将来可以帮助其他人。