Package Torello.Browser
Class WebSocketSender
- java.lang.Object
-
- Torello.Browser.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.
Hi-Lited Source-Code:- View Here: Torello/Browser/WebSocketSender.java
- Open New Browser-Tab: Torello/Browser/WebSocketSender.java
File Size: 13,595 Bytes Line Count: 368 '\n' Characters Found
-
-
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 theWebSocket
connection to the Browser's Remote Debug Port.void
send(int requestID, String requestJSON, Promise promise)
This method is the implementation-method for theSender
Functional-Interface.
-
-
-
Field Detail
-
QUIET
public boolean QUIET
A Verbose Flag. This field ispublic
, and may be set as needed..
-
sw
public StorageWriter sw
An output printer. This field ispublic
, and may be set as needed.
-
-
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 acceptsend(int, String, Promise)
messages to the browser.- Parameters:
url
- This is aURL
that is generated by the browser, and has a baseURL
that is just127.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 theWebSocket
connection to the Browser's Remote Debug Port.
-
send
public void send(int requestID, java.lang.String requestJSON, Promise promise)
This method is the implementation-method for theSender
Functional-Interface. This message accepts a Request & ID pair, and then transmits that request to a Browser's Remote-Debugging Port over theWebSocket
. It keeps thePromise
that was created by theScript
that sent this request, and saves thatPromise
until the Web-Socket receives a response about the request.- Specified by:
send
in interfaceSender<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 Browserpromise
- This is aPromise
which is automatically generated by theScript
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 ); } }
-
-