Tomcat WebSocket连接关闭,代码:1006问题

时间:2015-01-21 14:55:16

标签: java tomcat websocket

我正在使用websocket尝试Tomcat服务器。这就是我做的: 我创建了3个从Tomcat示例中复制的java文件。请参阅下面的代码。 然后我构建一个war文件并将其放在webapps中。但后来我收到此错误消息: 信息:WebSocket连接关闭,代码:1006

我是否会错过在Tomcat上制作websocket的任何步骤?

感谢。

1. ExamplesConfig.java

    import java.util.HashSet;
    import java.util.Set;
    import javax.websocket.Endpoint;
    import javax.websocket.server.ServerApplicationConfig;
    import javax.websocket.server.ServerEndpointConfig;
    public class ExamplesConfig implements ServerApplicationConfig
    {
        @Override
        public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned)
        {
            Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();
            System.out.println("ExamplesConfig ==========> getEndpointConfigs");
            if (scanned.contains(EchoEndpoint.class))
            {
                result.add(ServerEndpointConfig.Builder.create(EchoEndpoint.class, "/websocket/echoProgrammatic").build());
            }
            return result;
        }

        @Override
        public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned)
        {
            // Deploy all WebSocket endpoints defined by annotations in the examples
            // web application. Filter out all others to avoid issues when running
            // tests on Gump
            Set<Class<?>> results = new HashSet<Class<?>>();
            for (Class<?> clazz : scanned)
            {
                if (clazz.getPackage().getName().startsWith("websocket."))
                {
                    System.out.println("getAnnotatedEndpointClasses ===========>" + clazz);
                    results.add(clazz);
                }
            }
            return results;
        }
    }

2. EchoEndpoint.java

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

    import javax.websocket.Endpoint;
    import javax.websocket.EndpointConfig;
    import javax.websocket.MessageHandler;
    import javax.websocket.RemoteEndpoint;
    import javax.websocket.Session;

    public class EchoEndpoint extends Endpoint
    {
        @Override
        public void onOpen(Session session, EndpointConfig endpointConfig) {
            RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
            session.addMessageHandler(new EchoMessageHandlerText(remoteEndpointBasic));
            session.addMessageHandler(new EchoMessageHandlerBinary(remoteEndpointBasic));
        }

        private static class EchoMessageHandlerText
                implements MessageHandler.Partial<String> {

            private final RemoteEndpoint.Basic remoteEndpointBasic;

            private EchoMessageHandlerText(RemoteEndpoint.Basic remoteEndpointBasic) {
                this.remoteEndpointBasic = remoteEndpointBasic;
            }

            @Override
            public void onMessage(String message, boolean last) {
                try {
                    if (remoteEndpointBasic != null) {
                        remoteEndpointBasic.sendText(message, last);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        private static class EchoMessageHandlerBinary
                implements MessageHandler.Partial<ByteBuffer> {

            private final RemoteEndpoint.Basic remoteEndpointBasic;

            private EchoMessageHandlerBinary(RemoteEndpoint.Basic remoteEndpointBasic) {
                this.remoteEndpointBasic = remoteEndpointBasic;
            }

            @Override
            public void onMessage(ByteBuffer message, boolean last) {
                try {
                    if (remoteEndpointBasic != null) {
                        remoteEndpointBasic.sendBinary(message, last);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

3. EchoAnnotation.java

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

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

    @ServerEndpoint("/websocket/echoAnnotation")
    public class EchoAnnotation
    {
        @OnMessage
        public void echoTextMessage(Session session, String msg, boolean last) {
            try {
                if (session.isOpen()) {
                    System.out.println("==========> this is my code");
                    session.getBasicRemote().sendText(msg, last);
                }
            } catch (IOException e) {
                try {
                    session.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
        }

        @OnMessage
        public void echoBinaryMessage(Session session, ByteBuffer bb,
                boolean last) {
            try {
                if (session.isOpen()) {
                    session.getBasicRemote().sendBinary(bb, last);
                }
            } catch (IOException e) {
                try {
                    session.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
        }

        /**
         * Process a received pong. This is a NO-OP.
         *
         * @param pm    Ignored.
         */
        @OnMessage
        public void echoPongMessage(PongMessage pm) {
            // NO-OP
        }
    }

2 个答案:

答案 0 :(得分:0)

刚从这里找到答案:Can't connect to my tomcat 8 websocket

Tomcat 7显示错误,但Tomcat 8没有。

答案 1 :(得分:0)

如果您运行 JVM 版本 1.7 并且您的web.xml使用 Tomcat 7 .0.x >根据Tomcat documentation的Servlet规范版本3.0 。

您的web.xml文件应如下所示:

<web-app version="3.0" ... >