net.tootallnate.websocket
Class WebSocket

java.lang.Object
  extended by net.tootallnate.websocket.WebSocket

public final class WebSocket
extends java.lang.Object

Represents one end (client or server) of a single WebSocket connection. Takes care of the "handshake" phase, then allows for easy sending of text frames, and recieving frames through an event-based model. This is an inner class, used by WebSocketClient and WebSocketServer, and should never need to be instantiated directly by your code. However, instances are exposed in WebSocketServer through the onClientOpen, onClientClose, onClientMessage callbacks.

Author:
Nathan Rajlich

Nested Class Summary
static class WebSocket.Role
           
 
Field Summary
static boolean DEBUG
           
static int DEFAULT_PORT
          The default port of WebSockets, as defined in the spec.
 java.util.List<Draft> known_drafts
           
 
Constructor Summary
WebSocket(net.tootallnate.websocket.WebSocketListener listener, Draft draft, java.nio.channels.SocketChannel sockchannel)
          Used in WebSocketServer and WebSocketClient.
WebSocket(net.tootallnate.websocket.WebSocketListener listener, java.util.List<Draft> drafts, java.nio.channels.SocketChannel sockchannel)
           
 
Method Summary
 void abort()
           
 void abort(java.lang.String problemmessage)
           
 void close()
          Closes the underlying SocketChannel, and calls the listener's onClose event handler.
 java.net.InetSocketAddress getLocalSocketAddress()
           
 java.net.InetSocketAddress getRemoteSocketAddress()
           
 void handleRead()
          Should be called when a Selector has a key that is writable for this WebSocket's SocketChannel connection.
 void handleWrite()
           
 Draft.HandshakeState isFlashEdgeCase(java.nio.ByteBuffer request)
           
 void send(byte[] bytes)
           
 void send(java.lang.String text)
           
 void sendFrame(Framedata framedata)
           
 void startHandshake(HandshakeBuilder handshakedata)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.

See Also:
Constant Field Values

DEBUG

public static boolean DEBUG

known_drafts

public java.util.List<Draft> known_drafts
Constructor Detail

WebSocket

public WebSocket(net.tootallnate.websocket.WebSocketListener listener,
                 Draft draft,
                 java.nio.channels.SocketChannel sockchannel)
Used in WebSocketServer and WebSocketClient.

Parameters:
socketChannel - The SocketChannel instance to read and write to. The channel should already be registered with a Selector before construction of this object.
listener - The WebSocketListener to notify of events when they occur.

WebSocket

public WebSocket(net.tootallnate.websocket.WebSocketListener listener,
                 java.util.List<Draft> drafts,
                 java.nio.channels.SocketChannel sockchannel)
Method Detail

handleRead

public void handleRead()
                throws java.lang.InterruptedException,
                       java.io.IOException
Should be called when a Selector has a key that is writable for this WebSocket's SocketChannel connection.

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

abort

public void abort()

abort

public void abort(java.lang.String problemmessage)

close

public void close()
Closes the underlying SocketChannel, and calls the listener's onClose event handler.


send

public void send(java.lang.String text)
          throws java.lang.IllegalArgumentException,
                 java.nio.channels.NotYetConnectedException,
                 java.lang.InterruptedException
Throws:
java.io.IOException
java.lang.InterruptedException
java.lang.IllegalArgumentException
java.nio.channels.NotYetConnectedException

send

public void send(byte[] bytes)
          throws java.lang.IllegalArgumentException,
                 java.nio.channels.NotYetConnectedException,
                 java.lang.InterruptedException
Throws:
java.lang.IllegalArgumentException
java.nio.channels.NotYetConnectedException
java.lang.InterruptedException

sendFrame

public void sendFrame(Framedata framedata)
               throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

handleWrite

public void handleWrite()
                 throws java.io.IOException
Throws:
java.io.IOException

isFlashEdgeCase

public Draft.HandshakeState isFlashEdgeCase(java.nio.ByteBuffer request)

startHandshake

public void startHandshake(HandshakeBuilder handshakedata)
                    throws InvalidHandshakeException,
                           java.lang.InterruptedException
Throws:
InvalidHandshakeException
java.lang.InterruptedException

getRemoteSocketAddress

public java.net.InetSocketAddress getRemoteSocketAddress()

getLocalSocketAddress

public java.net.InetSocketAddress getLocalSocketAddress()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object