Class InternalWebSocket

java.lang.Object
com.iizix.jetty.InternalWebSocket
All Implemented Interfaces:
IWebSocketComm, Session.Listener, Session.Listener.AutoDemanding

public class InternalWebSocket extends Object implements Session.Listener, Session.Listener.AutoDemanding, IWebSocketComm
The internal web socket for the server side.
Author:
Christopher Mindus
  • Method Details

    • open

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

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

      public void setListener(WebSocketCommListener listener)
      Assigns the listener.
    • onWebSocketOpen

      public void onWebSocketOpen(Session session)

      A WebSocket Session has opened successfully and is ready to be used.

      Applications can store the given Session as a field so it can be used to send messages back to the other peer.

      Specified by:
      onWebSocketOpen in interface Session.Listener
      Parameters:
      session - the WebSocket session
    • onWebSocketBinary

      public void onWebSocketBinary(ByteBuffer buf, Callback callback)

      A WebSocket BINARY message has been received.

      Specified by:
      onWebSocketBinary in interface Session.Listener
      Parameters:
      buf - The raw payload array received
      callback - The callback to complete when the payload has been processed
    • onWebSocketText

      public void onWebSocketText(String text)

      A WebSocket TEXT message has been received.

      Specified by:
      onWebSocketText in interface Session.Listener
      Parameters:
      text - The text payload.
    • onWebSocketClose

      public void onWebSocketClose(int code, String reason)

      The WebSocket Session has been closed.

      Specified by:
      onWebSocketClose in interface Session.Listener
      Parameters:
      reason - The optional reason for the close.
      statusCode - The close status code.
    • onWebSocketError

      public void onWebSocketError(Throwable error)

      A WebSocket error has occurred during the processing of WebSocket frames.

      Usually errors occurs from bad or malformed incoming packets, for example text frames that do not contain UTF-8 bytes, frames that are too big, or other violations of the WebSocket specification.

      The WebSocket Session will be closed, but applications may explicitly close the Session providing a different status code or reason.

      Specified by:
      onWebSocketError in interface Session.Listener
      Parameters:
      error - The error that occurred.
    • onWebSocketPing

      public void onWebSocketPing(ByteBuffer buf)

      A WebSocket PING frame has been received.

      Specified by:
      onWebSocketPing in interface Session.Listener
      Parameters:
      payload - the PING payload
    • onWebSocketPong

      public void onWebSocketPong(ByteBuffer buf)

      A WebSocket PONG frame has been received.

      Specified by:
      onWebSocketPong in interface Session.Listener
      Parameters:
      buf - The PONG payload.
    • 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, 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(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.
    • 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.
    • 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 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.
    • getRemoteAddr

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