Alex Kolokolov
Alex Kolokolov

Reputation: 143

Java Spring MVC WebSocket application works on local application server only but not on openshift host

I tried to make a simple websocket chat, also using Spring MVC.
Source code is available in my repository here on GitHub
It consists of following parts:
Welcome view page index.jsp contains form for user's nickname and password entering and binding it to new User instance.

Welcome page

User instance is passed to the Spring MVC controller MainController.java. Controller creates new ModelAndView linked to the chat.jsp and passes the User instance to it for user's nickname displaying in the header and in the chat window. chat.jsp contains js code providing connection to websocket endpoint in Chat.java. And it also passes user's nikname to the endpoint to store it in its String field.
When I try it on my local Tomcat 8 everything works fine, just as expected.

working chat

This is request and response of websocket endpoint connection. enter image description here

But when I try it on OpenShift's Tomcat 7 remote host it doesn't work.
Here are the screenshots:

enter image description here enter image description here

As you can see user's nickname was not displayed in header and was not passed to the endpoint with request URL.
It seems that even the User instance has not been passed from the Spring MVC controller to the chat.jsp view page.
Can somebody please explain me what I'm doing wrong?

Upvotes: 1

Views: 909

Answers (2)

Alex Kolokolov
Alex Kolokolov

Reputation: 143

One problem has been solved. Now user nickname is displayed in the view header and it is also passed to websocket endpoint with connection request.
I just added one line <%@ page isELIgnored="false" %> to chat.jsp to make Expression Language work. For some reason it is necessary if you use Tomacat 7.
So now after adding the port numbers to websocket endpoint connection request I have got this: enter image description here enter image description here
But the chat still doesn't work properly, as connection has been closed right after its establishment.

Upvotes: 0

Jiri Fiala
Jiri Fiala

Reputation: 1400

I see two problems there:

  1. Ws and wss ports - use ports 8000 or 8443 for web socket connection on OpenShift. I recommend amending your chat.jsp:

    Chat.initialize = function () {
      if (window.location.protocol == 'http:') {
        Chat.connect('ws://' + window.location.host + ':8000/websocket/chat/${user.nickname}');
      } else {
        Chat.connect('wss://' + window.location.host + ':8443/websocket/chat/${user.nickname}');
      }
    };
    
  2. The Tomcat server version. You can use this quick-start to run Tomcat 8 on OpenShift Online. However, when I tried using the quick-start, I faced some trouble so I changed a few things and created a PR; before it's merged you can take a version that worked for me from here. In order to deploy Tomcat 8 on a small DIY gear, you can use this command: rhc app create tomcat8 diy --from-code https://github.com/jiri-fiala/openshift-tomcat8-quickstart. Replace tomcat8 with the name you want for your app. Note that it will take a few minutes to download and compile Tomcat 8.

With the Tomcat 8 running on OpenShift, I deployed your code as ROOT.war (I've created a package locally using mvn package), using Tomcat's web interface. The testing chat app seems to be just fine.

Of course, this is not quite an ideal deployment; the web app is deployed to the data directory this way. OpenShift Online presently does not offer a Tomcat 8 cartridge where you could push your code into the repo and have your web app built and deployed automatically.

Upvotes: 2

Related Questions