forked from SonicCloudOrg/sonic-driver-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/org/cloud/sonic/driver/webview/WebViewDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) [SonicCloudOrg] Sonic Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package org.cloud.sonic.driver.webview; | ||
|
||
import org.cloud.sonic.driver.common.tool.SonicRespException; | ||
import org.cloud.sonic.driver.webview.service.CdpClient; | ||
import org.cloud.sonic.driver.webview.service.impl.CdpClientImpl; | ||
|
||
public class WebViewDriver { | ||
private CdpClient cdpClient; | ||
|
||
/** | ||
* init driver | ||
* | ||
* @param url | ||
*/ | ||
public WebViewDriver(String url) { | ||
cdpClient = new CdpClientImpl(); | ||
cdpClient.newClient(url); | ||
} | ||
|
||
/** | ||
* get page source. | ||
* | ||
* @return | ||
* @throws SonicRespException | ||
*/ | ||
public String getPageSource() throws SonicRespException { | ||
return cdpClient.pageSource(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/cloud/sonic/driver/webview/service/CdpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) [SonicCloudOrg] Sonic Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package org.cloud.sonic.driver.webview.service; | ||
|
||
import org.cloud.sonic.driver.common.tool.Logger; | ||
import org.cloud.sonic.driver.common.tool.SonicRespException; | ||
|
||
/** | ||
* @author Eason | ||
* cdp client interface | ||
*/ | ||
public interface CdpClient { | ||
Logger getLogger(); | ||
|
||
void showLog(); | ||
|
||
void disableLog(); | ||
|
||
//Client handler. | ||
void newClient(String url); | ||
|
||
void closeClient(); | ||
|
||
String pageSource() throws SonicRespException; | ||
} |
147 changes: 147 additions & 0 deletions
147
src/main/java/org/cloud/sonic/driver/webview/service/impl/CdpClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (C) [SonicCloudOrg] Sonic Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package org.cloud.sonic.driver.webview.service.impl; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import org.cloud.sonic.driver.common.tool.Logger; | ||
import org.cloud.sonic.driver.common.tool.SonicRespException; | ||
import org.cloud.sonic.driver.poco.service.impl.WebSocketClientImpl; | ||
import org.cloud.sonic.driver.webview.service.CdpClient; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class CdpClientImpl implements CdpClient { | ||
private Logger logger; | ||
private String result = null; | ||
|
||
private org.java_websocket.client.WebSocketClient webSocketClient; | ||
|
||
public CdpClientImpl() { | ||
logger = new Logger(); | ||
} | ||
|
||
@Override | ||
public Logger getLogger() { | ||
return logger; | ||
} | ||
|
||
@Override | ||
public void showLog() { | ||
logger.showLog(); | ||
} | ||
|
||
@Override | ||
public void disableLog() { | ||
logger.disableLog(); | ||
} | ||
|
||
@Override | ||
public void newClient(String url) { | ||
connect(url); | ||
} | ||
|
||
@Override | ||
public void closeClient() { | ||
disconnect(); | ||
} | ||
|
||
@Override | ||
public String pageSource() throws SonicRespException { | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("id", 1); | ||
jsonObject.put("method", "Runtime.evaluate"); | ||
JSONObject params = new JSONObject(); | ||
params.put("expression", "window.location.toString()"); | ||
jsonObject.put("params", params); | ||
return sendAndReceive(jsonObject); | ||
} | ||
|
||
private String sendAndReceive(JSONObject jsonObject) throws SonicRespException { | ||
synchronized (WebSocketClientImpl.class) { | ||
webSocketClient.send(jsonObject.toString()); | ||
int wait = 0; | ||
while (result == null) { | ||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
wait++; | ||
if (wait >= 20) { | ||
break; | ||
} | ||
} | ||
if (result != null) { | ||
String re = result.toString(); | ||
result = null; | ||
return re; | ||
} else { | ||
throw new SonicRespException("result not found!"); | ||
} | ||
} | ||
} | ||
|
||
private void connect(String url) { | ||
URI ws = null; | ||
try { | ||
ws = new URI(url); | ||
} catch (URISyntaxException e) { | ||
e.printStackTrace(); | ||
} | ||
webSocketClient = new org.java_websocket.client.WebSocketClient(ws) { | ||
@Override | ||
public void onOpen(ServerHandshake serverHandshake) { | ||
logger.info("cdp ws connected."); | ||
} | ||
|
||
@Override | ||
public void onMessage(String s) { | ||
logger.info(s); | ||
result = s; | ||
} | ||
|
||
@Override | ||
public void onClose(int i, String s, boolean b) { | ||
logger.info("cdp ws close."); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
|
||
} | ||
}; | ||
webSocketClient.connect(); | ||
int waitConnect = 0; | ||
while (!webSocketClient.isOpen()) { | ||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
waitConnect++; | ||
if (waitConnect >= 20) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private void disconnect() { | ||
webSocketClient.close(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/org/cloud/sonic/driver/webview/WebViewDriverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.cloud.sonic.driver.webview; | ||
|
||
import org.cloud.sonic.driver.common.tool.SonicRespException; | ||
import org.junit.Test; | ||
|
||
public class WebViewDriverTest { | ||
static WebViewDriver webViewDriver; | ||
static String url = "ws:https://localhost:1234/devtools/page/2CFE3A51A00E0A8DCAFB3A2D3E190209"; | ||
|
||
@Test | ||
public void testDriver() throws SonicRespException { | ||
webViewDriver = new WebViewDriver(url); | ||
System.out.println(webViewDriver.getPageSource()); | ||
} | ||
} |