With the release of Jetty 7.5.0 and the latest draft 13 of the WebSocket protocol, the API for the client has be re-factored a little since my last blog on WebSocket: Server, Client and Load Test.

WebSocketClientFactory

When creating many instances of the java WebSocketClient, there is much that can be shared between multiple instances: buffer pools, thread pools and NIO selectors.  Thus the client API has been updated to use a factory pattern, where the factory can hold the configuration and instances of the common infrastructure:

WebSocketClient

Once the WebSocketClientFactory is started, WebSocketClient instances can be created and configured:

The WebSocketClient does not need to be started and the configuration set is copied to the connection instances as they are opened.

WebSocketClient.open(…)

A websocket connection can be created from a WebSocketClient by calling open and passing the URI and the websocket instance that will handle the call backs (eg onOpen, onMessage etc.):

The open call returns a Future to the WebSocket.Connection.  Like the NIO.2 API in JDK7, calling get with a timeout imposes a connect time on the connection attempt and the connection will be aborted if the get times out.   If the connection is successful, the connection returned by the get is the same object passed to the WebSocket.onOpen(Connection) callback, so it may be access and used in either way.

WebSocket.Connection

The connection instance accessed via the onOpen callback or Future.get() is used to send messages and also to configure the connection:

The  maximum message sizes are used to control how large messages can grow when they are being aggregated from multiple websocket frames.  Small max message sizes protect a server against DOS attack.

Jetty WebSocket Client API updated

