我很难使用javax获得一个简单的echo服务器。jetty 9.3 servlet上的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();
}
}
}
}
配置类:
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插件运行,但下面是我在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
Edit 3: [SOLVED]
问题是jetty-eclipse插件,结果是他们昨天发布的3.9.0版本(2015/10/11)解决了这个问题。手动部署也很棘手,但在使用eclipse时使用插件还是不错的。
当前运行的服务器中没有websocket实现类
这就是为什么不支持JSR356的原因。
你是否勾选了启用websocket支持的选项?
注意:要知道你使用的是第三方Eclipse插件(http://eclipse-jetty.github.io/)没有运行,管理,维护,被任何Eclipse Jetty开发人员认可和推荐。
Eclipse Jetty项目没有正式的Eclipse Plugin.
Eclipse Jetty Project开发人员更喜欢使用嵌入式Jetty工具可以在任何IDE(甚至IntelliJ, NetBeans, vim, emacs, JEdit等)。