Interface WebSocket

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  WebSocket.READYSTATE
      Enum which represents the state a websocket may be in
      static class  WebSocket.Role
      Enum which represents the states a websocket may be in
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_PORT
      The default port of WebSockets, as defined in the spec.
      static int DEFAULT_WSS_PORT
      The default wss port of WebSockets, as defined in the spec.
    • Method Summary

      All Methods Instance Methods Abstract Methods Deprecated Methods 
      Modifier and Type Method Description
      void close()
      Convenience function which behaves like close(CloseFrame.NORMAL)
      void close​(int code)
      sends the closing handshake.
      void close​(int code, java.lang.String message)
      sends the closing handshake.
      void closeConnection​(int code, java.lang.String message)
      This will close the connection immediately without a proper close handshake.
      <T> T getAttachment()
      Getter for the connection attachment.
      Draft getDraft()
      Getter for the draft
      java.net.InetSocketAddress getLocalSocketAddress()
      Returns the address of the endpoint this socket is bound to.
      WebSocket.READYSTATE getReadyState()
      Retrieve the WebSocket 'readyState'.
      java.net.InetSocketAddress getRemoteSocketAddress()
      Returns the address of the endpoint this socket is connected to, ornull if it is unconnected.
      java.lang.String getResourceDescriptor()
      Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2
      If the opening handshake has not yet happened it will return null.
      boolean hasBufferedData()
      Checks if the websocket has buffered data
      boolean isClosed()
      Is the websocket in the state CLOSED
      boolean isClosing()
      Is the websocket in the state CLOSING
      boolean isConnecting()
      Deprecated.
      boolean isFlushAndClose()
      Returns true when no further frames may be submitted
      This happens before the socket connection is closed.
      boolean isOpen()
      Is the websocket in the state OPEN
      void send​(byte[] bytes)
      Send Binary data (plain bytes) to the other end.
      void send​(java.lang.String text)
      Send Text data to the other end.
      void send​(java.nio.ByteBuffer bytes)
      Send Binary data (plain bytes) to the other end.
      void sendFragmentedFrame​(Framedata.Opcode op, java.nio.ByteBuffer buffer, boolean fin)
      Allows to send continuous/fragmented frames conveniently.
      void sendFrame​(java.util.Collection<Framedata> frames)
      Send a collection of frames to the other end
      void sendFrame​(Framedata framedata)
      Send a frame to the other end
      void sendPing()
      Send a ping to the other end
      <T> void setAttachment​(T attachment)
      Setter for an attachment on the socket connection.
    • Field Detail

      • DEFAULT_PORT

        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
      • DEFAULT_WSS_PORT

        static final int DEFAULT_WSS_PORT
        The default wss port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.
        See Also:
        Constant Field Values
    • Method Detail

      • close

        void close​(int code,
                   java.lang.String message)
        sends the closing handshake. may be send in response to an other handshake.
        Parameters:
        code - the closing code
        message - the closing message
      • 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()
        Convenience function which behaves like close(CloseFrame.NORMAL)
      • closeConnection

        void closeConnection​(int code,
                             java.lang.String message)
        This will close the connection immediately without a proper close handshake. The code and the message therefore won't be transfered over the wire also they will be forwarded to onClose/onWebsocketClose.
        Parameters:
        code - the closing code
        message - the closing message
      • send

        void send​(java.lang.String text)
           throws java.nio.channels.NotYetConnectedException
        Send Text data to the other end.
        Parameters:
        text - the text data to send
        Throws:
        java.nio.channels.NotYetConnectedException - websocket is not yet connected
      • send

        void send​(java.nio.ByteBuffer bytes)
           throws java.lang.IllegalArgumentException,
                  java.nio.channels.NotYetConnectedException
        Send Binary data (plain bytes) to the other end.
        Parameters:
        bytes - the binary data to send
        Throws:
        java.lang.IllegalArgumentException - the data is null
        java.nio.channels.NotYetConnectedException - websocket is not yet connected
      • send

        void send​(byte[] bytes)
           throws java.lang.IllegalArgumentException,
                  java.nio.channels.NotYetConnectedException
        Send Binary data (plain bytes) to the other end.
        Parameters:
        bytes - the byte array to send
        Throws:
        java.lang.IllegalArgumentException - the data is null
        java.nio.channels.NotYetConnectedException - websocket is not yet connected
      • 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​(java.util.Collection<Framedata> frames)
        Send a collection of frames to the other end
        Parameters:
        frames - the frames to send to the other end
      • sendPing

        void sendPing()
               throws java.nio.channels.NotYetConnectedException
        Send a ping to the other end
        Throws:
        java.nio.channels.NotYetConnectedException - websocket is not yet connected
      • sendFragmentedFrame

        void sendFragmentedFrame​(Framedata.Opcode op,
                                 java.nio.ByteBuffer buffer,
                                 boolean fin)
        Allows to send continuous/fragmented frames conveniently.
        For more into on this frame type see 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 will received 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. Opcode.TEXT, Opcode.BINARY are allowed.
        buffer - The buffer which contains the payload. It may have no bytes remaining.
        fin - true means the current frame is the last in the sequence.
      • hasBufferedData

        boolean hasBufferedData()
        Checks if the websocket has buffered data
        Returns:
        has the websocket buffered data
      • getRemoteSocketAddress

        java.net.InetSocketAddress getRemoteSocketAddress()
        Returns the address of the endpoint this socket is connected to, ornull if it is unconnected.
        Returns:
        never returns null
      • getLocalSocketAddress

        java.net.InetSocketAddress getLocalSocketAddress()
        Returns the address of the endpoint this socket is bound to.
        Returns:
        never returns null
      • isConnecting

        @Deprecated
        boolean isConnecting()
        Deprecated.
        Is the websocket in the state CONNECTING
        Returns:
        state equals READYSTATE.CONNECTING
      • isOpen

        boolean isOpen()
        Is the websocket in the state OPEN
        Returns:
        state equals READYSTATE.OPEN
      • isClosing

        boolean isClosing()
        Is the websocket in the state CLOSING
        Returns:
        state equals READYSTATE.CLOSING
      • isFlushAndClose

        boolean isFlushAndClose()
        Returns true when no further frames may be submitted
        This happens before the socket connection is closed.
        Returns:
        true when no further frames may be submitted
      • isClosed

        boolean isClosed()
        Is the websocket in the state CLOSED
        Returns:
        state equals READYSTATE.CLOSED
      • getDraft

        Draft getDraft()
        Getter for the draft
        Returns:
        the used draft
      • getReadyState

        WebSocket.READYSTATE getReadyState()
        Retrieve the WebSocket 'readyState'. This represents the state of the connection. It returns a numerical value, as per W3C WebSockets specs.
        Returns:
        Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
      • getResourceDescriptor

        java.lang.String getResourceDescriptor()
        Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2
        If the opening handshake has not yet happened it will return null.
        Returns:
        Returns the decoded path component of this URI.
      • setAttachment

        <T> void setAttachment​(T attachment)
        Setter for an attachment on the socket connection. The attachment may be of any type.
        Type Parameters:
        T - The type of the attachment
        Parameters:
        attachment - The object to be attached to the user
        Since:
        1.3.7
      • getAttachment

        <T> T getAttachment()
        Getter for the connection attachment.
        Type Parameters:
        T - The type of the attachment
        Returns:
        Returns the user attachment
        Since:
        1.3.7