Package 

Class WebSocketServer

  • All Implemented Interfaces:
    com.tds.common.websocket.conn.WebSocketListener , java.lang.Runnable

    
    public abstract class WebSocketServer
    extends AbstractWebSocket implements Runnable
                        

    WebSocketServer is an abstract class that only takes care of the HTTP handshake portion of WebSockets. It's up to a subclass to add functionality/purpose to the server.

    • Constructor Detail

      • WebSocketServer

        WebSocketServer()
        Creates a WebSocketServer that will attempt to listen on port WebSocketImpl.
      • WebSocketServer

        WebSocketServer(InetSocketAddress address)
        Creates a WebSocketServer that will attempt to bind/listen on the given address.
        Parameters:
        address - The address to listen to
      • WebSocketServer

        WebSocketServer(InetSocketAddress address, int decodercount)
        Parameters:
        address - The address (host:port) this server should listen on.
        decodercount - The number of WebSocketWorkers that will be used to process theincoming network data.
      • WebSocketServer

        WebSocketServer(InetSocketAddress address, List<Draft> drafts)
        Parameters:
        address - The address (host:port) this server should listen on.
        drafts - The versions of the WebSocket protocol that this server instance should complyto.
      • WebSocketServer

        WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts)
        Parameters:
        address - The address (host:port) this server should listen on.
        decodercount - The number of WebSocketWorkers that will be used to process theincoming network data.
        drafts - The versions of the WebSocket protocol that this server instance shouldcomply to.
      • WebSocketServer

        WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts, Collection<WebSocket> connectionscontainer)
        Creates a WebSocketServer that will attempt to bind/listen on the given address, andcomply with Draft version draft.
        Parameters:
        address - The address (host:port) this server should listen on.
        decodercount - The number of WebSocketWorkers that will be used to processthe incoming network data.
        drafts - The versions of the WebSocket protocol that this server instanceshould comply to.
        connectionscontainer - Allows to specify a collection that will be used to store thewebsockets in.
    • Method Detail

      • getConnections

         Collection<WebSocket> getConnections()

        Returns all currently connected clients. This collection does not allow any modification e.g.removing a client.

      • getMaxPendingConnections

         int getMaxPendingConnections()

        Returns the currently configured maximum number of pending connections.

      • setMaxPendingConnections

         void setMaxPendingConnections(int numberOfConnections)

        Set the requested maximum number of pending connections on the socket. The exact semantics areimplementation specific. The value provided should be greater than 0. If it is less than orequal to 0, then an implementation specific default will be used. This option will be passed as"backlog" parameter to bind

      • start

         void start()

        Starts the server selectorthread that binds to the currently set port number and listeners forWebSocket connection requests. Creates a fixed thread pool with the size May only be called once.

        Alternatively you can call run directly.

      • stop

         void stop(int timeout)

        Closes all connected clients sockets, then closes the underlying ServerSocketChannel,effectively killing the server socket selectorthread, freeing the port the server was bound toand stops all internal workerthreads.

        If this method is called before the server is started it will never start.

        Parameters:
        timeout - Specifies how many milliseconds the overall close handshaking may takealtogether before the connections are closed without proper closehandshaking.
      • getPort

         int getPort()

        Gets the port number that this server listens on.

      • run

         void run()
      • onWebsocketMessage

         final void onWebsocketMessage(WebSocket conn, String message)

        Called when an entire text frame has been received. Do whatever you want here...

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        message - The UTF-8 decoded message that was received.
      • onWebsocketMessage

         final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)

        Called when an entire binary frame has been received. Do whatever you want here...

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        blob - The binary message that was received.
      • onWebsocketOpen

         final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)

        Called after onHandshakeReceived returns true. Indicates that a completeWebSocket connection has been established, and we are ready to send/receive data.

        Parameters:
        conn - The WebSocket instance this event is occurring on.
      • onWebsocketClose

         final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)

        Called after WebSocket#close is explicity called, or when the other end of theWebSocket connection is closed.

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onWebsocketError

         final void onWebsocketError(WebSocket conn, Exception ex)

        Called if an exception worth noting occurred. If an error causes the connection to fail onClosewill be called additionally afterwards.

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        ex - The exception that occurred.
      • onWriteDemand

         final void onWriteDemand(WebSocket w)

        This method is used to inform the selector thread that there is data queued to be written tothe socket.

      • onWebsocketClosing

         void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)

        Called as soon as no further frames are accepted

        Parameters:
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onOpen

         abstract void onOpen(WebSocket conn, ClientHandshake handshake)

        Called after an opening handshake has been performed and the given websocket is ready to bewritten on.

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        handshake - The handshake of the websocket instance
      • onClose

         abstract void onClose(WebSocket conn, int code, String reason, boolean remote)

        Called after the websocket connection has been closed.

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        code - The codes can be looked up here: CloseFrame
        reason - Additional information string
        remote - Returns whether or not the closing of the connection was initiated by the remotehost.
      • onMessage

         abstract void onMessage(WebSocket conn, String message)

        Callback for string messages received from the remote host

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        message - The UTF-8 decoded message that was received.
      • onError

         abstract void onError(WebSocket conn, Exception ex)

        Called when errors occurs. If an error causes the websocket connection to fail will be called additionally. This method will becalled primarily because of IO or protocol errors. If the given exception is anRuntimeException that probably means that you encountered a bug.

        Parameters:
        conn - Can be null if there error does not belong to one specific websocket.
        ex - The exception causing this error
      • onStart

         abstract void onStart()

        Called when the server started up successfully.

        If any error occurred, onError is called instead.

      • onMessage

         void onMessage(WebSocket conn, ByteBuffer message)

        Callback for binary messages received from the remote host

        Parameters:
        conn - The WebSocket instance this event is occurring on.
        message - The binary message that was received.
      • broadcast

         void broadcast(String text)

        Send a text to all connected endpoints

        Parameters:
        text - the text to send to the endpoints
      • broadcast

         void broadcast(Array<byte> data)

        Send a byte array to all connected endpoints

        Parameters:
        data - the data to send to the endpoints
      • broadcast

         void broadcast(ByteBuffer data)

        Send a ByteBuffer to all connected endpoints

        Parameters:
        data - the data to send to the endpoints
      • broadcast

         void broadcast(Array<byte> data, Collection<WebSocket> clients)

        Send a byte array to a specific collection of websocket connections

        Parameters:
        data - the data to send to the endpoints
        clients - a collection of endpoints to whom the text has to be send
      • broadcast

         void broadcast(ByteBuffer data, Collection<WebSocket> clients)

        Send a ByteBuffer to a specific collection of websocket connections

        Parameters:
        data - the data to send to the endpoints
        clients - a collection of endpoints to whom the text has to be send
      • broadcast

         void broadcast(String text, Collection<WebSocket> clients)

        Send a text to a specific collection of websocket connections

        Parameters:
        text - the text to send to the endpoints
        clients - a collection of endpoints to whom the text has to be send