public class WebSocket extends Object implements AutoCloseable
WebSocketURI
that describes the server to connect and an Endpoint
to handle events (errors, incoming data). A
WebSocket
is the physical medium, managing underlying TCP connection and low level websocket events, whereas
the Endpoint
is here to handle high level events. The latter is more application oriented whereas the former
is the physical medium.
WARNING: this implementation is designed so that WebSocket
is only for client-side connection.
Constructor and Description |
---|
WebSocket(WebSocketURI uri,
Endpoint endpoint)
Create a new
WebSocket instance. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Equivalent to
close(new ReasonForClosure(CloseCodes.NORMAL_CLOSURE, "")) . |
void |
close(ReasonForClosure reasonForClosure)
Close the websocket connection as described in Section 7.
|
void |
connect()
Establish a connection to the server described by the URI.
|
ConnectionStates |
getCurrentState()
Returns the current state of this
WebSocket , see
ConnectionStates for the returned states. |
Endpoint |
getEndpoint()
|
WebSocketURI |
getURI() |
void |
ping()
Send ping with no payload to remote endpoint.
|
void |
ping(byte[] payload)
Send ping to remote endpoint.
|
void |
pong()
Send an unsolicited pong with no payload to remote endpoint.
|
void |
pong(byte[] payload)
Send an unsolicited pong to remote endpoint.
|
void |
sendBinary(byte[] binary)
Sends a binary message.
|
void |
sendText(String text)
Sends a text message.
|
protected void |
setSocket(Socket socket)
Sets the socket.
|
protected void |
setupSocket()
Open the socket and sets it.
|
public WebSocket(WebSocketURI uri, Endpoint endpoint) throws NullPointerException
WebSocket
instance. It doesn't connect to the remote server; user has to call
connect()
to do so.uri
- URI to connect toendpoint
- the endpoint that will handle eventsNullPointerException
- if 'endpoint' or 'uri' is nullpublic void close() throws IOException
close(new ReasonForClosure(CloseCodes.NORMAL_CLOSURE, ""))
.close
in interface AutoCloseable
IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void close(ReasonForClosure reasonForClosure) throws IOException
OnTimeOutCloser
.
As described in section 7.1.1 of the RFC, the client should ask the server to close the underlying TCP connection. If the client closes the TCP connection first, it would prevent it from re-opening the connection.
reasonForClosure
- the reason for closing the connection that will be sent to remote endpointIllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and associated streams when an error occursOnTimeOutCloser
public void connect() throws IllegalArgumentException, WebSocketException, ServerException
Receiver
. If at some point a problem is
found, this method will throw a WebSocketException
.
See section 4.1. Client Requirements.
IllegalArgumentException
- if the current state of this websocket is not NEWServerException
- if the connection is established but the server doesn't answer properlyWebSocketException
- if an error occurs during connection (most probably a socket error)public ConnectionStates getCurrentState()
WebSocket
, see
ConnectionStates
for the returned states.WebSocket
public WebSocketURI getURI()
WebSocketURI
of the remote endpointpublic void ping() throws IllegalStateException, IOException
IllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void ping(byte[] payload) throws IllegalStateException, IOException
payload
- the payload of the pingIllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void pong() throws IllegalStateException, IOException
IllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void pong(byte[] payload) throws IllegalStateException, IOException
payload
- the payload of the pongIllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void sendBinary(byte[] binary) throws IllegalStateException, IOException
binary
- binary message to sendIllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and
associated streams when an error occurspublic void sendText(String text) throws IllegalStateException, IOException
Warning: by default, MicroEJ strings are not encoded in UTF-8, to send an
UTF-8 string you need to declare it as follow
String s = new String("yourString".getBytes("UTF-8);
text
- text message to sendIllegalStateException
- if the connection state is not OPENIOException
- thrown by the underlying Socket
and
associated streams when an error occursprotected void setSocket(Socket socket) throws IOException
socket
- the socket to set.IOException
protected void setupSocket() throws IOException
IOException
- if an IOException
occurs.setSocket(Socket)