org.java_websocket.client
Class WebSocketClient

java.lang.Object
  extended by org.java_websocket.WebSocketAdapter
      extended by org.java_websocket.client.WebSocketClient
All Implemented Interfaces:
Runnable, WebSocketListener
Direct Known Subclasses:
WebSocketClientWrapper

public abstract class WebSocketClient
extends WebSocketAdapter
implements 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

Nested Class Summary
 class WebSocketClient.DefaultClientProxyChannel
           
static interface WebSocketClient.WebSocketClientFactory
           
 
Field Summary
protected  URI uri
          The URI this channel is supposed to connect to.
 
Constructor Summary
WebSocketClient(URI serverURI)
           
WebSocketClient(URI serverUri, Draft draft)
          Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
WebSocketClient(URI serverUri, Draft draft, Map<String,String> headers, int connecttimeout)
           
 
Method Summary
 void close()
           
 void closeBlocking()
           
 void connect()
          Starts a background thread that attempts and maintains a WebSocket connection to the URI specified in the constructor or via setURI.
 boolean connectBlocking()
          Same as connect but blocks until the websocket connected or failed to do so.
Returns whether it succeeded or not.
 ByteChannel createProxyChannel(ByteChannel towrap)
           
 WebSocket getConnection()
           
 Draft getDraft()
          Returns the protocol version this channel uses.
 InetSocketAddress getLocalSocketAddress(WebSocket conn)
           
 WebSocket.READYSTATE getReadyState()
          This represents the state of the connection.
 InetSocketAddress getRemoteSocketAddress(WebSocket conn)
           
 URI getURI()
          Gets the URI that this WebSocketClient is connected to.
 WebSocketFactory getWebSocketFactory()
           
abstract  void onClose(int code, String reason, boolean remote)
           
 void onCloseInitiated(int code, String reason)
           
 void onClosing(int code, String reason, boolean remote)
           
abstract  void onError(Exception ex)
           
 void onMessage(ByteBuffer bytes)
           
abstract  void onMessage(String message)
           
abstract  void onOpen(ServerHandshake handshakedata)
           
 void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
          Calls subclass' implementation of onClose.
 void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
          send when this peer sends a close handshake
 void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
          called as soon as no further frames are accepted
 void onWebsocketError(WebSocket conn, Exception ex)
          Calls subclass' implementation of onIOError.
 void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
          Called when an entire binary frame has been received.
 void onWebsocketMessage(WebSocket conn, String message)
          Calls subclass' implementation of onMessage.
 void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
          Calls subclass' implementation of onOpen.
 void onWriteDemand(WebSocket conn)
          This method is used to inform the selector thread that there is data queued to be written to the socket.
 void run()
           
 void send(byte[] data)
          Sends data to the connected WebSocket server.
 void send(String text)
          Sends text to the connected WebSocket server.
 void setProxy(InetSocketAddress proxyaddress)
           
 void setWebSocketFactory(WebSocketClient.WebSocketClientFactory wsf)
           
 
Methods inherited from class org.java_websocket.WebSocketAdapter
getFlashPolicy, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketMessageFragment, onWebsocketPing, onWebsocketPong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

uri

protected URI uri
The URI this channel is supposed to connect to.

Constructor Detail

WebSocketClient

public WebSocketClient(URI serverURI)

WebSocketClient

public WebSocketClient(URI serverUri,
                       Draft draft)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not attampt to connect automatically. You must call connect first to initiate the socket connection.


WebSocketClient

public WebSocketClient(URI serverUri,
                       Draft draft,
                       Map<String,String> headers,
                       int connecttimeout)
Method Detail

getURI

public URI getURI()
Gets the URI that this WebSocketClient is connected to.

Returns:
The URI for this WebSocketClient.

getDraft

public Draft getDraft()
Returns the protocol version this channel uses.


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.


connectBlocking

public boolean connectBlocking()
                        throws InterruptedException
Same as connect but blocks until the websocket connected or failed to do so.
Returns whether it succeeded or not.

Throws:
InterruptedException

close

