Package com.gmt2001.wsclient
Class WSClient
java.lang.Object
com.gmt2001.wsclient.WSClient
Provides a WebSocket client
- Author:
- gmt2001
-
Constructor Summary
ConstructorsConstructorDescriptionWSClient
(URI uri, WsClientFrameHandler handler) Constructor that does not initialize aWSPinger
WSClient
(URI uri, WsClientFrameHandler handler, WSPinger pinger) Constructor -
Method Summary
Modifier and TypeMethodDescriptionchannel()
Retrieves the socket channelvoid
close()
Sends a WebSocket frame of type Close with a status code of NORMAL_CLOSURE, then closes the socket, with a grace period for ongoing requests to finishvoid
Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoid
close
(WebSocketCloseStatus status) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoid
close
(WebSocketCloseStatus status, String reason) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoid
close
(WebSocketFrame closeFrame) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishboolean
connect()
Connects to the serverboolean
Indicates if the socket is connected.void
send
(byte[] binarydata) Sends a WebSocket frame of type Binaryvoid
send
(WebSocketFrame frame) Sends a WebSocket framevoid
Sends a WebSocket frame of type Textvoid
send
(JSONObject jso) Stringifys aJSONObject
, then sends it as a WebSocket frame of type Text
-
Constructor Details
-
WSClient
public WSClient(URI uri, WsClientFrameHandler handler) throws SSLException, IllegalArgumentException Constructor that does not initialize aWSPinger
- Parameters:
uri
- The URI to connect tohandler
- An object implementingWsClientFrameHandler
which will receive frames- Throws:
SSLException
- Failed to create theSslContext
IllegalArgumentException
- URI scheme is not ws or wss
-
WSClient
public WSClient(URI uri, WsClientFrameHandler handler, WSPinger pinger) throws SSLException, IllegalArgumentException Constructor- Parameters:
uri
- The URI to connect tohandler
- An object implementingWsClientFrameHandler
which will receive framespinger
- TheWSPinger
that will send PING on an interval, ornull
if this is not desired- Throws:
SSLException
- Failed to create theSslContext
IllegalArgumentException
- URI scheme is not ws or wss
-
-
Method Details
-
connect
Connects to the server- Returns:
- true if the socket has connected and is starting the handshake; false otherwise
- Throws:
InterruptedException
- The connection process was interruptedIllegalStateException
- Attempting to use a closed client
-
connected
public boolean connected()Indicates if the socket is connected. NOTE: Only indicates that the socket is ready to send/receive frames. Does not indicate authentication status or anything else- Returns:
- true if the socket is connected and ready to send/receive frames; false otherwise
-
channel
Retrieves the socket channel- Returns:
- The socket channel if connected; null otherwise
-
send
Sends a WebSocket frame- Parameters:
frame
- The frame to send
-
send
Sends a WebSocket frame of type Text- Parameters:
text
- The body content of the frame
-
send
Stringifys aJSONObject
, then sends it as a WebSocket frame of type Text- Parameters:
jso
- TheJSONObject
to send
-
send
public void send(byte[] binarydata) Sends a WebSocket frame of type Binary- Parameters:
binarydata
- The binary bytes to send
-
close
public void close()Sends a WebSocket frame of type Close with a status code of NORMAL_CLOSURE, then closes the socket, with a grace period for ongoing requests to finish -
close
Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finish- Parameters:
status
- The close status code to send
-
close
Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finish- Parameters:
status
- The close status code to sendreason
- The reason string to send
-
close
Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finish- Parameters:
status
- The close status code to sendreason
- The reason string to send
-
close
Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finish- Parameters:
closeFrame
- The close frame to send
-