Optimize: IPC

This commit is contained in:
杨红岩 2022-12-14 09:49:14 +08:00
parent 1530337c23
commit ab9079a1c2
4 changed files with 24 additions and 6 deletions

View File

@ -63,5 +63,6 @@ func timeTask() {
//需要正确的获取类型,否则会失败
fmt.Println("JS返回数据:", arguments.GetString(0))
})
fmt.Println(ipc.IPC.Browser().ChannelIds())
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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