net.tootallnate.websocket
Class WebSocketClient

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

public abstract class WebSocketClient
extends java.lang.Object
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, WebSocketDraft draft)
          Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
 
Method Summary
 void 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.
 WebSocketDraft getDraft()
          Called to retrieve the Draft of this listener.
 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.
 boolean onHandshakeRecieved(WebSocket conn, java.lang.String handshake, byte[] reply)
          Parses the server's handshake to verify that it's a valid WebSocket handshake.
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 run()
           
 void send(java.lang.String text)
          Sends text to the connected WebSocket server.
 
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,
                       WebSocketDraft 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.

Parameters:
serverUri - The URI of the WebSocket server to connect to.
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 WebSocketDraft getDraft()
Called to retrieve the Draft of this listener.


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 void close()
           throws java.io.IOException
Calls close on the underlying SocketChannel, which in turn closes the socket connection, and ends the client socket thread.

Throws:
java.io.IOException - When socket related I/O errors occur.

send

public void send(java.lang.String text)
          throws java.io.IOException
Sends text to the connected WebSocket server.

Parameters:
text - The String to send to the WebSocket server.
Throws:
java.io.IOException - When socket related I/O errors occur.

run

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

onHandshakeRecieved

public boolean onHandshakeRecieved(WebSocket conn,
                                   java.lang.String handshake,
                                   byte[] reply)
                            throws java.io.IOException,
                                   java.security.NoSuchAlgorithmException
Parses the server's handshake to verify that it's a valid WebSocket handshake.

Parameters:
conn - The WebSocket instance who's handshake has been recieved. In the case of WebSocketClient, this.conn == conn.
handshake - The entire UTF-8 decoded handshake from the connection.
Returns:
true if handshake is a valid WebSocket server handshake, false otherwise.
Throws:
java.io.IOException - When socket related I/O errors occur.
java.security.NoSuchAlgorithmException

onMessage

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

Parameters:
conn -
message -

onOpen

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

Parameters:
conn -

onClose

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

Parameters:
conn -

onMessage

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

onOpen

public abstract void onOpen()

onClose

public abstract void onClose()