Package com.iizix.nio

Class WebSocketCommNIO

java.lang.Object
com.iizix.nio.WebSocketCommNIO
All Implemented Interfaces:
IWebSocketComm
Direct Known Subclasses:
WebSocketComm

public class WebSocketCommNIO extends Object implements IWebSocketComm
Class to handle the WebSocket protocol.

The WebSocket Client pure socket implementation. This implementation is based on RFC 6455 "The WebSocket Protocol", also supporting the older "draft-ietf-hybi-thewebsocketprotocol-00" also called "Hixie 75/76" versions.

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g., using XMLHttpRequest or >iframe<s and long polling).

To be implemented (fragmentation compliance with RFC 6455).

Author:
Christopher Mindus
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final String
    The request URI.
  • Constructor Summary

    Constructors
    Constructor
    Description
    WebSocketCommNIO(WorkerNIO worker, boolean isSecure, int version, String[] subProtocols, boolean doFailForUnsupportedSubProtocol, int timeoutConnectClose, SocketChannel channel, WebSocketCommListener listener, WebSocketAcceptor acceptor, boolean isVerbose)
    Creates a new WebSocket Client Communication channel from an accepted socket connection and sets it to as CHECKING_CONNECT_REQUEST, before the negotiation is completed and then becomes CONNECTED.
    WebSocketCommNIO(String requestURI, String host, int port, int version, String[] subProtocols, boolean isOriginValidated, int timeoutConnectClose, String bindAddress, WorkerNIO worker, WebSocketCommListener listener, boolean isVerbose, SSLEngine engine)
    Creates a new WebSocket Client Communication channel as UNINITIALIZED.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if Binary Data Frames are supported.
    void
    Call this method to close the socket.
    boolean
    close(boolean rightNow)
    Call this method to close the socket.
    void
    close(int code, String reason)
    Call this method to close the socket.
    Gets the WebSocket attachment object, null for none.
    Gets the request domain.
    int
    Gets the port number for the connection to the server.
    Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
    Returns the fully qualified name of the client or the last proxy that sent the request.
    Returns the remote address to which this channel's socket is connected.
    Gets the state of the connection.
    Gets the negotiated and accepted sub-protocol, null for none (yet), or none is provided by the parties (server or client).
    boolean
    Checks if the connection is disposed of.
    boolean
    Checks if the connection is currently closing.
    boolean
    Flag for supporting software compression on client side.
    boolean
    Checks if the connection is established, i.e.
    boolean
    Checks if the connection is connecting.
    boolean
    Return if the connection is secured using SSL/TLS.
    void
    Opens the communication.
    void
    open(boolean noTimeouts)
    Opens the communication.
    void
    send(byte[] data)
    Sends binary data.
    void
    send(String data)
    Sends a String of data.
    boolean
    Sends a heart-beat.
    void
    Initializes the communication for server-side channels.
    void
    setAttachment(Object attachment)
    Gets the WebSocket attachment object, null for none.
    void
    setSubProtocol(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
  • Field Details

    • requestURI

      public final String requestURI
      The request URI.
  • Constructor Details

    • WebSocketCommNIO

      public WebSocketCommNIO(String requestURI, String host, int port, int version, String[] subProtocols, boolean isOriginValidated, int timeoutConnectClose, String bindAddress, WorkerNIO worker, WebSocketCommListener listener, boolean isVerbose, SSLEngine engine) throws IOException
      Creates a new WebSocket Client Communication channel as UNINITIALIZED.
      Parameters:
      requestURI - The request URI.
      host - The host name or IP address.
      port - The port number.
      version - The version of web sockets.
      subProtocols - The sub protocols array.
      isOriginValidated - Flag if origin should be validated.
      timeoutConnectClose - Timeout in milliseconds for connection closure.
      bindAddress - The bind address, or null for none.
      worker - The worker thread for socket processing.
      listener - The listener for WebSocket events.
      isVerbose - Flag for logging.
      engine - The SSL engine or null if SSL is not used.
      Throws:
      IOException - For SSL errors.
    • WebSocketCommNIO

      public WebSocketCommNIO(WorkerNIO worker, boolean isSecure, int version, String[] subProtocols, boolean doFailForUnsupportedSubProtocol, int timeoutConnectClose, SocketChannel channel, WebSocketCommListener listener, WebSocketAcceptor acceptor, boolean isVerbose) throws IOException
      Creates a new WebSocket Client Communication channel from an accepted socket connection and sets it to as CHECKING_CONNECT_REQUEST, before the negotiation is completed and then becomes CONNECTED.
      Parameters:
      worker - The worker thread for socket processing.
      channel - The SocketChannel instance that has been accepted.
      listener - The listener for WebSocket (server) events.
      acceptor - The URI acceptor for WebSocket requests.
      isVerbose - Flag for logging.
      Throws:
      IOException - For I/O exceptions.
  • Method Details

    • serverInitializeComm

      public void serverInitializeComm()
      Initializes the communication for server-side channels.
    • open

      public void open() throws IOException
      Opens the communication.
      Specified by:
      open in interface IWebSocketComm
      Throws:
      IOException
    • open

      public void open(boolean noTimeouts) throws IOException
      Opens the communication.
      Parameters:
      noTimeouts - Connect with debug = no timeouts! This way we can set breakpoints in Eclipse.
      Throws:
      IOException
    • getSubProtocol

      public 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.
    • setSubProtocol

      public void setSubProtocol(String subProtocol) throws 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:
      IllegalStateException - If the state doesn't permit this operation, or if the implementation doesn't allow it.
    • isCompressionSupported

      public boolean isCompressionSupported()
      Flag for supporting software compression on client side.
      Specified by:
      isCompressionSupported in interface IWebSocketComm
      Returns:
      The support flag depending on the subProtocol chosen.
    • getState

      public State getState()
      Gets the state of the connection.
      Specified by:
      getState in interface IWebSocketComm
      Returns:
      The state.
    • 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, i.e. CONNECTED.
      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, 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.
    • 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.
    • getDomain

      public 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.
    • getRemoteAddr

      public String getRemoteAddr()
      Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. 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 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).
      Specified by:
      getRemoteHost in interface IWebSocketComm
      Returns:
      a String containing the fully qualified name of the client.
    • getSocketAddress

      public InetSocketAddress 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.
    • send

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

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

      public boolean sendHeartBeat() throws 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:
      IOException - For send failures.
    • getAttachment

      public 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(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.