Class InternalWebSocket

  • All Implemented Interfaces:
    IWebSocketComm

    @WebSocket(maxBinaryMessageSize=10000000,
               maxTextMessageSize=15000000,
               idleTimeout=60000)
    public class InternalWebSocket
    extends java.lang.Object
    implements IWebSocketComm
    The internal web socket for the server side.
    Author:
    Christopher Mindus
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      booleanareBinaryDataFramesSupported()
      Checks if Binary Data Frames are supported.
      voidclose()
      Call this method to close the socket.
      booleanclose​(boolean rightNow)
      Call this method to close the socket.
      voidclose​(int code, java.lang.String reason)
      Call this method to close the socket.
      java.lang.ObjectgetAttachment()
      Gets the WebSocket attachment object, null for none.
      java.lang.StringgetDomain()
      Gets the request domain.
      intgetPort()
      Gets the port number for the connection to the server.
      java.lang.StringgetRemoteAddr()
      Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
      java.lang.StringgetRemoteHost()
      Returns the fully qualified name of the client or the last proxy that sent the request.
      java.net.SocketAddressgetSocketAddress()
      Returns the remote address to which this channel's socket is connected.
      java.lang.StringgetSubProtocol()
      Gets the negotiated and accepted sub-protocol, null for none (yet), or none is provided by the parties (server or client).
      booleanisClosed()
      Checks if the connection is disposed of.
      booleanisClosing()
      Checks if the connection is currently closing.
      booleanisConnected()
      Checks if the connection is established.
      booleanisConnecting()
      Checks if the connection is connecting.
      booleanisSecure()
      Return if the connection is secured using SSL/TLS.
      voidonWebSocketBinary​(byte[] buf, int offset, int len)
      Called for a binary message.
      voidonWebSocketClose​(int code, java.lang.String reason)
      Called when disconnected.
      voidonWebSocketConnect​(org.eclipse.jetty.websocket.api.Session s)
      Called when connected.
      voidonWebSocketError​(java.lang.Throwable error)
      Called when an error occurs.
      voidonWebSocketFrame​(org.eclipse.jetty.websocket.api.Frame frame)
      Called when a frame is received.
      voidonWebSocketText​(java.lang.String text)
      Called for a text message.
      voidsend​(byte[] data)
      Sends binary data.
      voidsend​(java.lang.String data)
      Sends a String of data.
      booleansendHeartBeat()
      Sends a heart-beat.
      voidsetAttachment​(java.lang.Object attachment)
      Gets the WebSocket attachment object, null for none.
      voidsetListener​(WebSocketCommListener listener)
      Assigns the listener.
      voidsetSubProtocol​(java.lang.String subProtocol)
      Assigns a sub-protocol for the WebSocket (in case the processing of sub-protocol negotiation is done e.g.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getDomain

        public java.lang.String getDomain()
        Gets the request domain.
        Specified by:
        getDomain in interface IWebSocketComm
        Returns:
        The domain name of the server.
      • getPort

        public int getPort()
        Gets the port number for the connection to the server.
        Specified by:
        getPort in interface IWebSocketComm
        Returns:
        The port number.
      • isSecure

        public boolean isSecure()
        Return if the connection is secured using SSL/TLS.
        Specified by:
        isSecure in interface IWebSocketComm
        Returns:
        The secure flag. Please note that if the server is behind a proxy, the connection might be secure anyway.
      • onWebSocketConnect

        @OnWebSocketConnect
        public void onWebSocketConnect​(org.eclipse.jetty.websocket.api.Session s)
        Called when connected.
      • onWebSocketBinary

        @OnWebSocketMessage
        public void onWebSocketBinary​(byte[] buf,
                                      int offset,
                                      int len)
        Called for a binary message.
      • onWebSocketText

        @OnWebSocketMessage
        public void onWebSocketText​(java.lang.String text)
        Called for a text message.
      • onWebSocketClose

        @OnWebSocketClose
        public void onWebSocketClose​(int code,
                                     java.lang.String reason)
        Called when disconnected.
      • onWebSocketError

        @OnWebSocketError
        public void onWebSocketError​(java.lang.Throwable error)
        Called when an error occurs.
      • onWebSocketFrame

        @OnWebSocketFrame
        public void onWebSocketFrame​(org.eclipse.jetty.websocket.api.Frame frame)
        Called when a frame is received.

        Flags one method in the class as receiving Frame events from the WebSocket implementation after they have been processed by any extensions declared during the Upgrade handshake.

      • isConnecting

        public boolean isConnecting()
        Checks if the connection is connecting.
        Specified by:
        isConnecting in interface IWebSocketComm
        Returns:
        true if the connection is pending.
      • isConnected

        public boolean isConnected()
        Checks if the connection is established.
        Specified by:
        isConnected in interface IWebSocketComm
        Returns:
        true if the connection is established.
      • isClosing

        public boolean isClosing()
        Checks if the connection is currently closing.
        Specified by:
        isClosing in interface IWebSocketComm
        Returns:
        true if not closed, but closing.
      • isClosed

        public boolean isClosed()
        Checks if the connection is disposed of.
        Specified by:
        isClosed in interface IWebSocketComm
        Returns:
        true if closed, not closing.
      • close

        public void close()
        Call this method to close the socket.
        Specified by:
        close in interface IWebSocketComm
      • close

        public void close​(int code,
                          java.lang.String reason)
        Call this method to close the socket.
        Specified by:
        close in interface IWebSocketComm
        Parameters:
        code - A close code: zero for none, otherwise a code 1000 or range 3000 to 4999.
        reason - The reason code: if code is zero, null applies.
      • close

        public boolean close​(boolean rightNow)
        Call this method to close the socket.
        Specified by:
        close in interface IWebSocketComm
        Parameters:
        rightNow - If true, the socket is hard-closed, i.e. no close conversation takes place with the server. If false, the client will send a close message to server that replies with a close itself, then the socket is closed.
        Returns:
        true if closed, false if already closed.
      • setSubProtocol

        public void setSubProtocol​(java.lang.String subProtocol)
                            throws java.lang.IllegalStateException
        Assigns a sub-protocol for the WebSocket (in case the processing of sub-protocol negotiation is done e.g. outside the WebSocket communication acceptor).
        Specified by:
        setSubProtocol in interface IWebSocketComm
        Throws:
        java.lang.IllegalStateException - If the state doesn't permit this operation, or if the implementation doesn't allow it.
      • getSubProtocol

        public java.lang.String getSubProtocol()
        Gets the negotiated and accepted sub-protocol, null for none (yet), or none is provided by the parties (server or client).
        Specified by:
        getSubProtocol in interface IWebSocketComm
        Returns:
        The String for the sub-protocol negotiated, null for none.
      • areBinaryDataFramesSupported

        public boolean areBinaryDataFramesSupported()
        Checks if Binary Data Frames are supported.
        Specified by:
        areBinaryDataFramesSupported in interface IWebSocketComm
        Returns:
        true if it is supported, false otherwise.
      • send

        public void send​(byte[] data)
                  throws java.io.IOException
        Sends binary data.
        Specified by:
        send in interface IWebSocketComm
        Parameters:
        data - The byte array containing the data.
        Throws:
        java.io.IOException - for failures, e.g. socket is closed, not open, not connected.
      • send

        public void send​(java.lang.String data)
                  throws java.io.IOException
        Sends a String of data.
        Specified by:
        send in interface IWebSocketComm
        Parameters:
        data - The String to send.
        Throws:
        java.io.IOException - for failures, e.g. socket is closed, not open, not connected.
      • sendHeartBeat

        public boolean sendHeartBeat()
                              throws java.io.IOException
        Sends a heart-beat.
        Specified by:
        sendHeartBeat in interface IWebSocketComm
        Returns:
        true for success, false for failure (e.g. closing state or closed).
        Throws:
        java.io.IOException - For send failures.
      • getRemoteAddr

        public java.lang.String getRemoteAddr()
        Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR. The IP address format depends if it is an IPv4 or IPv6 address.
        Specified by:
        getRemoteAddr in interface IWebSocketComm
        Returns:
        a String containing the IP address of the client that sent the request.
      • getRemoteHost

        public java.lang.String getRemoteHost()
        Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the hostname (to improve performance), this method returns the dotted-string form of the IP address (if IPv4, colon'ed-string for IPv6). For HTTP servlets, same as the value of the CGI variable REMOTE_HOST.
        Specified by:
        getRemoteHost in interface IWebSocketComm
        Returns:
        a String containing the fully qualified name of the client.
      • getSocketAddress

        public java.net.SocketAddress getSocketAddress()
        Returns the remote address to which this channel's socket is connected.

        Where the channel is bound and connected to an Internet Protocol socket address then the return value from this method is of type InetSocketAddress.

        Specified by:
        getSocketAddress in interface IWebSocketComm
        Returns:
        The remote address; null if the channel's socket is not connected.
      • getAttachment

        public java.lang.Object getAttachment()
        Gets the WebSocket attachment object, null for none.
        Specified by:
        getAttachment in interface IWebSocketComm
        Returns:
        An object set by setAttachment.
      • setAttachment

        public void setAttachment​(java.lang.Object attachment)
        Gets the WebSocket attachment object, null for none.
        Specified by:
        setAttachment in interface IWebSocketComm
        Parameters:
        attachment - The attachment object for the web socket.