net.tootallnate.websocket
Class WebSocketServer

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

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

WebSocketServer is an abstract class that only takes care of the HTTP handshake portion of WebSockets. It's up to a subclass to add functionality/purpose to the server.

Author:
Nathan Rajlich

Constructor Summary
WebSocketServer()
          Nullary constructor.
WebSocketServer(java.net.InetSocketAddress address)
          Creates a WebSocketServer that will attempt to listen on port port.
WebSocketServer(java.net.InetSocketAddress address, Draft draft)
          Creates a WebSocketServer that will attempt to listen on port port, and comply with Draft version draft.
 
Method Summary
 java.util.Set<WebSocket> connections()
          Returns a WebSocket[] of currently connected clients.
 java.net.InetSocketAddress getAddress()
           
 Draft getDraft()
           
protected  java.lang.String getFlashSecurityPolicy()
          Gets the XML string that should be returned if a client requests a Flash security policy.
 int getPort()
          Gets the port number that this server listens on.
abstract  void onClientClose(WebSocket conn)
           
abstract  void onClientMessage(WebSocket conn, java.lang.String message)
           
abstract  void onClientOpen(WebSocket conn)
           
 void onClose(WebSocket conn)
          Called after WebSocket#close is explicity called, or when the other end of the WebSocket connection is closed.
abstract  void onError(WebSocket conn, java.lang.Exception ex)
           
 void onMessage(WebSocket conn, java.lang.String message)
          Called when an entire text frame has been recieved.
 void onOpen(WebSocket conn)
          Called after onHandshakeRecieved returns true.
 void onWriteDemand(WebSocket conn)
           
 void run()
           
 void sendToAll(java.lang.String text)
          Sends text to all currently connected WebSocket clients.
 void sendToAllExcept(java.util.Set<WebSocket> connections, java.lang.String text)
          Sends text to all currently connected WebSocket clients, except for those found in the Set connections.
 void sendToAllExcept(WebSocket connection, java.lang.String text)
          Sends text to all currently connected WebSocket clients, except for the specified connection.
 void setAddress(java.net.InetSocketAddress address)
          Sets the port that this WebSocketServer should listen on.
 void start()
          Starts the server thread that binds to the currently set port number and listeners for WebSocket connection requests.
 void stop()
          Closes all connected clients sockets, then closes the underlying ServerSocketChannel, effectively killing the server socket thread and freeing the port the server was bound to.
 
Methods inherited from class net.tootallnate.websocket.WebSocketAdapter
getFlashPolicy, 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

WebSocketServer

public WebSocketServer()
                throws java.net.UnknownHostException
Nullary constructor. Creates a WebSocketServer that will attempt to listen on port WebSocket.DEFAULT_PORT.

Throws:
java.net.UnknownHostException

WebSocketServer

public WebSocketServer(java.net.InetSocketAddress address)
Creates a WebSocketServer that will attempt to listen on port port.

Parameters:
port - The port number this server should listen on.

WebSocketServer

public WebSocketServer(java.net.InetSocketAddress address,
                       Draft draft)
Creates a WebSocketServer that will attempt to listen on port port, and comply with Draft version draft.

Parameters:
port - The port number this server should listen on.
draft - The version of the WebSocket protocol that this server instance should comply to.
Method Detail

start

public void start()
Starts the server thread that binds to the currently set port number and listeners for WebSocket connection requests.

Throws:
java.lang.IllegalStateException

stop

public void stop()
          throws java.io.IOException
Closes all connected clients sockets, then closes the underlying ServerSocketChannel, effectively killing the server socket thread and freeing the port the server was bound to.

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

sendToAll

public void sendToAll(java.lang.String text)
               throws java.lang.InterruptedException
Sends text to all currently connected WebSocket clients.

Parameters:
text - The String to send across the network.
Throws:
java.io.IOException - When socket related I/O errors occur.
java.lang.InterruptedException

sendToAllExcept

public void sendToAllExcept(WebSocket connection,
                            java.lang.String text)
                     throws java.lang.InterruptedException
Sends text to all currently connected WebSocket clients, except for the specified connection.

Parameters:
connection - The WebSocket connection to ignore.
text - The String to send to every connection except connection.
Throws:
java.io.IOException - When socket related I/O errors occur.
java.lang.InterruptedException

sendToAllExcept

public void sendToAllExcept(java.util.Set<WebSocket> connections,
                            java.lang.String text)
                     throws java.lang.InterruptedException
Sends text to all currently connected WebSocket clients, except for those found in the Set connections.

Parameters:
connections -
text -
Throws:
java.io.IOException - When socket related I/O errors occur.
java.lang.InterruptedException

connections

public java.util.Set<WebSocket> connections()
Returns a WebSocket[] of currently connected clients.

Returns:
The currently connected clients in a WebSocket[].

setAddress

public void setAddress(java.net.InetSocketAddress address)
Sets the port that this WebSocketServer should listen on.

Parameters:
port - The port number to listen on.

getAddress

public java.net.InetSocketAddress getAddress()

getPort

public int getPort()
Gets the port number that this server listens on.

Returns:
The port number.

getDraft

public Draft getDraft()

run

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

getFlashSecurityPolicy

protected java.lang.String getFlashSecurityPolicy()
Gets the XML string that should be returned if a client requests a Flash security policy. The default implementation allows access from all remote domains, but only on the port that this WebSocketServer is listening on. This is specifically implemented for gitime's WebSocket client for Flash: http://github.com/gimite/web-socket-js

Returns:
An XML String that comforms to Flash's security policy. You MUST not include the null char at the end, it is appended automatically.

onMessage

public void onMessage(WebSocket conn,
                      java.lang.String message)
Called when an entire text frame has been recieved. Do whatever you want here...

Overrides:
onMessage in class WebSocketAdapter
Parameters:
conn - The WebSocket instance this event is occuring on.
message - The UTF-8 decoded message that was recieved.

onOpen

public void onOpen(WebSocket conn)
Called after onHandshakeRecieved returns true. Indicates that a complete WebSocket connection has been established, and we are ready to send/recieve data.

Overrides:
onOpen in class WebSocketAdapter
Parameters:
conn - The WebSocket instance this event is occuring on.

onClose

public void onClose(WebSocket conn)
Called after WebSocket#close is explicity called, or when the other end of the WebSocket connection is closed.

Overrides:
onClose in class WebSocketAdapter
Parameters:
conn - The WebSocket instance this event is occuring on.

onWriteDemand

public void onWriteDemand(WebSocket conn)

onClientOpen

public abstract void onClientOpen(WebSocket conn)

onClientClose

public abstract void onClientClose(WebSocket conn)

onClientMessage

public abstract void onClientMessage(WebSocket conn,
                                     java.lang.String message)

onError

public abstract void onError(WebSocket conn,
                             java.lang.Exception ex)
Overrides:
onError in class WebSocketAdapter
Parameters:
conn - may be null if the error does not belong to a single connection