0
我提出兩類:的WebSocket具有嵌入Tomcat是404
- 與網頁套接字的嵌入的Tomcat(20年5月8日)
- ServerEndpoint Main類
我上IntelliJ IDEA的運行的主類 並在谷歌瀏覽器的控制檯中運行此JavaScript:new WebSocket('ws://localhost:8080/ws')
。
我預期的響應代碼是200,但實際上它是404
我怎樣才能解決這個問題?
主要類:
package webapp;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import javax.servlet.ServletException;
import java.io.File;
public class Main {
private static final String STATIC_DIR = "src/main/static/";
public static void main(String[] args) throws ServletException, LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
File staticDir = new File(STATIC_DIR);
tomcat.addWebapp("/", staticDir.getAbsolutePath());
tomcat.start();
tomcat.getServer().await();
}
}
ServerEndpoint:
package webapp.websocket;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/ws")
public class SampleWebSocket {
@OnOpen
public void onOpen(Session session) {
System.out.println("open");
}
@OnClose
public void onClose(Session session) {
System.out.println("close");
}
@OnMessage
public String onMessage(String text) {
System.out.println("message:" + text);
return "Server:" + text;
}
}
謝謝。