jakartaee / websocket

Jakarta WebSocket
https://projects.eclipse.org/projects/ee4j.websocket
Other
60 stars 40 forks source link

I've been facing issue with websocket API 1.1.1 with tomcat 9 #446

Closed manivannan-r closed 1 month ago

manivannan-r commented 1 month ago

I have created a sample java web project, everything works fine but facing issue while connecting to websocket.

Java : JavaSE-11 Tomcat : 9.0.65 Websocket : 1.1.1 Eclipse : 2023-12 (4.30.0)

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WebSocket</groupId>
  <artifactId>WebSocket</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
        <groupId>jakarta.websocket</groupId>
         <artifactId>jakarta.websocket-api</artifactId>
        <version>1.1.1</version>
        </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
</project>

WebSocket.java

package com.test;

import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/sample/echo")
public class WebSocket {
     @OnOpen
        public void onOpen(Session session) {
            System.out.println("Connected: " + session.getId());
        }
        @OnMessage
        public void onMessage(String message, Session session){
            System.out.println("Message from client: " + message);
            // Send a message back to the client
            try {
                session.getBasicRemote().sendText("Echo: " + message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @OnClose
        public void onClose(Session session) {
            System.out.println("Disconnected: " + session.getId());
        }
}

web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

</web-app>

Client index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Test</title>
</head>
<body>
    <script>
        var ws = new WebSocket("ws://localhost:8080/sample/echo");

ws.onopen = function() {
    console.log("Connected");
    ws.send("Hello from client");
};

ws.onmessage = function(event) {
    console.log("Message from server: " + event.data);
};

ws.onerror = function(error) {
    console.error("WebSocket error: " + error);
};

ws.onclose = function() {
    console.log("Disconnected");
};
    </script>
</body>
</html>

Tried to run on Apache Tomcat Server 9.0.x, Server started successfully, but unable to establish websocket connection from client.

Error :

Screenshot 2024-08-02 at 1 28 28 PM
markt-asf commented 1 month ago

This is not an issue with the WebSocket API. You probably want the Tomcat users mailing list.

manivannan-r commented 1 month ago

Where i can find the websocket API compatability details ?