Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] Jetty-9 WebSocket API Usage.

The WebSocketClient.connect() is just gets you through the upgrade process on the HTTP connection layer.
You want to wait for the socket's onConnect() method to tell you when it is safe to start writing content to the socket.

I added an example to show this behavior.
http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/SimpleEchoClient.java?h=jetty-9

The behavior of WebSocketClient.connect() could probably be adjusted, we are still in early Milestone/SNAPSHOT releases after all.
Got a compelling argument one way or the other about this behavior?

--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
www.webtide.com
Developer advice, services and support
from the Jetty & CometD experts.



On Tue, Oct 2, 2012 at 8:52 PM, Vladimir Tsanev <tsachev@xxxxxxxxx> wrote:
Hi I am trying to adopt jetty-9 web socket library. I am using the current snapshot version.

I have a problem with getting the connection after connect on client side.

I have created a class which extends the WebSocketAdapter.
But WebSocketAdapter.onWebSocketConnect() seems to be invoked after the completion of the get method shown below.

Basically the code looks like this.

WebSocketClient client  = factory.newWebSocketClient(myAdapter);
UpgradeResponse response = client.connect(uri).get(timeout, TimeUnit.MILISECONDS);

// If I sleep a little more here everything is ok.
myAdapter.getConnection().write(...);

If am too fast with using the connection it could be null.

I found that the code that notifies the WebSocketListener (myAdapter) and  unblocks the upgrade request future is in the UpgradeConnection#read() method when response is got.
More specifically 
  notifyConnect - unblocks (completes) the future
  upgradeConnection - calls the connection.onOpen and down the road onWebSocketConnect of myAdapter.

If I change the code there to invoke first upgradeConnection and then the notifiyConnect method everything works as expected on my side,
also all test seems to pass. 

Can someone, please fix this or if it is intended to be so and I missed something point me to way to handle this.

--
Vlado



_______________________________________________
jetty-dev mailing list
jetty-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-dev



Back to the top