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.


    • Field Summary

      Fields 
      Modifier and Type Field Description
      boolean QUIET
      A Verbose Flag.
      StorageWriter sw
      An output printer.
    • Constructor Summary

      Constructors 
      Constructor Description
      WebSocketSender​(String url, boolean quiet, Consumer<Object> eventHandler)
      Opens a Connection to a Web Browser using a Web-Socket.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void disconnect()
      Closes the WebSocket connection to the Browser's Remote Debug Port.
      void send​(int requestID, String requestJSON, Promise promise)
      This method is the implementation-method for the Sender Functional-Interface.
      • 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,
                     boolean quiet,
                     java.util.function.Consumer<java.lang.Object> eventHandler)
                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:
         synchronized (promise)
         {
             promises.put(requestID, promise);
        
             // Print the request-message that is about to be sent, and then send it.
             if (! QUIET) if (sw != null)
                 sw.println(BYELLOW + "Sending JSON:\n\t" + RESET + 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
                 );
             }
         }