A simple library to provide web sockets support to GWT.
The simplest way to use the library is to add the following dependency into the build system. i.e.
<dependency>
<groupId>org.realityforge.gwt.websockets</groupId>
<artifactId>gwt-websockets</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
Then you add the following snippet into the .gwt.xml file.
<module rename-to='myapp'>
...
<!-- Enable the websocket library -->
<inherits name="org.realityforge.gwt.websockets.WebSockets"/>
</module>
Then you can interact with the WebSocket from within the browser.
final WebSocket webSocket = WebSocket.newWebSocketIfSupported();
if ( null != webSocket )
{
webSocket.setListener( new WebSocketListenerAdapter()
{
@Override
public void onOpen( final WebSocket webSocket )
{
// After we have connected we can send
webSocket.send( "Hello!" );
}
@Override
public void onMessage( final WebSocket webSocket, final String data )
{
// After we receive a message back we can close the socket
webSocket.close();
}
} );
webSocket.connect( "ws://localhost/ws/echo" );
}
This should be sufficient to put together a simple WebSocket application.
A very simple example of this code is available in the gwt-websockets-example project.
- Consider supporting a Blob object from the HTML5 File API with send and receive.