energy/example/dev-test/ipc-event/ipc-event.go

177 lines
6.4 KiB
Go
Raw Normal View History

2023-03-01 17:30:06 +08:00
package main
import (
"embed"
"fmt"
"github.com/energye/energy/cef"
"github.com/energye/energy/common/assetserve"
2023-03-02 18:47:39 +08:00
"github.com/energye/energy/example/dev-test/ipc-event/src"
2023-03-03 21:53:18 +08:00
"github.com/energye/energy/ipc"
2023-03-13 12:07:31 +08:00
"github.com/energye/golcl/lcl"
2023-03-14 23:37:22 +08:00
"time"
2023-03-13 10:50:45 +08:00
//_ "net/http/pprof"
2023-03-01 17:30:06 +08:00
)
//go:embed resources
var resources embed.FS
var cefApp *cef.TCEFApplication
func main() {
2023-03-13 10:50:45 +08:00
//go func() {
2023-03-13 17:54:22 +08:00
// http.ListenAndServe(":10000", nil)
2023-03-13 10:50:45 +08:00
//}()
2023-03-01 17:30:06 +08:00
//logger.SetEnable(true)
//logger.SetLevel(logger.CefLog_Debug)
//全局初始化 每个应用都必须调用的
cef.GlobalInit(nil, &resources)
//创建应用
cefApp = cef.NewApplication()
2023-03-21 20:46:59 +08:00
//cefApp.SetLogSeverity(consts.LOGSEVERITY_DEBUG)
2023-03-12 20:33:45 +08:00
//cefApp.SetSingleProcess(true)
2023-03-01 17:30:06 +08:00
//指定一个URL地址或本地html文件目录
cef.BrowserWindow.Config.Url = "http://localhost:22022/ipc-event.html"
2023-03-02 18:47:39 +08:00
//cef.BrowserWindow.Config.Url = "https://map.baidu.com/"
2023-03-01 17:30:06 +08:00
cef.BrowserWindow.Config.Title = "Energy - ipc-event"
cef.BrowserWindow.Config.IconFS = "resources/icon.ico"
2023-03-13 11:29:51 +08:00
2023-03-14 19:00:36 +08:00
// 测试用的入参 和 出参
var r0 = "字符串{}{}{}字符串[][]字符串"
var r2 = 66666611.0123
var r3 = true
var r4 = &MyError{error: "返回值"}
var r5 = make([]string, 3, 3)
r5[0] = "Array数组值1"
r5[1] = "Array数组值2"
r5[2] = "Array数组值3"
var r6 = make([]*src.StructVarDemo, 4, 4)
r6[0] = &src.StructVarDemo{StringField: "StringField1字符串1"}
r6[1] = &src.StructVarDemo{StringField: "StringField2字符串2", IntField: 111, BoolField: true, FloatField: 999.99, SubStructObj: &src.SubStructObj{StringField: "子对象String值", StructVarDemo: &src.StructVarDemo{StringField: "嵌套了嵌套了"}}}
var r7 = make([]src.StructVarDemo, 4, 4)
r7[0] = src.StructVarDemo{StringField: "r7参数字符串1"}
r7[1] = src.StructVarDemo{StringField: "r7参数字符串2"}
var r8 = map[string]string{}
r8["r8key1"] = "r8key1"
r8["r8key2"] = "r8key2"
var r9 = map[string]interface{}{}
r9["r9keyr6"] = r6
r9["r9keyr61"] = r6[1]
r9["r9keyr7"] = r7[1]
r9["r9keystrValue"] = "stringValue"
r9["r9keyintValue"] = 50000
r9["r9keyboolValue"] = true
r9["r9keyfloatValue"] = 5555555.99999
r9["r9keystrArrr5"] = r5
var r10 = make([]map[string]interface{}, 3)
r10[0] = r9
r10[1] = r9
r10[2] = r9
2023-03-14 23:37:22 +08:00
var tm = time.Now().Second()
2023-03-15 20:41:31 +08:00
var testGoEmit = 0
var testGoEmitAndCallback = 0
var testEmitName = 0
var testResultArgs = 0
2023-03-16 09:13:31 +08:00
var onTestName1Emit = 0
2023-03-15 14:29:45 +08:00
2023-03-21 20:46:59 +08:00
var ctime = 5
2023-03-15 14:29:45 +08:00
//监听事件js触发之后再触发js监听的事件
2023-03-14 17:30:45 +08:00
ipc.On("testGoEmit", func(context ipc.IContext) {
2023-03-15 20:41:31 +08:00
testGoEmit++
2023-03-14 23:37:22 +08:00
args := context.ArgumentList().JSONArray()
2023-03-21 20:46:59 +08:00
if tm >= 59-ctime {
2023-03-14 23:37:22 +08:00
tm = time.Now().Second()
}
2023-03-21 20:46:59 +08:00
if time.Now().Second() >= tm+ctime {
2023-03-16 09:13:31 +08:00
fmt.Println("GetIntByIndex", args.GetIntByIndex(0), "testGoEmit:", testGoEmit, "testGoEmitAndCallback:", testGoEmitAndCallback, "testEmitName:", testEmitName, "testResultArgs:", testResultArgs, "onTestName1Emit:", onTestName1Emit)
2023-03-14 23:37:22 +08:00
tm = time.Now().Second()
}
2023-03-15 14:29:45 +08:00
//触发JS监听的事件并传入参数
2023-03-21 16:53:02 +08:00
//ipc.Emit("onTestName1", r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10)
ipc.EmitAndCallback("onTestName2", []interface{}{r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10}, func(r1 string, r2 int, r3 float64, r4 bool) {
2023-03-21 22:35:40 +08:00
fmt.Println("onTestName1 callback", r1, r2, r3, r4)
2023-03-21 16:53:02 +08:00
})
2023-03-14 19:00:36 +08:00
})
2023-03-15 14:29:45 +08:00
ipc.On("testGoEmitAndCallback", func() {
2023-03-15 20:41:31 +08:00
testGoEmitAndCallback++
2023-03-21 20:37:00 +08:00
//fmt.Println("testGoEmitAndCallback")
2023-03-14 19:00:36 +08:00
//触发JS监听的函数并传入参数
2023-03-15 20:41:31 +08:00
//ipc.EmitAndCallback("onTestName2", []any{r0, r1 + count, r2, r3, r4, r5, r6, r7, r8, r9, r10}, func(r1 string) {
// //fmt.Println("onTestName2 r1: ", r1)
//})
2023-03-14 17:30:45 +08:00
})
2023-03-14 17:07:15 +08:00
2023-03-15 14:29:45 +08:00
ipc.On("testResultArgs", func(args1 int) (string, int, float64, bool, *MyError, []string, []*src.StructVarDemo, []src.StructVarDemo, map[string]string, map[string]interface{}, []map[string]interface{}) {
2023-03-15 20:41:31 +08:00
testResultArgs++
2023-03-16 21:19:27 +08:00
return r0, testResultArgs, r2, r3, r4, r5, r6, r7, r8, r9, r10
2023-03-11 14:24:22 +08:00
})
2023-03-11 00:02:03 +08:00
2023-03-15 14:29:45 +08:00
ipc.On("testInArgs", func(in1 string, in2 int, in3 float64, in4 bool, in5 []string, in6 []any, in7 map[string]any, in8 src.TestInArgs, in9 map[string]src.TestInArgs) (string, int, bool) {
2023-03-13 11:29:51 +08:00
fmt.Println("in1: ", in1)
fmt.Println("in2: ", in2)
fmt.Println("in3: ", in3)
fmt.Println("in4: ", in4)
fmt.Println("in5: ", in5)
fmt.Println("in6: ", in6)
fmt.Println("in7: ", in7)
fmt.Println("in8: ", in8, "in8.SubObj: ", in8.SubObj)
fmt.Println("in9: ", in9)
return "result testInArgs", 10, true //没有回调函数这些参数不会返回
2023-03-10 18:12:24 +08:00
})
2023-03-03 21:53:18 +08:00
ipc.On("testEmitName", func(context ipc.IContext) {
2023-03-15 20:41:31 +08:00
testEmitName++
2023-03-03 21:53:18 +08:00
argument := context.ArgumentList()
2023-03-21 20:37:00 +08:00
//fmt.Println("testEmitName", argument.Size(), context.BrowserId(), context.FrameId(), testEmitName)
2023-03-13 22:40:20 +08:00
//fmt.Println("data:", argument.GetByIndex(1).Data())
2023-03-10 14:42:15 +08:00
for i := 0; i < argument.Size(); i++ {
2023-03-13 22:40:20 +08:00
argument.GetByIndex(i)
//fmt.Println(i, "type:", value.Type(), "isInt:", value.IsInt())
2023-03-03 21:53:18 +08:00
}
2023-03-14 19:00:36 +08:00
2023-03-16 21:19:27 +08:00
context.Result(r0, testEmitName, r2, r3, r4, r5, r6, r7, r8, r9, r10)
2023-03-03 21:53:18 +08:00
})
2023-03-03 17:08:06 +08:00
2023-03-13 12:07:31 +08:00
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
2023-03-13 16:48:40 +08:00
if window.IsLCL() {
window.AsLCLBrowserWindow().BrowserWindow().SetOnConstrainedResize(func(sender lcl.IObject, minWidth, minHeight, maxWidth, maxHeight *int32) {
//Browser是在chromium加载完之后创建, 窗口创建时该对象还不存在
if window.AsLCLBrowserWindow().Browser() != nil {
fmt.Println("SetOnConstrainedResize Identifier", window.AsLCLBrowserWindow().Browser().Identifier())
fmt.Println("SetOnConstrainedResize MainFrame", window.AsLCLBrowserWindow().Browser().MainFrame())
}
})
}
2023-03-13 12:07:31 +08:00
})
2023-03-16 09:13:31 +08:00
//内置http服务链接安全配置
cef.SetBrowserProcessStartAfterCallback(func(b bool) {
fmt.Println("主进程启动 创建一个内置http服务")
//通过内置http服务加载资源
server := assetserve.NewAssetsHttpServer()
server.PORT = 22022
server.AssetsFSName = "resources" //必须设置目录名
server.Assets = &resources
go server.StartHttpServer()
2023-03-20 10:09:54 +08:00
//go func() {
// var i = 0
// time.Sleep(time.Second * 5)
// fmt.Println("5000")
// for true {
// i++
// time.Sleep(time.Second / 1000)
// //ipc.Emit("onTestName1", r0, i, r2, r3, r4, r5, r6, r7, r8, r9, r10)
2023-03-21 18:21:19 +08:00
// ipc.EmitAndCallback("onTestName2", []interface{}{r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10}, func(r1 string, r2 int, r3 float64, r4 bool) {
// fmt.Println("onTestName1 callback", r1, r2, r3, r4)
2023-03-20 10:09:54 +08:00
// })
// }
//}()
2023-03-16 09:13:31 +08:00
})
2023-03-01 17:30:06 +08:00
//运行应用
cef.Run(cefApp)
}
2023-03-12 00:51:00 +08:00
type MyError struct {
error string
}
func (m *MyError) Error() string {
return m.error
}