如何让 JSR-356 (javax.websockets.api) 在未嵌入的 Jetty 9.3 JEE 环境中工作?

How to get JSR-356 (javax.websockets.api) working on Jetty 9.3 JEE environment not embedded?

我很难尝试在码头 9.3 servlet(未嵌入)上使用 javax.websockets 获得一个简单的回显服务器。

服务器:

import java.io.IOException;

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


public class EchoServer extends Endpoint {

    @Override
    public void onOpen( Session session, EndpointConfig EndPointCfg ) {
        RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
        session.addMessageHandler( new EchoMessageHandler( remoteEndpointBasic ) );

    }

    private static class EchoMessageHandler implements MessageHandler.Whole<String> {

        private final RemoteEndpoint.Basic remoteEndpointBasic;


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


        @Override
        public void onMessage( String message ) {
            try {
                if ( remoteEndpointBasic != null ) {
                    remoteEndpointBasic.sendText( message );
                }
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }
}

配置class:

import java.util.HashSet;
import java.util.Set;

import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;


public class EchoServerCfg implements ServerApplicationConfig {

    @Override
    public Set<Class<?>> getAnnotatedEndpointClasses( Set<Class<?>> set ) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public Set<ServerEndpointConfig> getEndpointConfigs( Set<Class<? extends Endpoint>> set ) {
        Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();
        if ( set.contains( EchoServer.class ) ) {
            result.add( ServerEndpointConfig.Builder.create( EchoServer.class, "/websocket/echo" ).build() );
        }
        return result;
    }

}

它在 tomcat 7.0 上运行良好,但在 Jetty 9.3 上运行不佳,我们将不胜感激。


编辑 1: 它缺少有关问题的信息,我得到 404,所以我相信端点没有被映射:

$ wscat -c localhost:8080/websocket/echo 

error: Error: unexpected server response (404)

但它在 tomcat 上运行良好:

wscat -c localhost:8081/websocket/echo 

connected (press CTRL+C to quit)
> test

< test

编辑 2:

Java版本:

tiago@lenovo:~$ /opt/jdk/bin/java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

我正在使用 eclipse-jetty plugin 到 运行,但这是我在 shell 中的命令:

tiago@lenovo:~$ ps axo cmd  | grep jetty  | sed -re 's/:|\s+/\n/g'
/opt/jdk/bin/java
-Djetty.launcher.configuration=/tmp/eclipseJettyPlugin.config.TestServlet.xml
-Dfile.encoding=UTF-8
-classpath
/opt/eclipse/configuration/org.eclipse.osgi/bundles/1011/1/.cp/lib/eclipse-jetty-starters-common.jar
/opt/eclipse/configuration/org.eclipse.osgi/bundles/1011/1/.cp/lib/eclipse-jetty-starters-util.jar
/opt/eclipse/configuration/org.eclipse.osgi/bundles/1011/1/.cp/lib/eclipse-jetty-starters-console.jar
/opt/eclipse/configuration/org.eclipse.osgi/bundles/1011/1/.cp/lib/eclipse-jetty-starters-jetty9.jar
/opt/jetty/lib/jetty-rewrite-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-servlets-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-jndi-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-xml-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-annotations-9.3.3.v20150827.jar
/opt/jetty/lib/annotations/asm-commons-5.0.1.jar
/opt/jetty/lib/annotations/javax.annotation-api-1.2.jar
/opt/jetty/lib/annotations/asm-5.0.1.jar
/opt/jetty/lib/jetty-plus-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-proxy-9.3.3.v20150827.jar
/opt/jetty/lib/servlet-api-3.1.jar
/opt/jetty/lib/jetty-server-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-infinispan-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-schemas-3.1.jar
/opt/jetty/lib/jetty-servlet-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-io-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-http-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-jmx-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-quickstart-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-deploy-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-jaas-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-client-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-security-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-util-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-nosql-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-alpn-server-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-continuation-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-webapp-9.3.3.v20150827.jar
/opt/jetty/lib/jetty-jaspi-9.3.3.v20150827.jar
net.sourceforge.eclipsejetty.starter.jetty9.Jetty9LauncherMain

编辑 3:[已解决]

问题出在jetty-eclipse插件上,原来他们昨天2015/10/11发布的3.9.0版本解决了这个问题。手动部署也很棘手,但在使用 eclipse 时使用插件很好。

您的 运行 服务器中没有 websocket 实现 类。

这就是为什么没有可用的 JSR356 支持。

您是否勾选了启用 websocket 支持的选项?

Note: Know that the 3rd party Eclipse Plugin you are using (http://eclipse-jetty.github.io/) is not run, managed, maintained, endorsed, recommended by any of the Eclipse Jetty developers.

The Eclipse Jetty project has no official Eclipse Plugin.

The Eclipse Jetty Project developers prefer instead that you use Embedded Jetty facilities to develop/test while in any IDE (even IntelliJ, NetBeans, vim, emacs, JEdit, etc..).