如何在Heroku上部署和访问Java WebSocket终结点而无需任何其他框架 [英] How to deploy and access a Java WebSocket endpoint on Heroku without any additional frameworks
问题描述
我已经按照以下非常基本的教程来用Java设置WebSocket端点:
有一个问题,但是,Heroku上的Tomcat 8缺少websocket插件.
但是不用担心,您可以将其手动添加到war文件中.步骤如下:
-
在此处下载Tomcat websocket插件:
I have followed this very basic tutorial for setting up a WebSocket endpoint in Java: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html
Heroku, however, expects me to rely on Play Framework: https://devcenter.heroku.com/articles/play-java-websockets
My question is: how could I deploy the same without any additional frameworks and what procedure should I go through in order to make things work?
解决方案The problem you had was this:
The tutorial you followed was made for the GlassFish Application Server but Heroku only supports Tomcat 8 and Jetty. See here: https://devcenter.heroku.com/articles/war-deployment
But don't worry, I ported and tested the tutorial to run with Tomcat 8.
I also added the glassfish implementation of the javax.json specification. (Make sure to download the implementation and not the spec interfaces only) You can find it here: http://central.maven.org/maven2/org/glassfish/javax.json/1.0.4/
I also noticed why maybe your index.html didn't work locally: I think it was because the WebSocket URL was hardcoded in the websocket.js file. I have taken the liberty to fix this by making it dynamic.
Here is the complete NetBeans 8.0.2 project:
http://ray.hulha.net/WebsocketHome.zipHere is the best way to create a war file from inside NetBeans 7 or 8:
There is one catch however, the Tomcat 8 on Heroku is missing the websocket addon.
But no worries, you can added it manually to the war file. Here are the steps:
Download the Tomcat websocket addon here:
http://central.maven.org/maven2/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.29/tomcat-embed-websocket-8.0.29.jarThe war file is really just a zip file. So I used 7-zip to open the war file. Then navigate to the WEB-INF/lib folder inside the war file.
Copy the jar into the war. Drag and Drop the tomcat-embed-websocket-8.0.29.jar into the lib folder of the war file inside 7-Zip.
Z-Zip will ask if you really want to copy the jar into the war file. Say Yes.
Here is the compiled war file complete with the tomcat-embed-websocket-8.0.29.jar ready to deploy on Heroku: http://ray.hulha.net/WebsocketHome.war
I used this command to deploy it:
heroku war:deploy WebsocketHome.war --app websockethome
Make sure to replace the app name in the end with your app name.
And here is the working result: http://websockethome.herokuapp.com/
这篇关于如何在Heroku上部署和访问Java WebSocket终结点而无需任何其他框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!