2016-04-02 12 views
0

私はGoで小さなデスクトップWebアプリケーションを作成しています。このアプリケーションはローカルWebサーバーとして実行され、Chromeウィンドウは「アプリ」モードで生成されます。 Goプログラムはこの間にWebサーバーを継続して実行します。を使用してプロセス/プログラムが終了したかどうかを確認します。

ユーザーがこのChromeウィンドウを強制終了して、Webサーバーも閉じることができるようにする必要があります。

私は支援が必要な場所を示す以下のコメントを下さった。

package main 

import (
    "fmt" 
    "os/exec" 
) 

func main(){ 
    // Setup the application and arguments. 
    cmd := "chrome" 
    // URL will be local webserver. 
    args := []string{"--user-data-dir=c:\\","--window-size=800,600","--app=http://www.google.com"} 

    // Start local webserver here. 
    // ... 

    // Prepare Chrome in app mode. 
    cmdExec := exec.Command(cmd, args...); 

    // Start Chrome asynchronously. 
    cmdExec.Start() 

    // Show to the user on the command line that the application is running. 
    fmt.Println("Application in progress! Please close webapp to close webserver!") 

    // Keep the webserver running, do web app things... 

    // Watch for that process we started earlier. If the user closes that Chrome window 
    // Then alert the user that the webserver is now closing down. 

    // This is where I need help! 
    watchForProcessThatWeStartedEarlierForClosure...()//????   

    // And we are done! 
    fmt.Println("Application exit!") 
} 
+0

これは、Chromeのマルチプロセスアーキテクチャを考えるとややこしいかもしれません。 Webページがajax経由でサーバーに定期的にpingを送信し、pingが受信されない場合はタイムアウトする可能性がありますか? – nishantjr

答えて

2

cmdExecのWait()関数を使用して、子プロセスが終了するまで待機できます。

package main 

import (
    "fmt" 
    "os/exec" 
) 

func main(){ 
    // Setup the application and arguments. 
    cmd := "chrome" 
    // URL will be local webserver. 
    args := []string{"--user-data-dir=c:\\","--window-size=800,600","--app=http://www.google.com"} 

    // Start local webserver here. 
    // ... 

    // Prepare Chrome in app mode. 
    cmdExec := exec.Command(cmd, args...); 

    // Start Chrome asynchronously. 
    cmdExec.Start() 

    // Show to the user on the command line that the application is running. 
    fmt.Println("Application in progress! Please close webapp to close webserver!") 

    // Keep the webserver running, do web app things... 

    // Watch for that process we started earlier. If the user closes that Chrome window 
    // Then alert the user that the webserver is now closing down. 

    // Should probably handle the error here 
    _ = cmdExec.Wait()  

    // And we are done! 
    fmt.Println("Application exit!") 
} 

Chromiumでローカルにテストしました。ブラウザのウィンドウを閉じた後、Chromiumプロセスが存在してからWait()が戻るまでに数秒かかります。

関連する問題