Class WebSocketFactory
- java.lang.Object
-
- com.neovisionaries.ws.client.WebSocketFactory
-
public class WebSocketFactory extends java.lang.ObjectFactory to createWebSocketinstances.
-
-
Field Summary
Fields Modifier and Type Field Description private intmConnectionTimeoutprivate intmDualStackFallbackDelayprivate DualStackModemDualStackModeprivate ProxySettingsmProxySettingsprivate java.lang.String[]mServerNamesprivate SocketFactorySettingsmSocketFactorySettingsprivate intmSocketTimeoutprivate booleanmVerifyHostname
-
Constructor Summary
Constructors Constructor Description WebSocketFactory()Constructor.WebSocketFactory(WebSocketFactory other)Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private SocketConnectorcreateDirectRawSocket(java.lang.String host, int port, boolean secure, int timeout)private SocketConnectorcreateProxiedRawSocket(java.lang.String host, int port, boolean secure, int timeout)private SocketConnectorcreateRawSocket(java.lang.String host, int port, boolean secure, int timeout)WebSocketcreateSocket(java.lang.String uri)Create a WebSocket.WebSocketcreateSocket(java.lang.String uri, int timeout)Create a WebSocket.private WebSocketcreateSocket(java.lang.String scheme, java.lang.String userInfo, java.lang.String host, int port, java.lang.String path, java.lang.String query, int timeout)WebSocketcreateSocket(java.net.URI uri)Create a WebSocket.WebSocketcreateSocket(java.net.URI uri, int timeout)Create a WebSocket.WebSocketcreateSocket(java.net.URL url)Create a WebSocket.WebSocketcreateSocket(java.net.URL url, int timeout)Create a WebSocket.private WebSocketcreateWebSocket(boolean secure, java.lang.String userInfo, java.lang.String host, int port, java.lang.String path, java.lang.String query, SocketConnector connector)private static java.lang.StringdeterminePath(java.lang.String path)private static intdeterminePort(int port, boolean secure)intgetConnectionTimeout()Get the timeout value in milliseconds for socket connection.intgetDualStackFallbackDelay()Get the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.DualStackModegetDualStackMode()Get the dual stack mode that will be applied when establishing a socket connection.ProxySettingsgetProxySettings()Get the proxy settings.java.lang.String[]getServerNames()Get server names for SNI (Server Name Indication).javax.net.SocketFactorygetSocketFactory()Get the socket factory that has been set bysetSocketFactory(SocketFactory).intgetSocketTimeout()Get the timeout value in milliseconds for socket read and write operations.javax.net.ssl.SSLContextgetSSLContext()Get the SSL context that has been set bysetSSLContext(SSLContext).javax.net.ssl.SSLSocketFactorygetSSLSocketFactory()Get the SSL socket factory that has been set bysetSSLSocketFactory(SSLSocketFactory).booleangetVerifyHostname()Get the flag which indicates whether the hostname in the server's certificate should be verified or not.private static booleanisSecureConnectionRequired(java.lang.String scheme)WebSocketFactorysetConnectionTimeout(int timeout)Set the timeout value in milliseconds for socket connection.WebSocketFactorysetDualStackFallbackDelay(int delay)Set the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.WebSocketFactorysetDualStackMode(DualStackMode mode)Set the dual stack mode that will be applied when establishing a socket connection.WebSocketFactorysetServerName(java.lang.String serverName)Set a server name for SNI (Server Name Indication).WebSocketFactorysetServerNames(java.lang.String[] serverNames)Set server names for SNI (Server Name Indication).WebSocketFactorysetSocketFactory(javax.net.SocketFactory factory)Set a socket factory.WebSocketFactorysetSocketTimeout(int timeout)Set the timeout value in milliseconds for socket read and write operations.WebSocketFactorysetSSLContext(javax.net.ssl.SSLContext context)Set an SSL context to get a socket factory.WebSocketFactorysetSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)Set an SSL socket factory.WebSocketFactorysetVerifyHostname(boolean verifyHostname)Set the flag which indicates whether the hostname in the server's certificate should be verified or not.
-
-
-
Field Detail
-
mSocketFactorySettings
private final SocketFactorySettings mSocketFactorySettings
-
mProxySettings
private final ProxySettings mProxySettings
-
mConnectionTimeout
private int mConnectionTimeout
-
mSocketTimeout
private int mSocketTimeout
-
mDualStackMode
private DualStackMode mDualStackMode
-
mDualStackFallbackDelay
private int mDualStackFallbackDelay
-
mVerifyHostname
private boolean mVerifyHostname
-
mServerNames
private java.lang.String[] mServerNames
-
-
Constructor Detail
-
WebSocketFactory
public WebSocketFactory()
Constructor.
-
WebSocketFactory
public WebSocketFactory(WebSocketFactory other)
Copy constructor.- Parameters:
other- AWebSocketFactoryinstance to copy.- Throws:
java.lang.IllegalArgumentException- If the givenWebSocketFactoryinstance is null.- Since:
- 2.10
-
-
Method Detail
-
getSocketFactory
public javax.net.SocketFactory getSocketFactory()
Get the socket factory that has been set bysetSocketFactory(SocketFactory).- Returns:
- The socket factory.
-
setSocketFactory
public WebSocketFactory setSocketFactory(javax.net.SocketFactory factory)
Set a socket factory. SeecreateSocket(URI)for details.- Parameters:
factory- A socket factory.- Returns:
thisinstance.
-
getSSLSocketFactory
public javax.net.ssl.SSLSocketFactory getSSLSocketFactory()
Get the SSL socket factory that has been set bysetSSLSocketFactory(SSLSocketFactory).- Returns:
- The SSL socket factory.
-
setSSLSocketFactory
public WebSocketFactory setSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
Set an SSL socket factory. SeecreateSocket(URI)for details.- Parameters:
factory- An SSL socket factory.- Returns:
thisinstance.
-
getSSLContext
public javax.net.ssl.SSLContext getSSLContext()
Get the SSL context that has been set bysetSSLContext(SSLContext).- Returns:
- The SSL context.
-
setSSLContext
public WebSocketFactory setSSLContext(javax.net.ssl.SSLContext context)
Set an SSL context to get a socket factory. SeecreateSocket(URI)for details.- Parameters:
context- An SSL context.- Returns:
thisinstance.
-
getProxySettings
public ProxySettings getProxySettings()
Get the proxy settings.- Returns:
- The proxy settings.
- Since:
- 1.3
- See Also:
ProxySettings
-
getConnectionTimeout
public int getConnectionTimeout()
Get the timeout value in milliseconds for socket connection. The default value is 0 and it means an infinite timeout.When a
createSocketmethod which does not havetimeoutargument is called, the value returned by this method is used as a timeout value for socket connection.- Returns:
- The connection timeout value in milliseconds.
- Since:
- 1.10
-
setConnectionTimeout
public WebSocketFactory setConnectionTimeout(int timeout)
Set the timeout value in milliseconds for socket connection. A timeout of zero is interpreted as an infinite timeout.- Parameters:
timeout- The connection timeout value in milliseconds.- Returns:
thisobject.- Throws:
java.lang.IllegalArgumentException- The given timeout value is negative.- Since:
- 1.10
-
getSocketTimeout
public int getSocketTimeout()
Get the timeout value in milliseconds for socket read and write operations. The default value is 0 and it means an infinite timeout.This can be changed later with
getSocket().setSoTimeout(int).- Returns:
- The socket timeout value in milliseconds.
- Since:
- 2.14
- See Also:
Socket.setSoTimeout(int)
-
setSocketTimeout
public WebSocketFactory setSocketTimeout(int timeout)
Set the timeout value in milliseconds for socket read and write operations. A timeout of zero is interpreted as an infinite timeout.This can be changed later with
getSocket().setSoTimeout(int).- Parameters:
timeout- The socket timeout value in milliseconds.- Returns:
thisobject.- Throws:
java.lang.IllegalArgumentException- The given timeout value is negative.- Since:
- 2.14
- See Also:
Socket.setSoTimeout(int)
-
getDualStackMode
public DualStackMode getDualStackMode()
Get the dual stack mode that will be applied when establishing a socket connection. The default value isDualStackMode.BOTH.A hostname may resolve to an arbitrary amount of IPv4 and IPv6 addresses. This controls which IP address families will be used when establishing a connection. Note that IPv6 will be preferred, if activated.
- Returns:
- The dual stack mode.
-
setDualStackMode
public WebSocketFactory setDualStackMode(DualStackMode mode)
Set the dual stack mode that will be applied when establishing a socket connection.- Parameters:
mode- The dual stack mode to be applied.- Returns:
thisobject.
-
getDualStackFallbackDelay
public int getDualStackFallbackDelay()
Get the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.A hostname may resolve to an arbitrary amount of IPv4 and IPv6 addresses. This controls the maximum amount of time that may pass between attempts to establish a socket connection to an IP addresses before trying the next one. Note that the previous attempt will not be aborted. The connections will race until one has been established.
- Returns:
- The dual stack fallback delay in milliseconds.
-
setDualStackFallbackDelay
public WebSocketFactory setDualStackFallbackDelay(int delay)
Set the dual stack fallback delay in milliseconds that will be applied when establishing a socket connection.- Parameters:
delay- The dual stack fallback delay in milliseconds.- Returns:
thisobject.
-
getVerifyHostname
public boolean getVerifyHostname()
Get the flag which indicates whether the hostname in the server's certificate should be verified or not. The default value istrue. See the description ofsetVerifyHostname(boolean)to understand what this boolean flag means.- Returns:
trueif hostname verification is enabled.- Since:
- 2.3
-
setVerifyHostname
public WebSocketFactory setVerifyHostname(boolean verifyHostname)
Set the flag which indicates whether the hostname in the server's certificate should be verified or not. The default value istrue.Manual hostname verification has been enabled since the version 2.1. Because the verification is executed manually after
Socket.connect(SocketAddress, int)succeeds, the hostname verification is always executed even if you has passed anSSLContextwhich naively accepts any server certificate (e.g.NaiveSSLContext). However, this behavior is not desirable in some cases and you may want to disable the hostname verification. This setter method exists for the purpose and you can disable hostname verification by passingfalseto this method.- Parameters:
verifyHostname-trueto enable hostname verification.falseto disable hostname verification.- Returns:
thisobject.- Since:
- 2.3
-
getServerNames
public java.lang.String[] getServerNames()
Get server names for SNI (Server Name Indication).- Returns:
- List of host names.
- Since:
- 2.4
-
setServerNames
public WebSocketFactory setServerNames(java.lang.String[] serverNames)
Set server names for SNI (Server Name Indication). IfsetServerNames(List<SNIServerName>)method ofSSLParametersclass is available in the underlying system, the method is called to set up server names for SNI (Server Name Indication).- Parameters:
serverNames- List of host names.- Returns:
thisobject.- Since:
- 2.4
-
setServerName
public WebSocketFactory setServerName(java.lang.String serverName)
Set a server name for SNI (Server Name Indication). This method internally creates a String array of size 1 which contains the givenserverNameand callssetServerNames(String[]).- Parameters:
serverName- A host name.- Returns:
thisobject.- Since:
- 2.4
-
createSocket
public WebSocket createSocket(java.lang.String uri) throws java.io.IOException
Create a WebSocket.This method is an alias of
createSocket(uri,getConnectionTimeout()).- Parameters:
uri- The URI of the WebSocket endpoint on the server side.- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URI isnullor violates RFC 2396.java.io.IOException- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.
-
createSocket
public WebSocket createSocket(java.lang.String uri, int timeout) throws java.io.IOException
Create a WebSocket.This method is an alias of
createSocket(URI.create(uri), timeout).- Parameters:
uri- The URI of the WebSocket endpoint on the server side.timeout- The timeout value in milliseconds for socket connection. A timeout of zero is interpreted as an infinite timeout.- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URI isnullor violates RFC 2396, or the given timeout value is negative.java.io.IOException- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.- Since:
- 1.10
-
createSocket
public WebSocket createSocket(java.net.URL url) throws java.io.IOException
Create a WebSocket.This method is an alias of
createSocket(url,getConnectionTimeout()).- Parameters:
url- The URL of the WebSocket endpoint on the server side.- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URL isnullor failed to be converted into a URI.java.io.IOException- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.
-
createSocket
public WebSocket createSocket(java.net.URL url, int timeout) throws java.io.IOException
Create a WebSocket.This method is an alias of
createSocket(url.toURI(), timeout).- Parameters:
url- The URL of the WebSocket endpoint on the server side.timeout- The timeout value in milliseconds for socket connection.- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URL isnullor failed to be converted into a URI, or the given timeout value is negative.java.io.IOException- Failed to create a socket. Or, HTTP proxy handshake or SSL handshake failed.- Since:
- 1.10
-
createSocket
public WebSocket createSocket(java.net.URI uri) throws java.io.IOException
Create a WebSocket. This method is an alias ofcreateSocket(uri,getConnectionTimeout()).A socket factory (= a
SocketFactoryinstance) to create a raw socket (= aSocketinstance) is determined as described below.-
If the scheme of the URI is either
wssorhttps,-
If an
SSLContextinstance has been set bysetSSLContext(SSLContext), the value returned fromSSLContext.getSocketFactory()method of the instance is used. -
Otherwise, if an
SSLSocketFactoryinstance has been set bysetSSLSocketFactory(SSLSocketFactory), the instance is used. -
Otherwise, the value returned from
SSLSocketFactory.getDefault()is used.
-
If an
-
Otherwise (= the scheme of the URI is either
wsorhttp),-
If a
SocketFactoryinstance has been set bysetSocketFactory(SocketFactory), the instance is used. -
Otherwise, the value returned from
SocketFactory.getDefault()is used.
-
If a
- Parameters:
uri- The URI of the WebSocket endpoint on the server side. The scheme part of the URI must be one ofws,wss,httpandhttps(case-insensitive).- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URI isnullor violates RFC 2396.java.io.IOException- Failed to create a socket.
-
If the scheme of the URI is either
-
createSocket
public WebSocket createSocket(java.net.URI uri, int timeout) throws java.io.IOException
Create a WebSocket.A socket factory (= a
SocketFactoryinstance) to create a raw socket (= aSocketinstance) is determined as described below.-
If the scheme of the URI is either
wssorhttps,-
If an
SSLContextinstance has been set bysetSSLContext(SSLContext), the value returned fromSSLContext.getSocketFactory()method of the instance is used. -
Otherwise, if an
SSLSocketFactoryinstance has been set bysetSSLSocketFactory(SSLSocketFactory), the instance is used. -
Otherwise, the value returned from
SSLSocketFactory.getDefault()is used.
-
If an
-
Otherwise (= the scheme of the URI is either
wsorhttp),-
If a
SocketFactoryinstance has been set bysetSocketFactory(SocketFactory), the instance is used. -
Otherwise, the value returned from
SocketFactory.getDefault()is used.
-
If a
- Parameters:
uri- The URI of the WebSocket endpoint on the server side. The scheme part of the URI must be one ofws,wss,httpandhttps(case-insensitive).timeout- The timeout value in milliseconds for socket connection.- Returns:
- A WebSocket.
- Throws:
java.lang.IllegalArgumentException- The given URI isnullor violates RFC 2396, or the given timeout value is negative.java.io.IOException- Failed to create a socket.- Since:
- 1.10
-
If the scheme of the URI is either
-
createSocket
private WebSocket createSocket(java.lang.String scheme, java.lang.String userInfo, java.lang.String host, int port, java.lang.String path, java.lang.String query, int timeout) throws java.io.IOException
- Throws:
java.io.IOException
-
isSecureConnectionRequired
private static boolean isSecureConnectionRequired(java.lang.String scheme)
-
determinePath
private static java.lang.String determinePath(java.lang.String path)
-
createRawSocket
private SocketConnector createRawSocket(java.lang.String host, int port, boolean secure, int timeout) throws java.io.IOException
- Throws:
java.io.IOException
-
createProxiedRawSocket
private SocketConnector createProxiedRawSocket(java.lang.String host, int port, boolean secure, int timeout)
-
createDirectRawSocket
private SocketConnector createDirectRawSocket(java.lang.String host, int port, boolean secure, int timeout)
-
determinePort
private static int determinePort(int port, boolean secure)
-
createWebSocket
private WebSocket createWebSocket(boolean secure, java.lang.String userInfo, java.lang.String host, int port, java.lang.String path, java.lang.String query, SocketConnector connector)
-
-