[英]How to get JSR-356 (javax.websockets.api) working on Jetty 9.3 JEE environment not embedded?
我很難嘗試在Jetty 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();
}
}
}
}
配置類:
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
編輯3:[已解決]
問題出在jetty-eclipse插件上,事實證明他們昨天在2015/10/11發行的3.9.0版本解決了這個問題。 手動部署也很棘手,但是在使用eclipse時最好使用插件。
您正在運行的服務器中沒有websocket實現類。
這就是為什么您沒有JSR356支持的原因。
您是否勾選了啟用websocket支持的選項?
注意:請注意,任何Eclipse Jetty開發人員都不會運行,管理,維護,認可您正在使用的第三方Eclipse插件( http://eclipse-jetty.github.io/ )。
Eclipse Jetty項目沒有官方的Eclipse插件。
Eclipse Jetty Project開發人員更喜歡在任何IDE(甚至是IntelliJ,NetBeans,vim,emacs,JEdit等)中使用Embedded Jetty工具進行開發/測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.