tomcat 7.0.50 java websocket 实现给出 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21852459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:03:16  来源:igfitidea点击:

tomcat 7.0.50 java websocket implementation gives 404 error

javajavascripttomcatwebsocket

提问by Shailesh Vaishampayan

I am trying to implement a websocket on tomcat 7.0.50 using annotated endpoints as specified in Java Websocket API (1.0)- JSR 356. Following are the brief steps how I have coded it 1) Write a websocket endpoint using @ServerEndpoint annotation 2) implement @onOpen and @onMessage methods 3) open a websocket using javascript on google chrome.

我正在尝试使用 Java Websocket API (1.0)- JSR 356 中指定的带注释端点在 tomcat 7.0.50 上实现 websocket。以下是我如何对其进行编码的简要步骤 1) 使用 @ServerEndpoint 注释编写 websocket 端点 2)实现@onOpen 和@onMessage 方法 3) 在 google chrome 上使用 javascript 打开一个 websocket。

Please find code corresponding to above steps in order

请按顺序查找上述步骤对应的代码

1) STEP 1 & 2 - writing websocket server endpoint:

1) 第 1 步和第 2 步 - 编写 websocket 服务器端点:

        package com.jkweb.websocket;

       import java.io.IOException;
       import java.util.HashMap;
       import java.util.List;
       import java.util.Map;

       import javax.websocket.EndpointConfig;
       import javax.websocket.OnMessage;
       import javax.websocket.OnOpen;
       import javax.websocket.Session;
       import javax.websocket.server.PathParam;
       import javax.websocket.server.ServerEndpoint;

       import org.slf4j.Logger;
       import org.slf4j.LoggerFactory;

  @ServerEndpoint(value="/websocket/fileuploadtracker")

@OnOpen 
public void open(Session session,EndpointConfig config) {
    ......
}
@OnMessage
public void onMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

public static void sendMessage(String uniqueTocken,String msg){
    try {
        Session wsSession = socketConnectionMap.get(uniqueTocken);
        wsSession.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

}

}

2) STEP 3 - opening websocket using javascript api in chrome:

2) 第 3 步 - 在 chrome 中使用 javascript api 打开 websocket:

      wsurl =  "ws://localhost:8080/jkweb/websocket/fileuploadtracker",
        ws; 
        ws = new WebSocket(wsurl);
         ws.onopen = function()
         {
            // Web Socket is connected, send data using send()
            ws.send("Sending first Message");
            alert("Message is sent...");
         };
         ws.onmessage = function (evt) 
         { 
            var received_msg = evt.data;
            alert("Message is received...");
         };
         ws.onclose = function(evt)
         { 
            // websocket is closed.
            alert("Connection is closed..."+evt.code + ":"+evt.reason ); 
         };        

I am using following versions of softwares : 1) Tomcat - 7.0.50 2) Java - 1.7.45 3) Servlet - 3.0 4) Have included following Maven dependency 5) Chrome - 32.0.1700.107m

我正在使用以下软件版本:1) Tomcat - 7.0.50 2) Java - 1.7.45 3) Servlet - 3.0 4) 包含以下 Maven 依赖项 5) Chrome - 32.0.1700.107m

     <dependency>
     <groupId>javax.websocket</groupId>
     <artifactId>javax.websocket-api</artifactId>
     <version>1.0</version>
         <scope>provided</scope>
</dependency>   

However I am getting following error and connection is closed with 1066 error code in chrome's console:

但是,我收到以下错误,并且连接已关闭,并在 chrome 的控制台中显示 1066 错误代码:

      WebSocket connection to 'ws://localhost:8080/jkweb/websocket/fileuploadtracker' failed: Error during WebSocket handshake: Unexpected response code: 404

Is there any configuration I am missing out on. I tried to search a lot but couldn't find anything. Please I need this to be solved ASAP.

有没有我遗漏的配置。我试图搜索很多,但找不到任何东西。请我需要尽快解决这个问题。

采纳答案by Shailesh Vaishampayan

I have solved it. Problem was very specific to my installation. I had the websocket api .jar being installed in my app's WEB-INF/lib directory as well. Not sure why it was breaking it. can you put some light on the root cause of this behaviour and why it doesn't work when you gave websocket-api.jar in application's WEB-INF/lib along with tomcat's lib?

我已经解决了。问题非常特定于我的安装。我也将 websocket api .jar 安装在我的应用程序的 WEB-INF/lib 目录中。不知道为什么它会破坏它。你能解释一下这种行为的根本原因吗?为什么当你在应用程序的 WEB-INF/lib 和 tomcat 的 lib 中提供 websocket-api.jar 时它不起作用?

回答by kei

If you used tomcat server which was created in eclipse, you should delete the old server and create a new one with tomcat 7.0.50. This is my solution. PS: My tomcat server is 7.0.53, and the old one is 7.0.21. Good luck to you~

如果你使用的是在eclipse中创建的tomcat服务器,你应该删除旧服务器并使用tomcat 7.0.50创建一个新服务器。这是我的解决方案。PS:我的tomcat服务器是7.0.53,旧的是7.0.21。祝你好运~

回答by mahem

I also had the same problem. Try removing the unwanted jars which contains websocket in your $JAVA_HOME/lib folder. and then add only

我也有同样的问题。尝试删除 $JAVA_HOME/lib 文件夹中包含 websocket 的不需要的 jar。然后只添加