net.tootallnate.websocket
Class WebSocketClient

java.lang.Object
  extended by net.tootallnate.websocket.WebSocketAdapter
      extended by net.tootallnate.websocket.WebSocketClient
All Implemented Interfaces:
java.lang.Runnable

public abstract class WebSocketClient
extends WebSocketAdapter
implements java.lang.Runnable

The WebSocketClient is an abstract class that expects a valid "ws://" URI to connect to. When connected, an instance recieves important events related to the life of the connection. A subclass must implement onOpen, onClose, and onMessage to be useful. An instance can send messages to it's connected server via the send method.

Author:
Nathan Rajlich

Constructor Summary
WebSocketClient(java.net.URI serverURI)
           
WebSocketClient(java.net.URI serverUri, Draft draft)
          Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
 
Method Summary
 java.lang.Thread close()
          Calls close on the underlying SocketChannel, which in turn closes the socket connection, and ends the client socket thread.
 void connect()
          Starts a background thread that attempts and maintains a WebSocket connection to the URI specified in the constructor or via setURI.
 Draft getDraft()
           
 java.net.URI getURI()
          Gets the URI that this WebSocketClient is connected to.
abstract  void onClose()
           
 void onClose(WebSocket conn)
          Calls subclass' implementation of onClose.
abstract  void onError(java.lang.Exception ex)
           
 void onError(WebSocket conn, java.io.IOException ex)
          Calls subclass' implementation of onIOError.
abstract  void onMessage(java.lang.String message)
           
 void onMessage(WebSocket conn, java.lang.String message)
          Calls subclass' implementation of onMessage.
abstract  void onOpen()
           
 void onOpen(WebSocket conn)
          Calls subclass' implementation of onOpen.
 void onWriteDemand(WebSocket conn)
           
 void run()
           
 void send(java.lang.String text)
          Sends text to the connected WebSocket server.
 
Methods inherited from class net.tootallnate.websocket.WebSocketAdapter
getFlashPolicy, onError, onHandshakeRecievedAsClient, onHandshakeRecievedAsServer, onMessage, onPing, onPong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebSocketClient

public WebSocketClient(java.net.URI serverURI)

WebSocketClient

public WebSocketClient(java.net.URI serverUri,
                       Draft draft)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The client does not attampt to connect automatically. You must call connect first to initiate the socket connection.

Method Detail

getURI

public java.net.URI getURI()
Gets the URI that this WebSocketClient is connected to.

Returns:
The URI for this WebSocketClient.

getDraft

public Draft getDraft()

connect

public void connect()
Starts a background thread that attempts and maintains a WebSocket connection to the URI specified in the constructor or via setURI. setURI.


close

public java.lang.Thread close()
Calls close on the underlying SocketChannel, which in turn closes the socket connection, and ends the client socket thread.


send

public void send(java.lang.String text)
          throws java.nio.channels.NotYetConnectedException,
                 java.lang.InterruptedException
Sends text to the connected WebSocket server.

Parameters:
text - The String to send to the WebSocket server.
Throws:
java.nio.channels.NotYetConnectedException
java.lang.InterruptedException

run

public void run()
Specified by:
run in interface java.lang.Runnable

onMessage

public void onMessage(WebSocket conn,
                      java.lang.String message)
Calls subclass' implementation of onMessage.

Overrides:
onMessage in class WebSocketAdapter
Parameters:
conn -
message -

onOpen

public void onOpen(WebSocket conn)
Calls subclass' implementation of onOpen.

Overrides:
onOpen in class WebSocketAdapter
Parameters:
conn -

onClose

public void onClose(WebSocket conn)
Calls subclass' implementation of onClose.

Overrides:
onClose in class WebSocketAdapter
Parameters:
conn -

onError

public void onError(WebSocket conn,
                    java.io.IOException ex)
Calls subclass' implementation of onIOError.

Parameters:
conn -

onWriteDemand

public void onWriteDemand(WebSocket conn)

onMessage

public abstract void onMessage(java.lang.String message)

onOpen

public abstract void onOpen()

onClose

public abstract void onClose()

onError

public abstract void onError(java.lang.Exception ex)