public class WebSocket
extends java.lang.Object
implements java.lang.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() |
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) |
void |
sendText(java.lang.String text) |
protected void |
setSocket(java.net.Socket socket)
Sets the socket.
|
protected void |
setupSocket()
Open the socket and sets it.
|
public WebSocket(WebSocketURI uri, Endpoint endpoint) throws java.lang.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 eventsjava.lang.NullPointerException
- if 'endpoint' or 'uri' is nullpublic void connect() throws java.lang.IllegalArgumentException, WebSocketException, ServerException
Receiver
. If at some point a problem is
found, this method will throw a WebSocketException
.
See section 4.1. Client Requirements.
java.lang.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)protected void setupSocket() throws java.io.IOException
java.io.IOException
- if an IOException
occurs.setSocket(Socket)
protected void setSocket(java.net.Socket socket)
socket
- the socket to set.public ConnectionStates getCurrentState()
WebSocket
public WebSocketURI getURI()
WebSocketURI
of the remote endpointpublic void close(ReasonForClosure reasonForClosure) throws java.io.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 endpointjava.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occursOnTimeOutCloser
public void close() throws java.io.IOException
close(new ReasonForClosure(CloseCodes.NORMAL_CLOSURE, ""))
.close
in interface java.lang.AutoCloseable
java.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void ping(byte[] payload) throws java.lang.IllegalStateException, java.io.IOException
payload
- the payload of the pingjava.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void ping() throws java.lang.IllegalStateException, java.io.IOException
java.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void pong(byte[] payload) throws java.lang.IllegalStateException, java.io.IOException
payload
- the payload of the pongjava.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void pong() throws java.lang.IllegalStateException, java.io.IOException
java.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void sendBinary(byte[] binary) throws java.lang.IllegalStateException, java.io.IOException
binary
- binary message to sendjava.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurspublic void sendText(java.lang.String text) throws java.lang.IllegalStateException, java.io.IOException
text
- text message to sendjava.lang.IllegalStateException
- if the connection state is not OPENjava.io.IOException
- thrown by the underlying Socket
and associated streams when an error occurs