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

时间:2015-10-11 13:35:18

标签: java websocket jetty

我很难在jetty 9.3 servlet(未嵌入)上使用javax.websockets尝试获取一个简单的echo服务器。

服务器:

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

配置类:

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时使用插件很不错。

1 个答案:

答案 0 :(得分:1)

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

这就是为什么没有JSR356支持的原因。

您是否已选中启用websocket支持的选项?

  

注意:知道您正在使用的第三方Eclipse插件   (http://eclipse-jetty.github.io/)未运行,管理,维护,   任何Eclipse Jetty开发人员都推荐使用。

     

Eclipse Jetty项目没有正式的Eclipse插件。

     

Eclipse Jetty Project开发人员更喜欢您使用   嵌入式Jetty工具可在任何IDE中进行开发/测试(甚至   IntelliJ,NetBeans,vim,emacs,JEdit等。)。