我编写了一个客户端程序,该程序连接到服务器上的Websocket。我使用示例运行了tomcat8,并用我的客户端程序命中了EchoAnnotation端点。
我编写了以下端点程序:
@ServerEndpoint(value = "/websocket") public class PortServer implements AirMessageListener { public PortServer() { } @OnOpen public void start(Session session) { //do stuff } @OnClose public void end() { //do stuff } } @OnMessage public void incoming(String message) { //do stuff } @OnError public void onError(Throwable tw) throws Throwable { //do stuff }
我将其编译并创建一个名为portserver的war文件,并将其放入我的tomcat webapps目录中。然后,我将客户端程序从“连接到:”切换ws://localhost:8080/examples/websocket/echoAnnotation到“ ws://localhost:8080/portserver/websocket并运行”。我得到:
ws://localhost:8080/examples/websocket/echoAnnotation
ws://localhost:8080/portserver/websocket
Connecting to:ws://localhost:8080/portserver/websocket Exception in thread "main" com.corrisoft.air.exception.AirException: Error connecting to server at com.corrisoft.air.socket.AirSocketClient.<init>(AirSocketClient.java:60) at test.corrisoft.air.portserver.SocketConversation.<init>(SocketConversation.java:46) at test.corrisoft.air.portserver.RunPortServerTester.initConfigProperties(RunPortServerTester.java:76) at test.corrisoft.air.portserver.RunPortServerTester.<init>(RunPortServerTester.java:34) at test.corrisoft.air.portserver.RunPortServerTester.main(RunPortServerTester.java:109) Caused by: javax.websocket.DeploymentException: Handshake error. at org.glassfish.tyrus.client.ClientManager$1$1.run(ClientManager.java:466) at org.glassfish.tyrus.client.ClientManager$1.run(ClientManager.java:502) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:654) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112) at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:359) at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:195) at com.corrisoft.air.socket.AirSocketClient.<init>(AirSocketClient.java:58) ... 4 more Caused by: org.glassfish.tyrus.core.HandshakeException: Response code was not 101: 404. at org.glassfish.tyrus.core.Handshake.validateServerResponse(Handshake.java:279) at org.glassfish.tyrus.client.TyrusClientEngine.processResponse(TyrusClientEngine.java:138) at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleHandshake(GrizzlyClientFilter.java:318) at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleRead(GrizzlyClientFilter.java:288) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:291) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:209) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:137) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:115) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:550) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545) at java.lang.Thread.run(Thread.java:744)
我在我的portserver应用程序内放置了一个index.html并可以命中:http://localhost:8080/portserver很好,这意味着目录确定。然后,我验证了我的班级在我的WEB- INF / classes目录中。
http://localhost:8080/portserver
我查看了示例,发现我认为示例类可能是启用端点的“魔术”类,因此我实现了自己的示例并将其卡在jar文件中。
/** * */ package com.corrisoft.air.portserver; import java.util.HashSet; import java.util.Set; import javax.websocket.Endpoint; import javax.websocket.server.ServerApplicationConfig; import javax.websocket.server.ServerEndpointConfig; /** * @author Corrisoft Android Development */ public class WebSocketConfig implements ServerApplicationConfig { /* (non-Javadoc) * @see javax.websocket.server.ServerApplicationConfig#getAnnotatedEndpointClasses(java.util.Set) */ @Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) { // Deploy all WebSocket endpoints defined by annotations in the // web application. Filter out all others to avoid issues when running // tests on Gump Set<Class<?>> results = new HashSet<>(); for (Class<?> clazz : scanned) { if (clazz.getPackage().getName().startsWith("com.corrisoft.air")) { System.out.println("Adding endpoint for:" + clazz.getName()); results.add(clazz); } } return results; } /* (non-Javadoc) * @see javax.websocket.server.ServerApplicationConfig#getEndpointConfigs(java.util.Set) */ @Override public Set<ServerEndpointConfig> getEndpointConfigs( Set<Class<? extends Endpoint>> scanned) { return null; } }
它似乎没有在运行此类。
我错过了一些配置吗?
原来问题是我的一个依赖类从类路径中丢失了。在这种情况下,Tomcat 8不会添加端点,也不会在日志中引发异常。
我将相同的war文件部署到了tomcat 7,但出现了异常。处理类路径,直到状态良好为止,然后再部署回tomcat 8,在该处可以使用。
我在这里创建了缺陷56442:https ://issues.apache.org/bugzilla/show_bug.cgi?id = 56442是为了让tomcat吃掉异常而不是在日志中显示异常。