0

You can find the all the code below, started the tomcat server and It's perfectly working. But While making websocket request I'm getting 404 error. This is the error in chrome console :

WebSocket connection to 'ws://localhost:8088/websocket-example/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

and there is no server log.

Java WebSocket Code : WebSocketTest.java

package com.websockets;

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("/websocket")
public class WebSocketTest {

@OnMessage
public void onMessage(String message, Session session) throws IOException,
        InterruptedException {
    System.out.println("Message: " + message);
    session.getBasicRemote().sendText("Hello Mr. " + message);
}

@OnOpen
public void onOpen() {
    System.out.println("Client connected");
}

@OnClose
public void onClose() {
    System.out.println("Connection closed");
}
}

JS Code : hello.html

var webSocket = new WebSocket('ws://localhost:8088/websocket-example/websocket');

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<welcome-file-list>
  <welcome-file>hello.html</welcome-file>
</welcome-file-list>

</web-app>

Folder structure unders tomcat/webapps..

->webapps
     ->websocket-example
          ->hello.html
          ->WEB-INF
              ->classes
                  ->com
                      ->websockets
                          -> WebSocketTest.class
              ->web.xml

more info :

java version "1.7.0_75"

apache-tomcat-8.0.32

server running port : 8088

I'm not sure this information is enough,Let me know any other details need to debug?

Community
  • 1
  • 1
mahem
  • 1
  • 1
  • 4
  • [https://stackoverflow.com/questions/30500998/websocket-handshake-unexpected-response-code-404](https://stackoverflow.com/questions/30500998/websocket-handshake-unexpected-response-code-404) – yeafee Jul 08 '16 at 10:11

1 Answers1

0

Removed unwanted jars from $JAVA_HOME/lib. and then added the javax.websocket-api-1.0-rc4.jar then It's working fine.

mahem
  • 1
  • 1
  • 4