Package 

Class WebSocketClient

  • All Implemented Interfaces:
    com.tds.common.websocket.conn.WebSocket , com.tds.common.websocket.conn.WebSocketListener , java.lang.Runnable

    
    public abstract class WebSocketClient
    extends AbstractWebSocket implements Runnable, WebSocket
                        

    A subclass must implement at least onOpen, onClose, and onMessage to be useful. At runtime the user is expected to establish a connection via connect, then receive events like onMessage via the overloaded methods and to send data to the server.

    • Constructor Detail

      • WebSocketClient

        WebSocketClient(URI serverUri)
        Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
        Parameters:
        serverUri - the server URI to connect to
      • WebSocketClient

        WebSocketClient(URI serverUri, Draft protocolDraft)
        Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
        Parameters:
        serverUri - the server URI to connect to
        protocolDraft - The draft which should be used for this connection
      • WebSocketClient

        WebSocketClient(URI serverUri, Map<String, String> httpHeaders)
        Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
        Parameters:
        serverUri - the server URI to connect to
        httpHeaders - Additional HTTP-Headers
      • WebSocketClient

        WebSocketClient(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders)
        Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
        Parameters:
        serverUri - the server URI to connect to
        protocolDraft - The draft which should be used for this connection
        httpHeaders - Additional HTTP-Headers
      • WebSocketClient

        WebSocketClient(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders, int connectTimeout)
        Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
        Parameters:
        serverUri - the server URI to connect to
        protocolDraft - The draft which should be used for this connection
        httpHeaders - Additional HTTP-Headers
        connectTimeout - The Timeout for the connection
    • Method Detail

      • getURI

         URI getURI()

        Returns the URI that this WebSocketClient is connected to.

      • getSocket

         Socket getSocket()

        Returns the socket to allow Hostname Verification

      • setSocket

        @Deprecated() void setSocket(Socket socket)

        Accepts bound and unbound sockets. This method must be called before connect.If the given socket is not yet bound it will be bound to the uri specified in the constructor.

        Parameters:
        socket - The socket which should be used for the connection
      • setSocketFactory

         void setSocketFactory(SocketFactory socketFactory)

        Accepts a SocketFactory. This method must be called before connect. The socketwill be bound to the uri specified in the constructor.

        Parameters:
        socketFactory - The socket factory which should be used for the connection.
      • setProxy

         void setProxy(Proxy proxy)

        Method to set a proxy for this connection

        Parameters:
        proxy - the proxy to use for this websocket client
      • getDraft

         Draft getDraft()

        Returns the protocol version this channel uses. For more infos seehttps://github.com/TooTallNate/Java-WebSocket/wiki/Drafts

      • setDnsResolver

         void setDnsResolver(DnsResolver dnsResolver)

        Sets a custom DNS resolver.

        Parameters:
        dnsResolver - The DnsResolver to use.
      • addHeader

         void addHeader(String key, String value)
        Parameters:
        key - Name of the header to add.
        value - Value of the header to add.
      • reconnect

         void reconnect()

        Reinitiates the websocket connection. This method does not block.

      • reconnectBlocking

         boolean reconnectBlocking()

        Same as reconnect but blocks until the websocket reconnected or failed to doso.

      • connect

         void connect()

        Initiates the websocket connection. This method does not block.

      • connectBlocking

         boolean connectBlocking()

        Same as connect but blocks until the websocket connected or failed to do so.

      • connectBlocking

         boolean connectBlocking(long timeout, TimeUnit timeUnit)

        Same as connect but blocks with a timeout until the websocket connected or failedto do so.

        Parameters:
        timeout - The connect timeout
        timeUnit - The timeout time unit
      • close

         void close()

        Initiates the websocket close handshake. This method does not block In oder to make surethe connection is closed use closeBlocking

      • closeBlocking

         void closeBlocking()

        Same as close but blocks until the websocket closed or failed to do so.

      • send

         void send(String text)

        Sends text to the connected websocket server.

        Parameters:
        text - The string which will be transmitted.
      • send

         void send(Array<byte> data)

        Sends binary data to the connected webSocket server.

        Parameters:
        data - The byte-Array of data to send to the WebSocket server.
      • getAttachment

         <T> T getAttachment()

        Getter for the connection attachment.

      • setAttachment

         <T> void setAttachment(T attachment)

        Setter for an attachment on the socket connection. The attachment may be of any type.

        Parameters:
        attachment - The object to be attached to the user
      • sendPing

         void sendPing()

        Send a ping to the other end

      • run

         void run()
      • onWebsocketMessage

         final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)

        Called when an entire binary frame has been received. Do whatever you want here...

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        blob - The binary message that was received.
      • onWriteDemand

         final void onWriteDemand(WebSocket conn)

        This method is used to inform the selector thread that there is data queued to be written tothe socket.

        Parameters:
        conn - The WebSocket instance this event is occurring on.
      • onWebsocketClosing

         void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)

        Called as soon as no further frames are accepted

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onCloseInitiated

         void onCloseInitiated(int code, String reason)

        Send when this peer sends a close handshake

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
      • onClosing

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

        Called as soon as no further frames are accepted

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onOpen

         abstract void onOpen(ServerHandshake handshakedata)

        Called after an opening handshake has been performed and the given websocket is ready to bewritten on.

        Parameters:
        handshakedata - The handshake of the websocket instance
      • onMessage

         abstract void onMessage(String message)

        Callback for string messages received from the remote host

        Parameters:
        message - The UTF-8 decoded message that was received.
      • onClose

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

        Called after the websocket connection has been closed.

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onError

         abstract void onError(Exception ex)

        Called when errors occurs. If an error causes the websocket connection to fail will be called additionally. This method will be calledprimarily because of IO or protocol errors. If the given exception is an RuntimeExceptionthat probably means that you encountered a bug.

        Parameters:
        ex - The exception causing this error
      • onMessage

         void onMessage(ByteBuffer bytes)

        Callback for binary messages received from the remote host

        Parameters:
        bytes - The binary message that was received.
      • sendFragmentedFrame

         void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)

        Allows to send continuous/fragmented frames conveniently. For more into on this frame typesee http://tools.ietf.org/html/rfc6455#section-5.4

        If the first frame you send is also the last then it is not a fragmented frame and willreceived via onMessage instead of onFragmented even though it was send by this method.

        Parameters:
        op - This is only important for the first frame in the sequence.
        buffer - The buffer which contains the payload.
        fin - true means the current frame is the last in the sequence.
      • isOpen

         boolean isOpen()

        Is the websocket in the state OPEN

      • isFlushAndClose

         boolean isFlushAndClose()

        Returns true when no further frames may be submitted This happens before the socketconnection is closed.

      • isClosed

         boolean isClosed()

        Is the websocket in the state CLOSED

      • isClosing

         boolean isClosing()

        Is the websocket in the state CLOSING

      • hasBufferedData

         boolean hasBufferedData()

        Checks if the websocket has buffered data

      • close

         void close(int code)

        sends the closing handshake. may be send in response to an other handshake.

        Parameters:
        code - the closing code
      • close

         void close(int code, String message)

        sends the closing handshake. may be send in response to an other handshake.

        Parameters:
        code - the closing code
        message - the closing message
      • closeConnection

         void closeConnection(int code, String message)

        This will close the connection immediately without a proper close handshake. The code and themessage therefore won't be transferred over the wire also they will be forwarded toonClose/onWebsocketClose.

        Parameters:
        code - the closing code
        message - the closing message
      • send

         void send(ByteBuffer bytes)

        Send Binary data (plain bytes) to the other end.

        Parameters:
        bytes - the binary data to send
      • sendFrame

         void sendFrame(Framedata framedata)

        Send a frame to the other end

        Parameters:
        framedata - the frame to send to the other end
      • sendFrame

         void sendFrame(Collection<Framedata> frames)

        Send a collection of frames to the other end

        Parameters:
        frames - the frames to send to the other end
      • getResourceDescriptor

         String getResourceDescriptor()

        Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2If the opening handshake has not yet happened it will return null.

      • hasSSLSupport

         boolean hasSSLSupport()

        Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection

      • getSSLSession

         SSLSession getSSLSession()

        Returns the ssl session of websocket, if ssl/wss is used for this instance.

      • getProtocol

         IProtocol getProtocol()

        Returns the used Sec-WebSocket-Protocol for this websocket connection