websocket4j
Class AbstractWebSocket

java.lang.Object
  extended by websocket4j.AbstractWebSocket
Direct Known Subclasses:
WebSocket, WebSocket

public abstract class AbstractWebSocket
extends java.lang.Object

This class implements common functionality of server and client side web socket.


Field Summary
protected  java.io.InputStream in
           
protected  java.io.OutputStream out
           
protected  java.net.Socket socket
           
 
Constructor Summary
AbstractWebSocket(java.net.Socket socket, java.lang.Integer timeout, java.lang.Object... args)
          Creates new WebSocket instance using given socket for communication and limiting handshake time to given milliseconds.
 
Method Summary
protected  java.lang.String byteCollectionToString(java.util.Collection<java.lang.Byte> collection)
          Creates a string from given byte collection.
 void close()
          Closes the connection.
 java.lang.String getMessage()
          Gets a message form the other party.
protected abstract  void handshake(java.lang.Object... args)
          Performs an opening handshake.
 boolean isClosed()
          Checks if this socket is closed.
protected  byte[] makeResponseToken(int key1, int key2, byte[] token)
          Generates response to client's opening handshake challenge.
protected  byte[] readBytes(java.lang.Integer count)
          Reads given number of bytes from the socket.
protected  java.lang.String readLine()
          Reads line (terminated by "\r\n") from the socket.
 void sendMessage(java.lang.String message)
          Sends a message to the other party.
protected  void writeLine(java.lang.String line)
          Writes given line to socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

socket

protected java.net.Socket socket

in

protected java.io.InputStream in

out

protected java.io.OutputStream out
Constructor Detail

AbstractWebSocket

public AbstractWebSocket(java.net.Socket socket,
                         java.lang.Integer timeout,
                         java.lang.Object... args)
                  throws java.io.IOException
Creates new WebSocket instance using given socket for communication and limiting handshake time to given milliseconds.

Parameters:
socket - socket that should be used for communication
timeout - maximum time in milliseconds for the handshake
args - arguments that will be passed to handshake call
Throws:
java.io.IOException - exception thrown when there is communication error or protocol error
Method Detail

byteCollectionToString

protected java.lang.String byteCollectionToString(java.util.Collection<java.lang.Byte> collection)
Creates a string from given byte collection.

Parameters:
collection - collection to be converted
Returns:
string made from the byte collection

close

public void close()
           throws java.io.IOException
Closes the connection. Close is brutal, without the closing handshake. It worked better in some web browsers, but it might change sometime. TODO: do a proper closing handshake sometime

Throws:
java.io.IOException

getMessage

public java.lang.String getMessage()
                            throws java.io.IOException
Gets a message form the other party. Blocks if nothing is available.

Returns:
message from the other party
Throws:
java.io.IOException - thrown when there is a problem with the connection or protocol

handshake

protected abstract void handshake(java.lang.Object... args)
                           throws java.io.IOException,
                                  java.security.NoSuchAlgorithmException
Performs an opening handshake. If this method completes successfully, a connection with the other party is established.

Throws:
java.io.IOException - thrown when there is a problem in socket communication or client does not follow the handshake protocol
java.security.NoSuchAlgorithmException - thrown when JVM doesn't support MD5 algorithm

isClosed

public boolean isClosed()
Checks if this socket is closed.

Returns:
true if the socket is closed

readBytes

protected byte[] readBytes(java.lang.Integer count)
                    throws java.io.IOException
Reads given number of bytes from the socket.

Parameters:
count - number of sockets to read
Returns:
bytes that were read
Throws:
java.io.IOException - thrown when an error occurs while reading

readLine

protected java.lang.String readLine()
                             throws java.io.IOException
Reads line (terminated by "\r\n") from the socket.

Returns:
line that was read
Throws:
java.io.IOException - thrown when an error occurs while reading

sendMessage

public void sendMessage(java.lang.String message)
                 throws java.io.IOException
Sends a message to the other party.

Parameters:
message - message to be sent
Throws:
java.io.IOException - thrown when there is a problem with the connection.

writeLine

protected void writeLine(java.lang.String line)
                  throws java.io.IOException
Writes given line to socket. Line is terminated by "\r\n".

Parameters:
line - line to be written
Throws:
java.io.IOException - thrown when an error occurs while writing

makeResponseToken

protected byte[] makeResponseToken(int key1,
                                   int key2,
                                   byte[] token)
                            throws java.security.NoSuchAlgorithmException
Generates response to client's opening handshake challenge.

Parameters:
key1 - value of Sec-WebSocket-Key1 field
key2 - value of Sec-WebSocket-Key2 field
token - bytes sent after the fields
Returns:
response to this challenge
Throws:
java.security.NoSuchAlgorithmException - thrown if JVM doesn't support MD5 algorithm