Class WebSocketCommNIO
- java.lang.Object
- com.iizix.nio.WebSocketCommNIO
- All Implemented Interfaces:
IWebSocketComm
- Direct Known Subclasses:
WebSocketComm
public class WebSocketCommNIO extends java.lang.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 java.lang.StringrequestURIThe request URI.
Constructor Summary
Constructors Constructor Description WebSocketCommNIO(WorkerNIO worker, boolean isSecure, int version, java.lang.String[] subProtocols, boolean doFailForUnsupportedSubProtocol, int timeoutConnectClose, java.nio.channels.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(java.lang.String requestURI, java.lang.String host, int port, int version, java.lang.String[] subProtocols, boolean isOriginValidated, int timeoutConnectClose, java.lang.String bindAddress, WorkerNIO worker, WebSocketCommListener listener, boolean isVerbose, javax.net.ssl.SSLEngine engine)Creates a new WebSocket Client Communication channel as UNINITIALIZED.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.InetSocketAddressgetSocketAddress()Returns the remote address to which this channel's socket is connected.SocketCommNIO.StategetState()Gets the state of the connection.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, i.e.booleanisConnecting()Checks if the connection is connecting.booleanisSecure()Return if the connection is secured using SSL/TLS.voidopen()Opens the communication.voidopen(boolean noTimeouts)Opens the communication.voidsend(byte[] data)Sends binary data.voidsend(java.lang.String data)Sends a String of data.booleansendHeartBeat()Sends a heart-beat.voidserverInitializeComm()Initializes the communication for server-side channels.voidsetAttachment(java.lang.Object attachment)Gets the WebSocket attachment object, null for none.voidsetSubProtocol(java.lang.String subProtocol)Assigns a sub-protocol for the WebSocket (in case the processing of sub-protocol negotiation is done e.g.
Constructor Detail
WebSocketCommNIO
public WebSocketCommNIO(java.lang.String requestURI, java.lang.String host, int port, int version, java.lang.String[] subProtocols, boolean isOriginValidated, int timeoutConnectClose, java.lang.String bindAddress, WorkerNIO worker, WebSocketCommListener listener, boolean isVerbose, javax.net.ssl.SSLEngine engine) throws java.io.IOExceptionCreates a new WebSocket Client Communication channel as UNINITIALIZED.- Parameters:
worker- The worker thread for socket processing.listener- The listener for WebSocket events.isVerbose- Flag for logging.- Throws:
java.io.IOException- For SSL errors.
WebSocketCommNIO
public WebSocketCommNIO(WorkerNIO worker, boolean isSecure, int version, java.lang.String[] subProtocols, boolean doFailForUnsupportedSubProtocol, int timeoutConnectClose, java.nio.channels.SocketChannel channel, WebSocketCommListener listener, WebSocketAcceptor acceptor, boolean isVerbose) throws java.io.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:
java.io.IOException- For I/O exceptions.
Method Detail
serverInitializeComm
public void serverInitializeComm()
Initializes the communication for server-side channels.
open
public void open() throws java.io.IOExceptionOpens the communication.- Throws:
java.io.IOException
open
public void open(boolean noTimeouts) throws java.io.IOExceptionOpens the communication.- Parameters:
noTimeouts- Connect with debug = no timeouts! This way we can set breakpoints in Eclipse.- Throws:
java.io.IOException
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:
getSubProtocolin interfaceIWebSocketComm- Returns:
- The String for the sub-protocol negotiated, null for none.
setSubProtocol
public void setSubProtocol(java.lang.String subProtocol) throws java.lang.IllegalStateExceptionAssigns 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:
setSubProtocolin interfaceIWebSocketComm- Throws:
java.lang.IllegalStateException- If the state doesn't permit this operation, or if the implementation doesn't allow it.
getState
public SocketCommNIO.State getState()
Gets the state of the connection.
isConnecting
public boolean isConnecting()
Checks if the connection is connecting.- Specified by:
isConnectingin interfaceIWebSocketComm- Returns:
- true if the connection is pending.
isConnected
public boolean isConnected()
Checks if the connection is established, i.e. CONNECTED.- Specified by:
isConnectedin interfaceIWebSocketComm- Returns:
- true if the connection is established.
isClosing
public boolean isClosing()
Checks if the connection is currently closing.- Specified by:
isClosingin interfaceIWebSocketComm- Returns:
- true if not closed, but closing.
isClosed
public boolean isClosed()
Checks if the connection is disposed of.- Specified by:
isClosedin interfaceIWebSocketComm- Returns:
- true if closed, not closing.
close
public void close()
Call this method to close the socket.- Specified by:
closein interfaceIWebSocketComm
close
public void close(int code, java.lang.String reason)Call this method to close the socket.- Specified by:
closein interfaceIWebSocketComm- Parameters:
code- A close code: zero for none, otherwise a code 1000 or range 3000 to 4999.reason- The reason code: ifcodeis zero, null applies.
close
public boolean close(boolean rightNow)
Call this method to close the socket.- Specified by:
closein interfaceIWebSocketComm- 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:
areBinaryDataFramesSupportedin interfaceIWebSocketComm- Returns:
- true if it is supported, false otherwise.
getDomain
public java.lang.String getDomain()
Gets the request domain.- Specified by:
getDomainin interfaceIWebSocketComm- Returns:
- The domain name of the server.
getPort
public int getPort()
Gets the port number for the connection to the server.- Specified by:
getPortin interfaceIWebSocketComm- Returns:
- The port number.
isSecure
public boolean isSecure()
Return if the connection is secured using SSL/TLS.- Specified by:
isSecurein interfaceIWebSocketComm- Returns:
- The secure flag. Please note that if the server is behind a proxy, the connection might be secure anyway.
getRemoteAddr
public java.lang.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:
getRemoteAddrin interfaceIWebSocketComm- 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).- Specified by:
getRemoteHostin interfaceIWebSocketComm- Returns:
- a String containing the fully qualified name of the client.
getSocketAddress
public java.net.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:
getSocketAddressin interfaceIWebSocketComm- Returns:
- The remote address;
nullif the channel's socket is not connected.
send
public void send(byte[] data) throws java.io.IOExceptionSends binary data.- Specified by:
sendin interfaceIWebSocketComm- 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.IOExceptionSends a String of data.- Specified by:
sendin interfaceIWebSocketComm- 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.IOExceptionSends a heart-beat.- Specified by:
sendHeartBeatin interfaceIWebSocketComm- Returns:
- true for success, false for failure (e.g. closing state or closed).
- Throws:
java.io.IOException- For send failures.
getAttachment
public java.lang.Object getAttachment()
Gets the WebSocket attachment object, null for none.- Specified by:
getAttachmentin interfaceIWebSocketComm- Returns:
- An object set by
setAttachment.
setAttachment
public void setAttachment(java.lang.Object attachment)
Gets the WebSocket attachment object, null for none.- Specified by:
setAttachmentin interfaceIWebSocketComm- Parameters:
attachment- The attachment object for the web socket.