users@tyrus.java.net

connectToServer should only return when connection is valid?

From: Gerard Davison <gerard.davison_at_oracle.com>
Date: Mon, 29 Apr 2013 17:18:43 +0100

Hi,

It was my impression after reading the documentation on WebSocketContainer.connectToServer(…) that this method wouldn't return until a connection was successful.

I have found that the following code will fail eventually after a few loops because the connection isn't finished yet.


              for (int i = 0; i < 100; i++) {
                ClientManager client = ClientManager.createClient();
                EchoBeanClient beanClient = new EchoBeanClient();
                
                Session session = client.connectToServer(
                        beanClient,
                        ClientEndpointConfig.Builder.create().build(),
                        URI.create("ws://localhost:8025/echo"));
                
                session.getBasicRemote().sendText("OtherHello");
                
                // Wait until things are closed down
                
                if (false)
                {
                    while (session.isOpen()) {
                        System.out.println("Waiting");
                        TimeUnit.MILLISECONDS.sleep(10);
                    }
                }
                else {
                    beanClient.latch.await();
                    session.close();
                }
                
                //
                
                System.out.println("Client session closed, presume we have a result " + session);
            }


The exception seen is on the sendText:

INFO: WebSocket server started.
Server connected SessionImpl{uri=/echo, id='b101e686-b40b-4c93-8f60-b06c8040a18d', endpoint=EndpointWrapper{endpointClass=null, endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint_at_7da150, uri='/echo', contextPath='/'}} javax.websocket.server.DefaultServerEndpointConfig_at_1e98b70
java.lang.RuntimeException: Socket is not connected.
        at org.glassfish.tyrus.container.grizzly.GrizzlyClientSocket.send(GrizzlyClientSocket.java:229)
        at org.glassfish.tyrus.server.TyrusRemoteEndpoint.sendText(TyrusRemoteEndpoint.java:101)
        at org.glassfish.tyrus.core.RemoteEndpointWrapper.sendSyncText(RemoteEndpointWrapper.java:187)
        at org.glassfish.tyrus.core.RemoteEndpointWrapper$Basic.sendText(RemoteEndpointWrapper.java:86)
        at websocket.EchoBeanMain.main(EchoBeanMain.java:42)

I am using 1.0rc3 and JDK 1.7 on Linux, is this a bug or have I misunderstood how the API should work. Interestingly the copy of the javadoc I have says that the client class should have the @ServerEndpoint annotation rather than the @ClientEndpoint annotation. Is this right?

Thanks,

Gerard