Package ej.websocket

Class WebSocket

  • All Implemented Interfaces:
    AutoCloseable
    Direct Known Subclasses:
    WebSocketSecure

    public class WebSocket
    extends Object
    implements AutoCloseable
    Representation for the physical websocket connection following RFC 6455 definition. It needs a 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.

    • Method Detail

      • addHeader

        public void addHeader​(String name,
                              String value)
        Adds a header to send when connecting.
        Parameters:
        name - the header name
        value - the header value
      • connect

        public void connect()
                     throws IllegalArgumentException,
                            WebSocketException,
                            ServerException
        Establish a connection to the server described by the URI. Once the connection is up, the endpoint will be notify of high level events whereas the websocket will manage low level events (such as ping, close, etc) thank a thread running a Receiver. If at some point a problem is found, this method will throw a WebSocketException.

        See section 4.1. Client Requirements.

        Throws:
        IllegalArgumentException - if the current state of this websocket is not NEW
        ServerException - if the connection is established but the server doesn't answer properly
        WebSocketException - if an error occurs during connection (most probably a socket error)
      • setSocket

        protected void setSocket​(Socket socket)
                          throws IOException
        Sets the socket.
        Parameters:
        socket - the socket to set.
        Throws:
        IOException
      • close

        public void close​(ReasonForClosure reasonForClosure)
                   throws IOException
        Close the websocket connection as described in Section 7. Closing the Connection..
        • Send a closing handshake to the server (see Section 7.1.2. Start the WebSocket Closing Handshake).
        • Start a thread that will close the TCP connection after a timeout if the server doesn't answer thanks to a OnTimeOutCloser.
        • Wait for a close frame in the Receiver to close the TCP connection and stop the OnTimeOutCloser thread.

        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.

        Parameters:
        reasonForClosure - the reason for closing the connection that will be sent to remote endpoint
        Throws:
        IllegalStateException - if the connection state is not OPEN
        IOException - thrown by the underlying Socket and associated streams when an error occurs
        See Also:
        OnTimeOutCloser
      • close

        public void close()
                   throws IOException
        Equivalent to close(new ReasonForClosure(CloseCodes.NORMAL_CLOSURE, "")).
        Specified by:
        close in interface AutoCloseable
        Throws:
        IOException - thrown by the underlying Socket and associated streams when an error occurs
      • sendText

        public void sendText​(String text)
                      throws IllegalStateException,
                             IOException
        Sends a text message.

        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);

        Parameters:
        text - text message to send
        Throws:
        IllegalStateException - if the connection state is not OPEN
        IOException - thrown by the underlying Socket and associated streams when an error occurs