public void close()

closeBlocking

public void closeBlocking()
                   throws InterruptedException
Throws:
InterruptedException

send

public void send(String text)
          throws NotYetConnectedException
Sends text to the connected WebSocket server.

Parameters:
text - The String to send to the WebSocket server.
Throws:
NotYetConnectedException

send

public void send(byte[] data)
          throws NotYetConnectedException
Sends data to the connected WebSocket server.

Parameters:
data - The Byte-Array of data to send to the WebSocket server.
Throws:
NotYetConnectedException

run

public void run()
Specified by:
run in interface Runnable

getReadyState

public WebSocket.READYSTATE getReadyState()
This represents the state of the connection. You can use this method instead of


onWebsocketMessage

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

Specified by:
onWebsocketMessage in interface WebSocketListener
Parameters:
conn -
message -

onWebsocketMessage

public final void onWebsocketMessage(WebSocket conn,
                                     ByteBuffer blob)
Description copied from interface: WebSocketListener
Called when an entire binary frame has been received. Do whatever you want here...

Specified by:
onWebsocketMessage in interface WebSocketListener
Parameters:
conn - The WebSocket instance this event is occurring on.
blob - The binary message that was received.

onWebsocketOpen

public final void onWebsocketOpen(WebSocket conn,
                                  Handshakedata handshake)
Calls subclass' implementation of onOpen.

Specified by:
onWebsocketOpen in interface WebSocketListener
Parameters:
conn -

onWebsocketClose

public final void onWebsocketClose(WebSocket conn,
                                   int code,
                                   String reason,
                                   boolean remote)
Calls subclass' implementation of onClose.

Specified by:
onWebsocketClose in interface WebSocketListener
Parameters:
conn -

onWebsocketError

public final void onWebsocketError(WebSocket conn,
                                   Exception ex)
Calls subclass' implementation of onIOError.

Specified by:
onWebsocketError in interface WebSocketListener
Parameters:
conn -
ex - The exception that occurred.
Might be null if the exception is not related to any specific connection. For example if the server port could not be bound.

onWriteDemand

public final void onWriteDemand(WebSocket conn)
Description copied from interface: WebSocketListener
This method is used to inform the selector thread that there is data queued to be written to the socket.

Specified by:
onWriteDemand in interface WebSocketListener

onWebsocketCloseInitiated

public void onWebsocketCloseInitiated(WebSocket conn,
                                      int code,
                                      String reason)
Description copied from interface: WebSocketListener
send when this peer sends a close handshake

Specified by:
onWebsocketCloseInitiated in interface WebSocketListener

onWebsocketClosing

public void onWebsocketClosing(WebSocket conn,
                               int code,
                               String reason,
                               boolean remote)
Description copied from interface: WebSocketListener
called as soon as no further frames are accepted

Specified by:
onWebsocketClosing in interface WebSocketListener

onCloseInitiated

public void onCloseInitiated(int code,
                             String reason)

onClosing

public void onClosing(int code,
                      String reason,
                      boolean remote)

getConnection

public WebSocket getConnection()

setWebSocketFactory

public final void setWebSocketFactory(WebSocketClient.WebSocketClientFactory wsf)

getWebSocketFactory

public final WebSocketFactory getWebSocketFactory()

getLocalSocketAddress

public InetSocketAddress getLocalSocketAddress(WebSocket conn)
Specified by:
getLocalSocketAddress in interface WebSocketListener

getRemoteSocketAddress

public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
Specified by:
getRemoteSocketAddress in interface WebSocketListener

onOpen

public abstract void onOpen(ServerHandshake handshakedata)

onMessage

public abstract void onMessage(String message)

onClose

public abstract void onClose(int code,
                             String reason,
                             boolean remote)

onError

public abstract void onError(Exception ex)

onMessage

public void onMessage(ByteBuffer bytes)

createProxyChannel

public ByteChannel createProxyChannel(ByteChannel towrap)

setProxy

public void setProxy(InetSocketAddress proxyaddress)


Copyright © 2013 Pusher. All Rights Reserved.