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 aWSPingerWSClient(URI uri, WsClientFrameHandler handler, WSPinger pinger) Constructor -
Method Summary
Modifier and TypeMethodDescriptionchannel()Retrieves the socket channelvoidclose()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 finishvoidSends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoidclose(WebSocketCloseStatus status) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoidclose(WebSocketCloseStatus status, String reason) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishvoidclose(WebSocketFrame closeFrame) Sends a WebSocket frame of type Close, then closes the socket, with a grace period for ongoing requests to finishbooleanconnect()Connects to the serverbooleanIndicates if the socket is connected.voidsend(byte[] binarydata) Sends a WebSocket frame of type Binaryvoidsend(WebSocketFrame frame) Sends a WebSocket framevoidSends a WebSocket frame of type Textvoidsend(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 implementingWsClientFrameHandlerwhich will receive frames- Throws:
SSLException- Failed to create theSslContextIllegalArgumentException- 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 implementingWsClientFrameHandlerwhich will receive framespinger- TheWSPingerthat will send PING on an interval, ornullif this is not desired- Throws:
SSLException- Failed to create theSslContextIllegalArgumentException- 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- TheJSONObjectto 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
-