[英]Apache Tomcat returning 404 when using websocket
我在AWS Elastic Beanstalk上使用Apache Tomcat / 8.0.47,當我在本地mac上運行時,可以很好地連接並得到良好的響應:
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: example.com:80" \
--header "Origin: http://example.com:80" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" http://localhost:8080/echo
但是,如果我進入AWS實例,我只會得到404。
這不是nginx問題,因為在ssh進入時我直接在Tomcat端口上運行。
我花了整整一天的時間尋找解決方案,但我很茫然。 我在網上找到的任何內容似乎都試圖解決負載平衡器/ nginx問題。
供參考的是我的代碼:
package com.mypackage.sockets
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/echo", configurator=EchoEndpointConfigurator.class)
public class EchoServer {
private Set<Session> userSessions = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) {
System.out.println(session.getId() + " has opened a connection");
userSessions.add(session);
}
@OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
userSessions.remove(session);
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
try {
session.getBasicRemote().sendText(message);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
和
package com.mypackage.sockets;
import javax.websocket.server.ServerEndpointConfig.Configurator;
public class EchoEndpointConfigurator extends Configurator {
private static EchoServer echoServer = new EchoServer();
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
return (T)echoServer;
}
}
我也有一個web.xml文件,但只引用了所有正常工作的servlet。
我可能需要在AWS Elastic Beanstalk上設置Tomcat的標志嗎
事實證明,盡管curl命令在本地而非遠程運行,但這不是我的問題。 通過直接在ec2上暴露該端口,我能夠弄清楚該端口是否在8080端口上工作。 最后,為了使其能夠在端口80上工作,我從Apache HTTPD更改為Nginx。 然后使用經典的負載均衡器完美工作。 尚未弄清應用程序負載平衡器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.