Skip to content

Commit

Permalink
Fix: #32 The rendering process ipc listens for adding a frameId group.
Browse files Browse the repository at this point in the history
Add a frame object to implement the ipc target.IWindow interface
  • Loading branch information
sxmxta committed Jun 23, 2024
1 parent 70c2fb8 commit 85fc273
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
20 changes: 17 additions & 3 deletions examples/ipc-on-emit/js-to-go/jstogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/cef/ipc"
"github.com/energye/energy/v2/cef/ipc/target"
_ "github.com/energye/energy/v2/examples/syso"
"github.com/energye/energy/v2/pkgs/assetserve"
"time"
Expand Down Expand Up @@ -32,20 +33,22 @@ func main() {
server.Assets = resources
go server.StartHttpServer()
// 在这里模拟传递参数在主进程触发JS监听的事件
})
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
// 定时执行web js
go timeTask()
go timeTask(window)
})
//运行应用
cef.Run(cefApp)
}

// 定时执行web js
func timeTask() {
func timeTask(window cef.IBrowserWindow) {
//这里模拟go中触发js监听的事件
var param0 = 0
for {
//每1秒钟执行一次
time.Sleep(time.Second)
time.Sleep(time.Second * 2)
fmt.Println("timeTask", param0)
param0++
//将数据发送出去
Expand All @@ -55,5 +58,16 @@ func timeTask() {
//需要正确的获取类型,否则会失败
fmt.Println("JS返回数据:", r1)
})
window.Chromium().AsTargetWindow()
browser := window.Browser()
frameNames := browser.GetFrameNames()
for i := 0; i < browser.FrameCount(); i++ {
frame := browser.GetFrameByName(frameNames[i].Value)
if !frame.IsMain() {
fmt.Println("\tname:", frameNames[i].Name, "value:", frameNames[i].Value, "frameId:", frame.Identifier())
targetFrame := target.NewTarget(frame, window.Browser().BrowserId(), frame.Identifier())
ipc.EmitTarget("js-on-event-demo", targetFrame, fmt.Sprintf("当前FrameId: %d Go发送的数据: %d", frame.Identifier(), param0), float64(param0+10))
}
}
}
}
48 changes: 25 additions & 23 deletions examples/ipc-on-emit/js-to-go/resources/js-to-go.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@
<head>
<meta charset="UTF-8">
<title>js-to-go</title>
<script type="application/javascript">
function clearMessage() {
document.getElementById("message").innerHTML = "";
}

function writeMessage(data) {
let message = document.getElementById("message");
message.innerHTML = message.innerHTML + data + "<br>"
}

//ipc.on函数有2个参数
//参数1 事件名 必填 string类型
//参数2 回调函数 非必填 function类型, go传递的参数
ipc.on('js-on-event-demo', function (p1, p2, p3) {
writeMessage("js-on-event-demo Go传递过来的数据: " + p1 + " p2: " + p2 + " p3: " + p3)
})
//带有返回值
ipc.on('js-on-event-demo-return', function (p1, p2) {
writeMessage("js-on-event-demo-return-Go传递过来的数据: " + p1 + " p2: " + p2)
//该返回值将返回到go中
return 'js返回的数据: ' + p1
})
</script>
</head>
<body style="margin: 0px;padding: 0px;">
JS中监听事件Go中调用<br>
<iframe id="123" name="一二三" src="sub-frame.html"></iframe>
<iframe id="456" name="四五六" src="sub-frame.html"></iframe>
<div id="message"></div>
<script type="application/javascript">
function clearMessage() {
document.getElementById("message").innerHTML = "";
}

function writeMessage(data) {
let message = document.getElementById("message");
message.innerHTML = message.innerHTML + data + "<br>"
}

//ipc.on函数有2个参数
//参数1 事件名 必填 string类型
//参数2 回调函数 非必填 function类型, go传递的参数
ipc.on('js-on-event-demo', function (p1, p2, p3) {
writeMessage("js-on-event-demo Go传递过来的数据: " + p1 + " p2: " + p2 + " p3: " + p3)
})
//带有返回值
ipc.on('js-on-event-demo-return', function (p1, p2) {
writeMessage("js-on-event-demo-return-Go传递过来的数据: " + p1 + " p2: " + p2)
//该返回值将返回到go中
return 'js返回的数据: ' + p1
})
</script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/ipc-on-emit/js-to-go/resources/sub-frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js-to-go</title>
</head>
<body style="margin: 0px;padding: 0px;">
JS中监听事件Go中调用<br>
<div id="message"></div>
<script type="application/javascript">
function clearMessage() {
document.getElementById("message").innerHTML = "";
}

function writeMessage(data) {
let message = document.getElementById("message");
message.innerHTML = message.innerHTML + data + "<br>"
}

//ipc.on函数有2个参数
//参数1 事件名 必填 string类型
//参数2 回调函数 非必填 function类型, go传递的参数
ipc.on('js-on-event-demo', function (p1, p2, p3) {
writeMessage("js-on-event-demo Go传递过来的数据: " + p1 + " p2: " + p2 + " p3: " + p3)
})
//带有返回值
ipc.on('js-on-event-demo-return', function (p1, p2) {
writeMessage("js-on-event-demo-return-Go传递过来的数据: " + p1 + " p2: " + p2)
//该返回值将返回到go中
return 'js返回的数据: ' + p1
})
</script>
</body>
</html>

0 comments on commit 85fc273

Please sign in to comment.