gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:53:08 24 4
gpt4 key购买 nike

我很难尝试在 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 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..).

关于java - 如何让 JSR-356 (javax.websockets.api) 在未嵌入的 Jetty 9.3 JEE 环境中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33065600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com