Class WebSocketImpl

  • All Implemented Interfaces:
    WebSocket

    public class WebSocketImpl
    extends Object
    implements WebSocket
    Represents one end (client or server) of a single WebSocketImpl connection. Takes care of the "handshake" phase, then allows for easy sending of text frames, and receiving frames through an event-based model.
    • Field Detail

      • RCVBUF

        public static int RCVBUF
      • DEBUG

        public static boolean DEBUG
        Activate debug mode for additional infos
      • channel

        public ByteChannel channel
        the possibly wrapped channel object whose selection is controlled by key
      • workerThread

        public volatile WebSocketServer.WebSocketWorker workerThread
        Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
    • Constructor Detail

      • WebSocketImpl

        public WebSocketImpl​(WebSocketListener listener,
                             List<Draft> drafts)
        Creates a websocket with server role
        Parameters:
        listener - The listener for this instance
        drafts - The drafts which should be used
      • WebSocketImpl

        public WebSocketImpl​(WebSocketListener listener,
                             Draft draft)
        creates a websocket with client role
        Parameters:
        listener - The listener for this instance
        draft - The draft which should be used
    • Method Detail

      • decode

        public void decode​(ByteBuffer socketBuffer)
        Method to decode the provided ByteBuffer
        Parameters:
        socketBuffer - the ByteBuffer to decode
      • close

        public void close​(int code,
                          String message,
                          boolean remote)
      • close

        public void close​(int code,
                          String message)
        Description copied from interface: WebSocket
        sends the closing handshake. may be send in response to an other handshake.
        Specified by:
        close in interface WebSocket
        Parameters:
        code - the closing code
        message - the closing message
      • closeConnection

        public void closeConnection​(int code,
                                    String message,
                                    boolean remote)
        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
        remote - Indicates who "generated" code.
        true means that this endpoint received the code from the other endpoint.
        false means this endpoint decided to send the given code,
        remote may also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the code but close the connection the same time this endpoint does do but with an other code.
      • closeConnection

        protected void closeConnection​(int code,
                                       boolean remote)
      • closeConnection

        public void closeConnection()
      • closeConnection

        public void closeConnection​(int code,
                                    String message)
        Description copied from interface: WebSocket
        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.
        Specified by:
        closeConnection in interface WebSocket
        Parameters:
        code - the closing code
        message - the closing message
      • flushAndClose

        public void flushAndClose​(int code,
                                  String message,
                                  boolean remote)
      • eot

        public void eot()
      • close

        public void close​(int code)
        Description copied from interface: WebSocket
        sends the closing handshake. may be send in response to an other handshake.
        Specified by:
        close in interface WebSocket
        Parameters:
        code - the closing code
      • sendFragmentedFrame

        public void sendFragmentedFrame​(Framedata.Opcode op,
                                        ByteBuffer buffer,
                                        boolean fin)
        Description copied from interface: WebSocket
        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.
        Specified by:
        sendFragmentedFrame in interface WebSocket
        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.
      • sendFrame

        public void sendFrame​(Collection<Framedata> frames)
        Description copied from interface: WebSocket
        Send a collection of frames to the other end
        Specified by:
        sendFrame in interface WebSocket
        Parameters:
        frames - the frames to send to the other end
      • sendFrame

        public void sendFrame​(Framedata framedata)
        Description copied from interface: WebSocket
        Send a frame to the other end
        Specified by:
        sendFrame in interface WebSocket
        Parameters:
        framedata - the frame to send to the other end
      • hasBufferedData

        public boolean hasBufferedData()
        Description copied from interface: WebSocket
        Checks if the websocket has buffered data
        Specified by:
        hasBufferedData in interface WebSocket
        Returns:
        has the websocket buffered data
      • isConnecting

        @Deprecated
        public boolean isConnecting()
        Deprecated.
        Description copied from interface: WebSocket
        Is the websocket in the state CONNECTING
        Specified by:
        isConnecting in interface WebSocket
        Returns:
        state equals READYSTATE.CONNECTING
      • isOpen

        public boolean isOpen()
        Description copied from interface: WebSocket
        Is the websocket in the state OPEN
        Specified by:
        isOpen in interface WebSocket
        Returns:
        state equals READYSTATE.OPEN
      • isClosing

        public boolean isClosing()
        Description copied from interface: WebSocket
        Is the websocket in the state CLOSING
        Specified by:
        isClosing in interface WebSocket
        Returns:
        state equals READYSTATE.CLOSING
      • isFlushAndClose

        public boolean isFlushAndClose()
        Description copied from interface: WebSocket
        Returns true when no further frames may be submitted
        This happens before the socket connection is closed.
        Specified by:
        isFlushAndClose in interface WebSocket
        Returns:
        true when no further frames may be submitted
      • isClosed

        public boolean isClosed()
        Description copied from interface: WebSocket
        Is the websocket in the state CLOSED
        Specified by:
        isClosed in interface WebSocket
        Returns:
        state equals READYSTATE.CLOSED
      • getReadyState

        public WebSocket.READYSTATE getReadyState()
        Description copied from interface: WebSocket
        Retrieve the WebSocket 'readyState'. This represents the state of the connection. It returns a numerical value, as per W3C WebSockets specs.
        Specified by:
        getReadyState in interface WebSocket
        Returns:
        Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • getRemoteSocketAddress

        public InetSocketAddress getRemoteSocketAddress()
        Description copied from interface: WebSocket
        Returns the address of the endpoint this socket is connected to, ornull if it is unconnected.
        Specified by:
        getRemoteSocketAddress in interface WebSocket
        Returns:
        never returns null
      • getDraft

        public Draft getDraft()
        Description copied from interface: WebSocket
        Getter for the draft
        Specified by:
        getDraft in interface WebSocket
        Returns:
        the used draft
      • close

        public void close()
        Description copied from interface: WebSocket
        Convenience function which behaves like close(CloseFrame.NORMAL)
        Specified by:
        close in interface WebSocket
      • getResourceDescriptor

        public String getResourceDescriptor()
        Description copied from interface: WebSocket
        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.
        Specified by:
        getResourceDescriptor in interface WebSocket
        Returns:
        Returns the decoded path component of this URI.
      • updateLastPong

        public void updateLastPong()
        Update the timestamp when the last pong was received
      • getWebSocketListener

        public WebSocketListener getWebSocketListener()
        Getter for the websocket listener
        Returns:
        the websocket listener associated with this instance
      • getAttachment

        public <T> T getAttachment()
        Description copied from interface: WebSocket
        Getter for the connection attachment.
        Specified by:
        getAttachment in interface WebSocket
        Type Parameters:
        T - The type of the attachment
        Returns:
        Returns the user attachment
      • setAttachment

        public <T> void setAttachment​(T attachment)
        Description copied from interface: WebSocket
        Setter for an attachment on the socket connection. The attachment may be of any type.
        Specified by:
        setAttachment in interface WebSocket
        Type Parameters:
        T - The type of the attachment
        Parameters:
        attachment - The object to be attached to the user