sun-pc 7 months ago
parent
commit
4c6f12a7d5
1 changed files with 54 additions and 6 deletions
  1. 54 6
      src/main.go

+ 54 - 6
src/main.go

@@ -8,6 +8,7 @@ import (
 	"launcher/api"
 	"launcher/bus"
 	"launcher/conf"
+	"net"
 	"os"
 	"path"
 	"path/filepath"
@@ -15,6 +16,8 @@ import (
 	"strings"
 	"time"
 
+	netUrl "net/url"
+
 	"github.com/energye/energy/v2/cef"
 	"github.com/energye/energy/v2/cef/ipc"
 	"github.com/energye/energy/v2/common"
@@ -232,6 +235,25 @@ func quitRun() {
 	gNatsMrg.Quit()
 }
 
+func IsNetworkConnected(url string) bool {
+	u, err := netUrl.Parse(url)
+	if err != nil {
+		fmt.Println(err)
+		return false
+
+	}
+	url = u.Host
+
+	fmt.Println(url)
+	conn, err := net.DialTimeout("tcp", url, 5*time.Second)
+	fmt.Println(err)
+	if err != nil {
+		return false
+	}
+	defer conn.Close()
+	return true
+}
+
 func createRunUi(url string) *cef.TCEFApplication {
 	//全局初始化 每个应用都必须调用的
 	cef.GlobalInit(nil, &resources)
@@ -239,7 +261,6 @@ func createRunUi(url string) *cef.TCEFApplication {
 	var app = cef.NewApplication()
 	fmt.Println("cef-------------")
 	// app.SetSingleProcess(true)
-
 	app.SetAllowFileAccessFromFiles(true)
 	app.SetDisableWebSecurity(true)
 	app.SetAllowInsecureLocalhost(true)
@@ -265,6 +286,7 @@ func createRunUi(url string) *cef.TCEFApplication {
 			url = string(uri)
 		}
 	}
+
 	cef.BrowserWindow.Config.Url = fmt.Sprintf("%s?t=%d&shortcut=%s", url, time.Now().UnixMicro(), shortcut)
 	cef.BrowserWindow.Config.Title = "launcher"
 	cef.BrowserWindow.Config.IconFS = "assets/3dqueen.ico"
@@ -303,6 +325,29 @@ func createRunUi(url string) *cef.TCEFApplication {
 		fmt.Println("WebRTCMultipleRoutes=>", window.Chromium().WebRTCMultipleRoutes())
 		fmt.Println("WebRTCNonproxiedUDP=>", window.Chromium().WebRTCNonproxiedUDP())
 
+		ticker := time.NewTicker(10 * time.Second)
+		go func() {
+			for {
+				<-ticker.C
+				if !IsNetworkConnected(url) {
+					// 标识
+					fmt.Printf("network connect url:%s 失败\n", url)
+					cef.QueueSyncCall(func(int) {
+						// 这个闭包将在UI线程中执行
+						lcl.Application.MessageBox(" 网络连接失败! 请检查网络配置", "", 0)
+						time.Sleep(1 * time.Second)
+					})
+
+					isQuiting = true
+					window.CloseBrowserWindow()
+				} else {
+					// 网络已连接,可以发送另一个事件通知JavaScript
+					fmt.Printf("network connect url:%s 成功\n", url)
+				}
+			}
+
+		}()
+
 		api.RegDrag(event)
 
 		event.SetOnBeforeDownload(func(sender lcl.IObject, browser *cef.ICefBrowser, downloadItem *cef.ICefDownloadItem, suggestedName string, callback *cef.ICefBeforeDownloadCallback, window cef.IBrowserWindow) {
@@ -621,11 +666,14 @@ func trayDemo(browserWindow cef.IBrowserWindow) {
 		}
 	})
 	tray.AddMenuItem("退出", func() {
-		err := bus.NatsCenter.Publish("lancher.quitclicked", []byte{})
-		if err != nil {
-			isQuiting = true
-			browserWindow.CloseBrowserWindow()
-		}
+		isQuiting = true
+		browserWindow.CloseBrowserWindow()
+		// err := bus.NatsCenter.Publish("lancher.quitclicked", []byte{})
+		// fmt.Println("托盘退出:", err)
+		// if err != nil {
+		// 	isQuiting = true
+		// 	browserWindow.CloseBrowserWindow()
+		// }
 	})
 
 	api.QuitApp = func(flag int) {