25 thoughts on “Jetty WebSocket Client API updated

  • Pingback:Greg Wilkins: Jetty WebSocket Client API updated

  • Pingback:Jetty WebSocket Client API updated | Eclipse | Syngu

  • September 7, 2011 at 6:54 pm
    Permalink

    wow…sounds like excellent progress. is there any plan to allow anyone else to run this stuff (e.g. by posting a sample on a repository someplace, and a link to it at the end of the post)?

      • September 8, 2011 at 3:06 pm
        Permalink

        Example doesn’t compile due to missing classes in jetty-websocket-7.5.0-v20010901.jar (it is empty)

        • September 8, 2011 at 3:17 pm
          Permalink

          Ooops that source is now against 7.5.1-SNAPSHOT. My bad. I’ll re-release when 7.5.1 is out in the next day or two.

  • September 14, 2011 at 6:46 am
    Permalink

    I’m now try to run dotCMS on Jetty for two weeks but can’t succeed.

    It seems some questions about javax.mail.session in web.xml

    mail/MailSession

    javax.mail.Session

    Container

    The error is about these tags.
    WARN::Configuration problem at | ?mail/MailSession| ?javax.mail.Session| ?Container| : java.lang.IllegalStateException: Nothing to bind for name javax.mail.Session/default

    May you give me some hints to resolve the issue?

    I think the issue is about some missing jar file or need to be any else setting in web.xml.

    Thanks for your kindly answer.

  • September 24, 2011 at 5:33 pm
    Permalink

    Any update on the example code?

    Thanks

  • October 19, 2011 at 10:52 am
    Permalink

    How does the Jetty WebSocket implementation address support for reverse proxies?

  • October 19, 2011 at 11:57 pm
    Permalink

    Jason,

    The jetty websocket implementation makes no special provisions for reverse proxies. The websocket protocol designed to have a reasonable chance of passing through many styles of intermediaries, but there will be some that will not allow a successful websocket connection to be established.

    Hopefully once the websocket specification is final, these intermediaries will have a good motivation to be updated to support HTTP upgrade mechanism.

    Meanwhile, the best way to handle such intermediaries, is to use something like cometd.org, which can try websockets, and if that fails, it will fall back to long polling.
    We have a java client for cometd for both HTTP and websockets.

  • December 6, 2011 at 7:57 pm
    Permalink

    I just discovered that the WebSocket Client doesn’t support SSL.

    Do you anticipate ‘wss’ support being added in the near future?

    Thanks!

      • March 4, 2012 at 3:47 am
        Permalink

        Many thanks for ws4py I can cronifm that it works great with cherrypy 3.2.0 and Chrome 14.0.835.159 beta on OSX. It also works great with Firefox 7.0 on OSX, with a small modification Firefox sends Connection:keep-alive, Upgradein the initial handshake, and the current logic at ws4py/server/cherrypyserver.py:125 will only accept Connection: UpgradeI don’t know enough about the relevant standards to say who should change, but it’s an easy tweak to ws4py if you’re interested.Finally, a dumb question: how should I initialize a handler derived from WebSocketHandler? I want to pass arguments to its constructor, but since it’s instantiated by WebSocketTool I don’t see how to get to the new instance to hook it into the rest of my application.Cheers!Tim

        • April 9, 2012 at 7:50 pm
          Permalink

          This is really eitcixng stuff, and nice that Jetty has picked this up so fast, even if things are still moving around spec wise.So my interest is getting this to work with GWT. I have the above running fine, and all I’m doing extra is deploying my own Servlet to try and experiment with the websocket connection.In the above example you are using:var location = document.location.toString().replace( http:’,’ws:’);Which in my case is going to give me a GWT generated URL. That isn’t going to match against anything the server is expecting is my guess.Any tips on what my ws: url should look like? Or how to get some extra debug on the doWebSocketConnect call? so far I don’t think I’m hitting that method at all WireShark seems to hint at some websocket attempt, but I only see the request side, but not an upgrade response.Somewhere better to discuss this?

  • December 18, 2011 at 4:31 am
    Permalink

    Will the Jetty websocket client be able to handle connecting to servers which have http basic authentication enabled? It doesn’t appear that it does currently, I’m hoping it will in the future?

    • December 18, 2011 at 10:33 pm
      Permalink

      Jetty websockets can certainly handle basic auth on the handshake. It is treated as a normal HTTP request up until the 101 response is sent.

      The problems is the browsers don’t handle 401 or 302 responses for the handshake, nor supply credentials. You need to raise this with the W3c, as the protocol supports it and the browsers need to be encouraged to support it.

      cheers

      • December 19, 2011 at 9:44 pm
        Permalink

        Thanks for the reply, the jetty websocket client doesn’t provide a way to authenticate during the connection either. I was wondering if that would added. If not, maybe I’ll add it and submit it.

        That will certainly not help adoption much if none of the standard http authentication mechanisms will work with the browser websocket implementations. Hopefully they’ll end up supporting it. I’m running into problems with that currently.

        • December 19, 2011 at 11:21 pm
          Permalink

          Ah good point about our client. We should support adding arbitrary cookies and headers for such things as auth.

          A patch to that effect would be very much welcome.

          cheers

      • March 4, 2012 at 10:58 am
        Permalink

        Sylvain:Thanks for the quick response, I filed the tikcet. In the meantime, I figured-out that I can access the handler using cherrypy.request.ws_handler, and it’s working well I’ve got a simple application that delivers images to the client running on my localhost.In the long run, I need to deploy my little app behind an Apache reverse proxy that provides SSL and authentication. Any issues you’re aware of? I have some vague idea that secure sockets are required to go through a proxy, are they supported via cherrypy + ws4py?Many thanks,Tim

  • December 21, 2011 at 8:58 pm
    Permalink

    The client does support cookies, I’ll add the support into the code and submit a patch. It won’t be very hard.

  • March 1, 2012 at 1:00 am
    Permalink

    If I want to just embed the websocket client in my application without having to use the rest of the Jetty jars, if there a way to do it? I know I can figure it out by stripping out all other jars/classes and working my way up to fix the missing class failures. But if somebody has already figured out the set of mininum jars needed for this configuration, it will help me a bunch

    thanks
    Andy

  • March 21, 2012 at 5:21 am
    Permalink

    hai,
    I am using jetty websocket server version 7.5.But problem is when webpage idle in 15 min it will automatically close the connection.pls suggest me what is the problem.

    • March 21, 2012 at 12:49 pm
      Permalink

      That is because either the client or the server is implementing an idle timeout. If you want to keep it open, you need to use the server API to send some pings or other keep alive messages

      • April 8, 2012 at 9:59 am
        Permalink

        hi greg,first off, thanks for the soarwfte, your time, this blog and your replies. my previous question might be a little ambiguous. since i’m using cometd, i’m trying to tie the dao layer to the ajax layer. i’ve been experimenting with the examples for 2 whole days and have a better understanding now. i read all the docs on the jetty website, but couldn’t find any examples on how to apply the QoSFilter on the ContinuationCometServlet (since i’ll be calling the dao layer from the servlet). is this the right way to do it (or is there a better way)? any suggestions on this?thanks

  • February 27, 2013 at 10:39 am
    Permalink

    We have a java client that is behind a proxy server. I want to connect tomcat websocket implementation with our java standalone client (not on browser). Can we use proxy server with your client API to connect websocket. If you have any sample java code for this, please send us. Thanks in Advance.

Comments are closed.