I downloaded an example of a chat on a web socket from a github. In STS, everything works. After deploying to tomcat, at first nothing was displayed at all until it inherited the class where main from SpringBootServletInitializer is located and did not register in build.gradle:

providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") 

Now you can successfully go to the login page, enter the chat, but you can’t write anything and the list of users is empty, and also writes a connection error. What could be the reason for this different behavior? STS, as I understand it, also launches via tomcat, but there are no errors. Can you please tell me how to get sockets in an application that runs on tomcat 8?

Update

I tried to remove the libraries that are needed for working with sockets from my application, because I read that tomcat uses its own, but the application did not start, also indicated them as providedRuntime , but this did not help.

Update 2

Found an error, in scripts there is a chatSocket.init("/ws"); line chatSocket.init("/ws"); So, I checked, it connects not with chat / ws, but with / ws, probably this is the error, I will do it right if I write something like chatSocket.init(serverPrefix+"/ws"); or is there any directive to change the root? (inconvenient, it will be necessary to change the prefix for launching via STS) Perhaps I am confusing something, I recently started learning scripts and spring.

  • I figured out what the problem was, I had to chatSocket.init (); specify the path "/ chat / ws". The only thing that is strange, when you first load the page with a chat, the connection error of the sockets, and after rebooting everything is ok. - roma2341
  • Please post your comment as a response. - Nicolas Chabanovsky

1 answer 1

When using the spring tool suite, the chat page is located at localhost: 8080 / chat.html. When deployed via tomcat, the site root will already be localhost: 8080 / <war file name> /. Therefore, in the scripts it is necessary to add the prefix <name of the war file> to the address of the socket "/ ws".