在Tomcat 6中使用websockets

时间:2014-12-08 20:11:05

标签: java tomcat websocket

有没有办法在tomcat 6中使用websockets(业务需求)?

我一直在尝试使用javax.websocket.jar,但我无法让它工作

这是我的代码:

import java.io.IOException;
import java.nio.ByteBuffer;

import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/echoAnnotation")
public class EchoAnnotation {    @OnMessage
     public void echoTextMessage(Session session, String msg, boolean last) {
          try {
               if (session.isOpen()) {
                    session.getBasicRemote().sendText(msg, last);
               }
          } catch (IOException e) {
               try {
                    session.close();
               } catch (IOException e1) {
                    // Ignore
               }
          }
     }

}

我的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_2_5.xsd"
   version="2.5"> 

    <description>
      Servlet and JSP Examples.
    </description>
    <display-name>Servlet and JSP Examples</display-name>

    <servlet>
        <servlet-name>echoAnnotation</servlet-name>
        <servlet-class>EchoAnnotation</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>echoAnnotation</servlet-name>
        <url-pattern>/echoAnnotation</url-pattern>
    </servlet-mapping>
</web-app>

但是我无法建立websocket连接,收到错误消息Firefox can't establish a connection to the server at ws://192.168.1.101:8080/prototypes/echoAnnotation并直接转到页面结果HTTP Status 404 - Servlet echoAnnotation is not available

这可能吗?我做错了什么?

1 个答案:

答案 0 :(得分:1)

我发现tomcat 6不支持Java WebSockets。见http://tomcat.apache.org/whichversion.html