简体   繁体   English

在Tomcat 6中使用websockets

[英]Using websockets in Tomcat 6

Is there any way to use websockets in tomcat 6 (business requirement)? 有什么方法可以在tomcat 6中使用websocket(业务需求)?

I've been trying using javax.websocket.jar but I can't get it to work 我一直在尝试使用javax.websocket.jar,但我无法使其正常工作

Here's my code: 这是我的代码:

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

}

And my web.xml: 而我的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>

However I cannot make a websocket connection, I get an error message Firefox can't establish a connection to the server at ws://192.168.1.101:8080/prototypes/echoAnnotation and going directly to the page results in HTTP Status 404 - Servlet echoAnnotation is not available 但是我无法建立网络套接字连接,我收到一条错误消息, 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

Is this possible? 这可能吗? What am I doing wrong? 我究竟做错了什么?

As I found tomcat 6 doesn't support Java WebSockets. 我发现tomcat 6不支持Java WebSockets。 See http://tomcat.apache.org/whichversion.html 参见http://tomcat.apache.org/whichversion.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM