Can't connect to my tomcat 8 websocket

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have written a client program that connects to my websocket on the server. I set up tomcat8 with the examples working and hit the EchoAnnotation endpoint with my client program.

I wrote this endpoint program as follows:

@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 } 

I compile this and create a war file called portserver and drop it into my tomcat webapps directory. I then switched my client program from connecting to: ws://localhost:8080/examples/websocket/echoAnnotation to ws://localhost:8080/portserver/websocket and run it. I get:

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) 

I placed an index.html inside my portserver app and can hit: http://localhost:8080/portserver just fine, which means the directories are OK. I then verified that my class was in my WEB-INF/classes directory.

I looked at the examples and found the ExamplesConfig class that I thought might be a "magic" class that enables the endpoints, so I implemented my own and and stuck in the jar file.

/**  *   */ 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;     } } 

It does not seem to be running this class.

Is there some configuration I missed?

回答1:

Turns out that the problem was that one of my dependent classes was missing from the classpath. Tomcat 8, under these circumstances, doesn't add the endpoint and doesn't throw an exception into the log.

I deployed the same war file to tomcat 7 and got an exception. Worked the classpath until it was good and then deployed back to tomcat 8 where it is now working.

I created defect 56442 here: https://issues.apache.org/bugzilla/show_bug.cgi?id=56442 for tomcat eating the exception instead of displaying in the log.



回答2:

For anyone else plagued by this; take a CLOSE look at your URI. I was piecing my url together, based on a configuration file. I missed a single "/" character when constructing the URL, and was convinced that it was correct! If you do stuff like the following, I suggest, printing out the "constructed URL" and studying that closely before chasing your tail:

public static final String WEBSOCKETHOST = "localhost";         // TODO: Get from configuration public static final int WEBSOCKETPORT = 10080;                  // TODO: Get from configuration public static final String WEBSOCKETSERVERROOT = "/sceagents";  // TODO: Get from configuration public static final String WEBSOCKETSERVERENDPOINT = "neo";     // TODO: Get from configuration public static final String WEBSOCKETPROTOCOL = "ws";            // TODO: Get from configuration  String uri = WEBSOCKETPROTOCOL + "://" + WEBSOCKETHOST + ":" + Integer.toString(WEBSOCKETPORT) + WEBSOCKETSERVERROOT + "/" + WEBSOCKETSERVERENDPOINT; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!