I try to create a web application with the help of maven on the seventh tomcat, but I can’t legit the web socket to the server, the html page is visible, but there is no connection. The project is successfully assembled and compiled.

This is the project structure:

project tree

Here is my endpoint:

package ru.cadmy.startgame; import java.io.IOException; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value="/startgame") public class GameEndPoint { @OnOpen public void onOpen(Session session) throws IOException { System.out.println("Welcome"); session.getBasicRemote().sendText("Hello websockets"); } } 

Pom

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ru.cadmy</groupId> <artifactId>startgame</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>startgame Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat7-websocket</artifactId> <version>7.0.56</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> </dependencies> <build> <finalName>startgame</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project> 

Front end

 <!DOCTYPE html> <meta charset="utf-8" /> <title>WebSocket Test</title> <script type="text/javascript"> var wsUri = "ws://localhost:8080/startgame"; var output; function init() { output = document.getElementById("output"); testWebSocket(); } function testWebSocket() { websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { onOpen(evt) }; websocket.onclose = function(evt) { onClose(evt) }; websocket.onmessage = function(evt) { onMessage(evt) }; websocket.onerror = function(evt) { onError(evt) }; } function onOpen(evt) { writeToScreen("CONNECTED"); doSend("WebSocket rocks"); } function onClose(evt) { writeToScreen("DISCONNECTED"); } function onMessage(evt) { writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); websocket.close(); } function onError(evt) { writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); } function doSend(message) { writeToScreen("SENT: " + message); websocket.send(message); } function writeToScreen(message) { var pre = document.createElement("p"); pre.style.wordWrap = "break-word"; pre.innerHTML = message; output.appendChild(pre); } window.addEventListener("load", init, false); </script> <h2>WebSocket Test</h2> <div id="output"></div> 

And web.xml, which I strongly doubt:

 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="GameEndPoint" implementation="ru.cadmy.startgame.GameEndPoint" url-pattern="/" /> </endpoints> 

What could be the problem? How to solve?

UPD: in short, the server still needs to be configured in pom, and not just run O_o

  • @cadmy, If possible, post answers on the forum, they can help many in the future. - Nicolas Chabanovsky

0