Class WebSocketSender

  • All Implemented Interfaces:
    Sender<java.lang.String>

    public class WebSocketSender
    extends java.lang.Object
    implements Sender<java.lang.String>
    This class implements a connection to a Web-Browser using the Remote Debug Protocol over Web-Sockets.

    Browser Remote Debug Protocol Connection Class


    Java is capable of communicating with either a Headless instance of Google Chrome - or any browser that implements the Remote Debuggin Protocol. It is not mandatory to run the browser in headless mode, but it is more common.


    • Constructor Summary

      Constructors 
      Constructor Description
      WebSocketSender​(String url, Consumer<BrowserEvent> eventHandler, Consumer<RDPError> rdpErrorHandler, Consumer<BrowserError> browserErrorHandler)
      Opens a Connection to a Web Browser using a Web-Socket.
    • Method Summary

       
      @FunctionalInterface: (Lambda) Method
      Modifier and Type Method Description
      void send​(int requestID, String requestJSON, Promise promise)
      This method is the implementation-method for the Sender Functional-Interface.
       
      Close the Browser WebSocket Connection
      Modifier and Type Method Description
      void disconnect()
      Closes the WebSocket connection to the Browser's Remote Debug Port.
       
      Set Logs
      Modifier and Type Method Description
      AppendableSafe setApplicationLayerLog​(Appendable a)
      AppendableSafe setErrorLog​(Appendable a)
      AppendableSafe setRawFrameLog​(Appendable a)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WebSocketSender

        🡇     🗕  🗗  🗖
        public WebSocketSender​
                    (java.lang.String url,
                     java.util.function.Consumer<BrowserEvent> eventHandler,
                     java.util.function.Consumer<RDPError> rdpErrorHandler,
                     java.util.function.Consumer<BrowserError> browserErrorHandler)
                throws java.io.IOException,
                       WebSocketException
        
        Opens a Connection to a Web Browser using a Web-Socket. This class will now be ready to accept send(int, String, Promise) messages to the browser.
        Parameters:
        url - This is a URL that is generated by the browser, and has a base URL that is just 127.0.0.1, followed by a port number. There will also be an identifier-code.
        Throws:
        java.io.IOException - Throws if there are problems connecting the socket.
        WebSocketException - Throws if the NeoVisionaries Package encounters a problem building the socket connection.
    • Method Detail

      • send

        🡅     🗕  🗗  🗖
        public void send​(int requestID,
                         java.lang.String requestJSON,
                         Promise promise)
        This method is the implementation-method for the Sender Functional-Interface. This message accepts a Request & ID pair, and then transmits that request to a Browser's Remote-Debugging Port over the WebSocket. It keeps the Promise that was created by the Script that sent this request, and saves that Promise until the Web-Socket receives a response about the request.
        Specified by:
        send in interface Sender<java.lang.String>
        Parameters:
        requestID - This may be any number. It is used to map requests sent over the Web Socket to responses received from it.
        requestJSON - This is the JSON Method Request sent to the Browser
        promise - This is a Promise which is automatically generated by the Script object that is sending the request.
        Code:
        Exact Method Body:
         this.promises.put(requestID, promise);
        
         // Print the request-message that is about to be sent, and then send it.
         app.append(BGREEN_BKGND + " Sending JSON: " + RESET + '\n' + requestJSON);
        
         try
             { webSocket.sendText(requestJSON); }
        
         catch (Exception e)
         {
             throw new AsynchronousException(
                 "When attempting to send a JSON Request, an Exception was thrown:\n" + 
                 e.getMessage() + "\nSee Exception getCause() for details.", e
             );
         }