diff --git a/example/common/common.go b/example/common/common.go new file mode 100644 index 00000000..d865ba9e --- /dev/null +++ b/example/common/common.go @@ -0,0 +1,20 @@ +package common + +import ( + "net" +) + +// Port 获取并返回未使用的net socket端口 +func Port() int { + //主进程获取端口号 + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") + if err != nil { + panic("Failed to Get unused Port number Error: " + err.Error()) + } + listen, err := net.ListenTCP("tcp", addr) + if err != nil { + panic("Failed to Get unused Port number Error: " + err.Error()) + } + defer listen.Close() + return listen.Addr().(*net.TCPAddr).Port +} diff --git a/example/ipc-on-emit/go-to-go/go_to_go.go b/example/ipc-on-emit/go-to-go/gotogo.go similarity index 100% rename from example/ipc-on-emit/go-to-go/go_to_go.go rename to example/ipc-on-emit/go-to-go/gotogo.go diff --git a/example/ipc-on-emit/go-to-js/go_to_js.go b/example/ipc-on-emit/go-to-js/gotojs.go similarity index 95% rename from example/ipc-on-emit/go-to-js/go_to_js.go rename to example/ipc-on-emit/go-to-js/gotojs.go index 17bb9e17..eac0f245 100644 --- a/example/ipc-on-emit/go-to-js/go_to_js.go +++ b/example/ipc-on-emit/go-to-js/gotojs.go @@ -8,6 +8,7 @@ import ( "github.com/energye/energy/v2/cef/exception" "github.com/energye/energy/v2/cef/ipc" "github.com/energye/energy/v2/cef/ipc/context" + "github.com/energye/energy/v2/example/common" "github.com/energye/energy/v2/pkgs/assetserve" "strings" ) @@ -23,8 +24,9 @@ func main() { }) //创建应用 cefApp := cef.NewApplication() + port := common.Port() //指定一个URL地址,或本地html文件目录 - cef.BrowserWindow.Config.Url = "http://localhost:22022/go-to-js.html" + cef.BrowserWindow.Config.Url = fmt.Sprintf("http://localhost:%d/go-to-js.html", port) cef.BrowserWindow.Config.Title = "Energy - go on event - js emit event" //在go中监听一个事件, 不带返回值 @@ -99,7 +101,7 @@ func main() { fmt.Println("主进程启动 创建一个内置http服务") //通过内置http服务加载资源 server := assetserve.NewAssetsHttpServer() - server.PORT = 22022 + server.PORT = port server.AssetsFSName = "resources" //必须设置目录名 server.Assets = &resources go server.StartHttpServer() diff --git a/example/ipc-on-emit/js-to-go/js_to_go.go b/example/ipc-on-emit/js-to-go/jstogo.go similarity index 100% rename from example/ipc-on-emit/js-to-go/js_to_go.go rename to example/ipc-on-emit/js-to-go/jstogo.go