Web Sockets with Google Web Toolkit, Tomcat 7, and Chrome browser

When I started to work on a Web Sockets prototype to connect a Google Web Toolkit (GWT) application with a Tomcat application server, it was surprisingly hard to find a detailed tutorial. In the following, I will give an small example that can be used as starting point for your application.

On the server side, we use Tomcat as application server. As of version 7.0.29, Tomcat supports the Web Sockets protocol as defined in RFC 6455. Instead of a implementing a HTTP servlet with methods to process GET and POST messages, we now have to extend class WebSocketServlet and override method createWebSocketInbound:

1
2
3
4
5
6
7
8
public class RealtimeQueries extends WebSocketServlet
{
     @Override
     protected StreamInbound createWebSocketInbound( final String subProtocol, final HttpServletRequest request )
     {
          return new MyWebSocketInbound( );
     }
}

Class MyWebSocketInbound is responsible to handle the communication to one client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MyWebSocketInbound extends MessageInbound
{
     @Override
     protected void onOpen( WsOutbound outbound )
     {
     }
 
     @Override
     protected void onClose( int status )
     {
     }
 
     @Override
     protected void onBinaryMessage( final ByteBuffer message )
     { 
     }
 
     @Override
     protected void onTextMessage( final CharBuffer message ) throws IOException
     {
     }
}

Of course, you have to implement all method according to your application. In order to test the servlet, we need some Java implementation. After having check a few examples, I chose Java-WebSocket for this. The implementation is again very simple; the most important point is to select Draft_17, because this stands for RFC 6455:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
final URI uri = new URI( "ws://localhost:8080/ws/test" );
final WebSocketClient ws = new WebSocketClient( uri, new Draft_17( ) )
{
    @Override
    public void onOpen( ServerHandshake arg0 )
    {
        this.send( "Help" );
    }
 
    @Override
    public void onMessage( String arg0 )
    {
    }
 
    @Override
    public void onError( Exception arg0 )
    {
    }
 
    @Override
    public void onClose( int arg0, String arg1, boolean arg2 )
    {
    }
};
 
ws.connect( );

In the last line, the connection is opened. After the connection has been established, method onOpen is called. There, we send a first message to the server. By the way, the URL ends on „ws/test“ which is the path under which the servlet has been registered.

Finally, for the GWT frontend, we use the WebSockets implementation of project gwt-comet:

1
2
3
4
5
6
7
8
9
final WebSocket ws = WebSocket.create( "ws://localhost:8080/rtm/ws" );
ws.setOnOpen( new OpenHandler( )
{
    @Override
    public void onOpen( WebSocket webSocket )
    {
        ws.send( "Help" );
    }
} );

For further documentation, have a look at the project’s Web page.

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


*