Search code examples
javatomcatwebsockettomcat9java-websocket

Associate java with websocket


I have several processes or nodes called A, B and C, which speak between them via sockets. I want to print their state as they speak via a websocket.

To that end, I created a class named WebSocket with the necesary @ApplicationScoped, @ServerEndpoint("/status"), @OnOpen, @OnMessage, @OnClose and @OnError tags. Each tag has its corresponding function. I also created a TomCat server.

The problem is I can't manage to get my processes to speak to the TomCat server and vice versa through the WebSocket class.

Executing my tomcat server, runs an instance of WebSocket where the value of Node is null so making a get to fetch the node's status and printing it is not viable.

The other way around, when executing my nodes, each creates a WebSocket but it's unable to comunicate with Tomcat.

End result is my three nodes running with three instances of WebSocket that can't talk to tomcat while tomcat is running with it's own WebSocket instance that can't fetch the node's status.

Could someone please clarify:

  • Which way is the communication meant to be? Are websockets designed to fetch info or to just wait until it is sent to him.
  • How to solve my issue, ideally by creating first the nodes (each with its own WebSocket) and the tomcat server just waiting to be sent info.
  • To display the screen I made a small .html file (http://localhost:63343/test/web/index.html) but tomcat has one that opens up when runned (http://localhost:8080/test_war_exploded/). If I manage to send messages correctly from the nodes to the tomcat server, which of these two website will be getting the messages? AKA which one is valid?

EDIT:

I just tested executing the Tomcat server and with it, several sockets. Each socket executes a new WebSocket and I can then execute a different node based on a parameter given to the WebSocket.

This is not an option. It might work but in my case all nodes must be separate programs, and doing this means they are all a same program, but simply under different threads.


Solution

  • I found the issue. The client needs it's own class to communicate with the server, which is not there. WebSocket class is used by the Tomcat server and the client shouldn't use it, needs a new one.

    Follow this tutorial for a good example.

    In my case it made me understand my design mistake. I get a Could not find an implementation class exception on the ContainerProvider, but that's another story.