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

       
      User Provided Output-Log Fields
      Modifier and Type Field
      boolean QUIET
      StorageWriter sw
    • Constructor Summary

      Constructors 
      Constructor
      WebSocketSender​(String url, boolean quiet)
    • Method Summary

       
      @FunctionalInterface (Lambda) Method
      Modifier and Type Method
      void send​(int requestID, String requestJSON, Promise promise)
       
      Close the Browser WebSocket Connection
      Modifier and Type Method
      void disconnect()
      • Methods inherited from class java.lang.Object

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

      • QUIET

        🡇    
        public boolean QUIET
        A Verbose Flag. This field is public, and may be set as needed..
        Code:
        Exact Field Declaration Expression:
        public boolean QUIET;
        
      • sw

        🡅  🡇    
        public StorageWriter sw
        An output printer. This field is public, and may be set as needed.
        Code:
        Exact Field Declaration Expression:
        public StorageWriter sw = null;
        
    • Constructor Detail

      • WebSocketSender

        🡅  🡇    
        public WebSocketSender​(java.lang.String url,
                               boolean quiet)
                        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

      • disconnect

        🡅  🡇    
        public void disconnect()
        Closes the WebSocket connection to the Browser's Remote Debug Port.
        Code:
        Exact Method Body:
         webSocket.disconnect();
        
      • 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(C.BYELLOW + "Sending JSON:\n\t" + C.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
                 );
             }
         }