How to deploy and access a Java WebSocket endpoint on Heroku without any additional frameworks

感情迁移 提交于 2021-01-27 04:32:11

问题


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?


回答1:


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.zip

Here 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:

  1. 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.jar

  2. The 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.

  3. 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.

  4. 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/




回答2:


Heroku does not require Play framework to use Websockets. That is just an example. As long as your app from the Oracle tutorial binds to $PORT it should work.



来源:https://stackoverflow.com/questions/49841902/how-to-deploy-and-access-a-java-websocket-endpoint-on-heroku-without-any-additio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!