我想在Heroku上部署Web应用程序,必须使用WebSocketServer.ja中的staticfiles文件夹来提供
port(getHerokuAssignedPort());
webSocketIdleTimeoutMillis(1000000000);
staticFileLocation("/public");
webSocket("/em/", WebSocketHandler.class);
init();
和websocketClient.js
var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/em/");
但是我无法从/ public文件夹中提供* .html,* .css,* .js文件。我的本地系统无法在heroku网站上运行。它显示应用程序错误。
答案 0 :(得分:1)
public class Bootstrap {
private static final int DEFAULT_PORT = 4567;
public static void main(String[] args) throws URISyntaxException {
port(getHerokuAssignedPort());
externalStaticFileLocation(getPublicFolderPath());
}
private static int getHerokuAssignedPort() {
ProcessBuilder processBuilder = new ProcessBuilder();
if (processBuilder.environment().get("PORT") != null) {
return Integer.parseInt(processBuilder.environment().get("PORT"));
}
return DEFAULT_PORT;
}
private static String getPublicFolderPath() throws URISyntaxException{
CodeSource codeSource = Bootstrap.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
return jarDir + File.separator + "public";
}}
/ public目录应与build jar文件位于同一文件夹中。