tomcat 7.0.50 java websocket实现给出了404错误 [英] tomcat 7.0.50 java websocket implementation gives 404 error

查看:88
本文介绍了tomcat 7.0.50 java websocket实现给出了404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java Websocket API(1.0) - JSR 356中指定的带注释端点在tomcat 7.0.50上实现websocket。以下是我编写它的简要步骤
1)编写一个websocket端点使用@ServerEndpoint注释
2)实现@onOpen和@onMessage方法
3)在谷歌浏览器上使用javascript打开websocket。

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.

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

Please find code corresponding to above steps in order

1)步骤1& 2 - 编写websocket服务器端点:

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

        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)第3步 - 使用chrome中的javascript api打开websocket:

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

      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 ); 
         };        

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

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

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

但是我收到以下错误,连接因chrome控制台中的1066错误代码而关闭:

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

      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.

推荐答案

我已经解决了。问题对我的安装非常具体。
我的websocket api .jar也安装在我的应用程序的WEB-INF / lib目录中。
不知道为什么会破坏它。你能否解释一下这个行为的根本原因以及当你在应用程序的WEB-INF / lib和tomcat的lib中给出websocket-api.jar时它为什么不起作用?

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?

这篇关于tomcat 7.0.50 java websocket实现给出了404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