From ab9079a1c246330fed162100000dee6772097ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BA=A2=E5=B2=A9?= Date: Wed, 14 Dec 2022 09:49:14 +0800 Subject: [PATCH] Optimize: IPC --- .../browser-ipc-on-emit/js-to-go/js-to-go.go | 1 + example/mini-browser/MiniBrowser.go | 8 ++++++++ ipc/cef-ipc-browser.go | 18 +++++++++++++----- ipc/cef-ipc.go | 3 ++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/example/browser-ipc-on-emit/js-to-go/js-to-go.go b/example/browser-ipc-on-emit/js-to-go/js-to-go.go index 7c9ca809..edf3e520 100644 --- a/example/browser-ipc-on-emit/js-to-go/js-to-go.go +++ b/example/browser-ipc-on-emit/js-to-go/js-to-go.go @@ -63,5 +63,6 @@ func timeTask() { //需要正确的获取类型,否则会失败 fmt.Println("JS返回数据:", arguments.GetString(0)) }) + fmt.Println(ipc.IPC.Browser().ChannelIds()) } } diff --git a/example/mini-browser/MiniBrowser.go b/example/mini-browser/MiniBrowser.go index b68e22bd..e84737d5 100644 --- a/example/mini-browser/MiniBrowser.go +++ b/example/mini-browser/MiniBrowser.go @@ -6,7 +6,9 @@ import ( "github.com/energye/energy/cef" "github.com/energye/energy/common/assetserve" "github.com/energye/energy/example/mini-browser/src" + "github.com/energye/energy/ipc" "github.com/energye/energy/logger" + "time" ) //go:embed libs @@ -39,6 +41,12 @@ func main() { server.AssetsFSName = "resources" //必须设置目录名 server.Assets = &resources go server.StartHttpServer() + go func() { + for { + time.Sleep(time.Second) + fmt.Println("ChannelIds:", ipc.IPC.Browser().ChannelIds()) + } + }() }) cef.Run(cefApp) } diff --git a/ipc/cef-ipc-browser.go b/ipc/cef-ipc-browser.go index 474df510..26f52cd4 100644 --- a/ipc/cef-ipc-browser.go +++ b/ipc/cef-ipc-browser.go @@ -87,10 +87,6 @@ func (m *event) add(name string, eventCallback EventCallback) { } } -func (m *event) removeOnEvent(name string) { - delete(m.event, name) -} - func (m *event) Get(name string) EventCallback { if call, ok := m.event[name]; ok { return call @@ -105,10 +101,22 @@ func (m *browserChannel) Channel(channelId int64) *channel { return nil } +func (m *browserChannel) ChannelIds() (result []int64) { + m.channel.Range(func(key, value any) bool { + result = append(result, key.(int64)) + return true + }) + return +} + func (m *browserChannel) putChannel(channelId int64, value *channel) { m.channel.Store(channelId, value) } +func (m *event) removeOnEvent(name string) { + delete(m.event, name) +} + func (m *browserChannel) Close() { if m.unixListener != nil { m.unixListener.Close() @@ -174,7 +182,7 @@ func (m *browserChannel) singleProcessChannelId() (int64, bool) { //单进程,只有一个IPC连接,直接取出来就好 m.channel.Range(func(key, value any) bool { channelId = key.(int64) - return true + return false }) if channelId != 0 { return channelId, true diff --git a/ipc/cef-ipc.go b/ipc/cef-ipc.go index a37dad05..4a3da924 100644 --- a/ipc/cef-ipc.go +++ b/ipc/cef-ipc.go @@ -80,7 +80,6 @@ type IEventOn interface { type IEventEmit interface { IEventOn Events() *event - Channel(channelId int64) *channel SetOnEvent(callback func(event IEventOn)) //IPC 事件监听 Emit(eventName string, arguments IArgumentList) //IPC 异步事件触发 EmitAndCallback(eventName string, arguments IArgumentList, callback IPCCallback) //IPC 回调事件触发 @@ -91,6 +90,8 @@ type IEventEmit interface { type IBrowseEventEmit interface { IEventOn IEventEmit + Channel(channelId int64) *channel //IPC 获取指定的通道 + ChannelIds() (result []int64) //IPC 获取所有通道 EmitChannelId(eventName string, channelId int64, arguments IArgumentList) //IPC 异步事件触发-指定通道ID EmitChannelIdAndCallback(eventName string, channelId int64, arguments IArgumentList, callback IPCCallback) //IPC 回调事件触发-指定通道ID EmitChannelIdAndReturn(eventName string, channelId int64, arguments IArgumentList) IIPCContext //IPC 返回值事件触发(处理时间复杂操作尽量不使用,容易造成UI进程锁死)-指定通道ID