0

I'm building my java web application on my home server.

My home server is using apache2 as the web-server and tomcat(9.0.5) as the java-container, along with mysql database, all on Ubuntu OS 16.04 LTS.

I try to build the .war file then deploy through tomcat , everything worked well, even the MySQL database connection , then i realize that my Websocket is not working.

There is no initial message sent from server and client cannot send message to server as well. There is no error in the browser console showing that there is a websocket connection error.

The odd is, my web app work well when deployed by netbeans on my localhost, can someone tells me what have i missed.

Although i think the problem is how i deploy the app, but for more info, this is the snapshot of my code for websocket on both client and server.

Client (using javascript):

function onMessage(e) {...} //some implementations

var socket = null;
window.onload = function() {
  var uname = ...;
  socket = new WebSocket("ws://" + window.location.host + getPageContextPath() + "/actions/"+uname+"/review/"+bookID);
  socket.onmessage = onMessage;
};

Server (using java annotation):

@ApplicationScoped
@ServerEndpoint(value = "/actions/{uname}/{page}/{id}")
public class EndpointServer {
@OnOpen
    public void onOpen(Session session, @PathParam("uname") String uname, 
            @PathParam("page") String page, @PathParam("id") String id) {
        //do something when a client view the page
        ...
    }
  • Are you connecting webocket through httpd, or directly to Tomcat? How have you configured httpd to proxy to Tomcat? – Christopher Schultz Feb 28 '18 at 00:43
  • After doing some searching, i find that some people say running behind Apache as a proxy may cause the problem, so i think through httpd. Could you tell me what is httpd and how it affect the outcome – Đào Thiện Tuấn Feb 28 '18 at 05:43
  • Apache httpd is a web server. If you are using it with Tomcat, you are using httpd as a reverse-proxy. When you do that with Websocket, you need to use the proper proxying mechanism, which is `mod_proxy_wstunnel`. If you don't know if you are using httpd or not, then you need to go find out and come back with the right answer. – Christopher Schultz Feb 28 '18 at 15:23
  • Does httpd change name to apache2, cause i run "service httpd status" return not found, but "apache2" return active running, so i think i connect through httpd. – Đào Thiện Tuấn Feb 28 '18 at 19:06
  • The service name depends upon the environment and package-manager. The `apache2` service is almost certainly Apache httpd. – Christopher Schultz Feb 28 '18 at 20:35
  • I follow some guide then enable proxy (mod_proxy) and proxy_wstunnel(mod...) modules, are there any configurations for this to work, it's still not working. – Đào Thiện Tuấn Mar 01 '18 at 04:55
  • Actually i found out that tomcat does not support Dependency Injection , which i use in my webapp. So everything is clear now. – Đào Thiện Tuấn Mar 01 '18 at 13:53

0 Answers0