net.tootallnate.websocket
Class WebSocketServer

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

public abstract class WebSocketServer
extends java.lang.Object
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(int port)
          Creates a WebSocketServer that will attempt to listen on port port.
WebSocketServer(int port, WebSocketDraft draft)
          Creates a WebSocketServer that will attempt to listen on port port, and comply with WebSocketDraft version draft.
 
Method Summary
 WebSocket[] connections()
          Returns a WebSocket[] of currently connected clients.
 WebSocketDraft getDraft()
          Called to retrieve the Draft of this listener.
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.
 boolean onHandshakeRecieved(WebSocket conn, java.lang.String handshake, byte[] key3)
          Called by a WebSocket instance when a client connection has finished sending a handshake.
 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 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 setPort(int port)
          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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebSocketServer

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


WebSocketServer

public WebSocketServer(int port)
Creates a WebSocketServer that will attempt to listen on port port.

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

WebSocketServer

public WebSocketServer(int port,
                       WebSocketDraft draft)
Creates a WebSocketServer that will attempt to listen on port port, and comply with WebSocketDraft 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.


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.io.IOException
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.

sendToAllExcept

public void sendToAllExcept(WebSocket connection,
                            java.lang.String text)
                     throws java.io.IOException
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.

sendToAllExcept

public void sendToAllExcept(java.util.Set<WebSocket> connections,
                            java.lang.String text)
                     throws java.io.IOException
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.

connections

public WebSocket[] connections()
Returns a WebSocket[] of currently connected clients.

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

setPort

public void setPort(int port)
Sets the port that this WebSocketServer should listen on.

Parameters:
port - The port number to listen on.

getPort

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

Returns:
The port number.

getDraft

public WebSocketDraft getDraft()
Called to retrieve the Draft of this listener.


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.

onHandshakeRecieved

public boolean onHandshakeRecieved(WebSocket conn,
                                   java.lang.String handshake,
                                   byte[] key3)
                            throws java.io.IOException,
                                   java.security.NoSuchAlgorithmException
Called by a WebSocket instance when a client connection has finished sending a handshake. This method verifies that the handshake is a valid WebSocket cliend request. Then sends a WebSocket server handshake if it is valid, or closes the connection if it is not.

Parameters:
conn - The WebSocket instance who's handshake has been recieved.
handshake - The entire UTF-8 decoded handshake from the connection.
Returns:
True if the client sent a valid WebSocket handshake and this server successfully sent a 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)
Called when an entire text frame has been recieved. Do whatever you want here...

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.

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.

Parameters:
conn - The WebSocket instance this event is occuring on.

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)