Skip to content

Commit

Permalink
修复:线程 JvEmitEventThread 和 JvHandleMessageThread CPU占用极高 和 报错Can't get…
Browse files Browse the repository at this point in the history
… WSEndpoint
  • Loading branch information
fanyong920 committed Nov 18, 2024
1 parent 0ce14be commit c83cdcc
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 156 deletions.
5 changes: 3 additions & 2 deletions example/src/main/java/com/ruiyun/example/A_LaunchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import java.util.List;

public class A_LaunchTest {
public final LaunchOptions launchOptions = LaunchOptions.builder().headless(false).build();
public final LaunchOptions launchOptions = LaunchOptions.builder().executablePath("C:\\Users\\fanyong\\Desktop\\jvppeteer\\example\\.local-browser\\win32-130.0.6723.58\\chrome-win32\\chrome.exe").headless(true).build();

/**
* 手动配置路径来启动浏览器
* 优先级: 1 高
*/
@Test
public void test99() throws IOException {
launchOptions.setExecutablePath("C:\\Users\\fanyong\\Desktop\\jvppeteer\\example\\.local-browser\\win32-127.0.6533.99\\chrome-win32\\chrome.exe");
launchOptions.setExecutablePath("C:\\Users\\fanyong\\Desktop\\jvppeteer\\example\\.local-browser\\win32-130.0.6723.58\\chrome-win32\\chrome.exe");
launchOptions.setUserDataDir("C:\\Users\\fanyong\\Desktop\\dir");
try (Browser browser = getBrowser()) {
//打开一个页面
Page page = browser.newPage();
Expand Down
60 changes: 50 additions & 10 deletions example/src/main/java/com/ruiyun/example/S_PageApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.ruiyun.jvppeteer.entities.Viewport;
import com.ruiyun.jvppeteer.entities.VisionDeficiency;
import com.ruiyun.jvppeteer.entities.WaitForSelectorOptions;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -44,7 +46,6 @@
import java.util.List;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class S_PageApiTest extends A_LaunchTest {
/**
Expand Down Expand Up @@ -912,17 +913,29 @@ public void test28() throws Exception {
Page page = browser.newPage();
page.goTo("https://www.geetest.com/demo/slide-en.html");
List<CookieParam> cookieParams = new ArrayList<>();
CookieParam cookieParam = new CookieParam();
cookieParam.setPath("/");
cookieParam.setName("ANON");
cookieParam.setSecure(true);
cookieParam.setSameSite("None");
CookieParam cookieParam1 = new CookieParam();
cookieParam1.setPath("/");
cookieParam1.setName("ANON");
cookieParam1.setSecure(true);
cookieParam1.setSameSite("None");
//UTC time in seconds, counted from January 1, 1970.
cookieParam.setExpires(System.currentTimeMillis() / 1000 -500000);
cookieParam.setValue("hahaah");
cookieParams.add(cookieParam);
cookieParam1.setExpires(System.currentTimeMillis() / 1000 - 500);
cookieParam1.setValue("hahaah");
//domain一定要设置正确
cookieParam.setDomain("www.geetest.com");
cookieParam1.setDomain("www.geetest.com");
cookieParams.add(cookieParam1);

CookieParam cookieParam2 = new CookieParam();
cookieParam2.setPath("/");
cookieParam2.setName("ANON2");
cookieParam2.setSecure(true);
cookieParam2.setSameSite("None");
//UTC time in seconds, counted from January 1, 1970.
cookieParam2.setExpires(System.currentTimeMillis() / 1000 + 500);
cookieParam2.setValue("hahaah2");
//domain一定要设置正确
cookieParam2.setDomain("www.geetest.com");
cookieParams.add(cookieParam2);
page.setCookie(cookieParams);
List<Cookie> cookies1 = page.cookies();
for (Cookie cookie : cookies1) {
Expand All @@ -932,4 +945,31 @@ public void test28() throws Exception {
browser.close();
}

/**
* 录制屏幕 录制格式gif
*/
@Test
public void test29() throws Exception {
launchOptions.setDevtools(true);
Browser browser = Puppeteer.launch(launchOptions);
Page page = browser.newPage();
page.goTo("https://www.geetest.com/demo/slide-en.html");
List<CookieParam> cookieParams = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date expires = format.parse("2025-05-30 21:23:57");
CookieParam cookieANON = new CookieParam();
cookieANON.setName("ANON");
cookieANON.setValue("A=3BA4B946A45109124C797104FFFFFFFF&E=1e78&W=1");
cookieANON.setDomain(".live.com");
cookieANON.setPath("/");
cookieANON.setHttpOnly(true);
cookieANON.setSecure(true);
cookieANON.setSameSite("None");
cookieANON.setExpires(expires.getTime());
cookieParams.add(cookieANON);
page.setCookie(cookieParams);
System.out.println("完成了");
browser.close();
}

}
23 changes: 23 additions & 0 deletions example/src/main/java/com/ruiyun/example/Test2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Browser;
import com.ruiyun.jvppeteer.core.Page;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.Target;
import java.util.List;

public class Test2 {
public static void main(String[] args) throws Exception {
try (Browser browser = Puppeteer.launch()) {
//打开一个页面
Page page = browser.newPage();
Target target1 = page.target();
System.out.println("one type=" + target1.type() + ", url=" + target1.url() + ",id=" + target1.getTargetId());
List<Target> targets = browser.targets();
//看看targets里面都有什么,包含browser,page,等类型,其中还包含了上面newPage得到page
for (Target target : targets) {
System.out.println("two type=" + target.type() + ", url=" + target.url() + ",id=" + target.getTargetId());
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/ruiyun/jvppeteer/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* 存放所用到的常量
Expand Down Expand Up @@ -424,4 +425,6 @@ public interface Constant {
put(WebPermission.MIDI_SYSEX, "midiSysex");
}
};

List<String> EVENTS = Arrays.stream(CDPSession.CDPSessionEvent.values()).map(CDPSession.CDPSessionEvent::getEventName).collect(Collectors.toList());
}
76 changes: 43 additions & 33 deletions src/main/java/com/ruiyun/jvppeteer/core/BrowserRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void destroyProcess(Process process) {
* @return 连接对象
* @throws InterruptedException 打断异常
*/
public Connection setUpConnection(boolean usePipe, int timeout, int slowMo, boolean dumpio) throws InterruptedException {
public Connection setUpConnection(boolean usePipe, int timeout, int slowMo, boolean dumpio) throws InterruptedException, IOException {
if (usePipe) {/* pipe connection*/
throw new LaunchException("Temporarily not supported pipe connect to chromuim.If you have a pipe connect to chromium idea,pleaze new a issue in github:https://github.com/fanyong920/jvppeteer/issues");
} else {/*websocket connection*/
Expand All @@ -159,7 +160,8 @@ public Connection setUpConnection(boolean usePipe, int timeout, int slowMo, bool
}

static class StreamReader {
private final StringBuilder ws = new StringBuilder();
private final StringBuilder chromeOutputBuilder = new StringBuilder();
private volatile String wsEndpoint = null;
private final int timeout;
private final boolean dumpio;
private final InputStream inputStream;
Expand All @@ -168,41 +170,50 @@ public StreamReader(int timeout, boolean dumpio, InputStream inputStream) {
this.timeout = timeout;
this.dumpio = dumpio;
this.inputStream = inputStream;
start();
}

public String getResult() {
StringBuilder chromeOutputBuilder = new StringBuilder();
long now = System.currentTimeMillis();
long base = 0;
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader)
) {
String line;
while ((line = reader.readLine()) != null) {
if (dumpio) {
System.out.println(line);
}
long remaining = timeout - base;
if (remaining <= 0) {
throw new TimeoutException("Timed out after " + timeout + " ms while trying to connect to the browser!"
+ "Chrome output: "
+ chromeOutputBuilder);
}
Matcher matcher = WS_ENDPOINT_PATTERN.matcher(line);
if (matcher.find()) {
return matcher.group(1);
}
if (chromeOutputBuilder.length() != 0) {
chromeOutputBuilder.append(System.lineSeparator());
private void start() {
Runnable runnable = () -> {
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line;
while ((line = reader.readLine()) != null) {
if (dumpio) {
System.out.println(line);
}
Matcher matcher = WS_ENDPOINT_PATTERN.matcher(line);
if (matcher.find()) {
wsEndpoint = matcher.group(1);
break;
}
if (chromeOutputBuilder.length() != 0) {
chromeOutputBuilder.append(System.lineSeparator());
}
chromeOutputBuilder.append(line);
}
chromeOutputBuilder.append(line);
base = System.currentTimeMillis() - now;
} catch (Exception e) {
LOGGER.error("Failed to launch the browser process!{},{}", chromeOutputBuilder, e.getMessage(), e);
}
};
new Thread(runnable).start();
}

} catch (Exception e) {
LOGGER.error("Failed to launch the browser process!please see TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md:", e);
public String waitFor() {
long now = System.currentTimeMillis();
long base = 0;
while (true) {
if (Objects.nonNull(wsEndpoint)) {
return wsEndpoint;
}
long remaining = timeout - base;
if (remaining <= 0) {
throw new TimeoutException("Failed to launch the browser process!"
+ "Chrome output: "
+ chromeOutputBuilder);
}
base = System.currentTimeMillis() - now;
}
throw new LaunchException("Can't get WSEndpoint");
}


Expand All @@ -216,8 +227,7 @@ public String getResult() {
* @return ws url
*/
private String waitForWSEndpoint(int timeout, boolean dumpio) {
StreamReader reader = new StreamReader(timeout, dumpio, process.getInputStream());
return reader.getResult();
return new StreamReader(timeout, dumpio, process.getInputStream()).waitFor();
}

public Process getProcess() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ruiyun/jvppeteer/entities/CookieParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CookieParam {
/**
* UTC time in seconds, counted from January 1, 1970.
*/
private Number expires;
private long expires;
private boolean httpOnly;
private boolean secure;
/**
Expand Down Expand Up @@ -58,11 +58,11 @@ public void setPath(String path) {
this.path = path;
}

public Number getExpires() {
public long getExpires() {
return expires;
}

public void setExpires(Number expires) {
public void setExpires(long expires) {
this.expires = expires;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/ruiyun/jvppeteer/launch/ChromeLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public Browser launch(LaunchOptions options) throws IOException {
options.setArgs(new ArrayList<>());
}
this.executablePath = this.computeExecutablePath(options.getExecutablePath(), options.getPreferredRevision());
String temporaryUserDataDir = options.getUserDataDir();
String temporaryUserDataDir = null;
List<String> defaultArgs = this.defaultArgs(options);
List<String> chromeArguments = new ArrayList<>(defaultArgs);
boolean isCustomUserDir = false;
boolean isCustomRemoteDebugger = false;
for (String arg : chromeArguments) {
if (arg.startsWith("--remote-debugging-")) {
isCustomRemoteDebugger = true;
} else if (arg.startsWith("--user-data-dir")) {
}
if (arg.startsWith("--user-data-dir")) {
isCustomUserDir = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ public Browser launch(LaunchOptions options) throws IOException {
options.setArgs(new ArrayList<>());
}
this.executablePath = this.computeExecutablePath(options.getExecutablePath(), options.getPreferredRevision());
String temporaryUserDataDir = options.getUserDataDir();
String temporaryUserDataDir = null;
List<String> defaultArgs = this.defaultArgs(options);
List<String> firefoxArguments = new ArrayList<>(defaultArgs);
boolean isCustomUserDir = false;
boolean isCustomRemoteDebugger = false;
for (String arg : firefoxArguments) {
if (arg.startsWith("--remote-debugging-")) {
isCustomRemoteDebugger = true;
} else if (arg.equals("-profile") || arg.equals("--profile")) {
}
if (arg.equals("-profile") || arg.equals("--profile")) {
isCustomUserDir = true;
}
}
Expand Down
Loading

0 comments on commit c83cdcc

Please sign in to comment.