Can specify a time before auto-restart

This commit is contained in:
Pierre HUBERT 2020-04-12 17:24:49 +02:00
parent 98875aa47d
commit a98a65936f
5 changed files with 24 additions and 2 deletions

View File

@ -16,6 +16,11 @@ type Config struct {
Port int Port int
Path string Path string
Token string Token string
// Amount of time to wait before automatically
//quitting the application when the websocket
// connection was interrupted
Restart int
} }
// Get the URL associated with the configuration // Get the URL associated with the configuration

View File

@ -6,4 +6,9 @@ secure: false
hostname: 127.0.0.1 hostname: 127.0.0.1
port: 3000 port: 3000
path: /rtc_proxy/ws path: /rtc_proxy/ws
token: SecretToken token: SecretToken
# Number of seconds to wait before automatically
# closing the proxy (if WebSocket connection was
# interrupted)
restart: 0

10
main.go
View File

@ -2,7 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"time"
) )
func main() { func main() {
@ -17,6 +19,14 @@ func main() {
// First, load the config // First, load the config
conf := loadConfig(os.Args[1]) conf := loadConfig(os.Args[1])
/// Avoid to quick restart loop
defer timeBeforeQuit(&conf)
// Then connect to websocket // Then connect to websocket
openWs(&conf) openWs(&conf)
} }
func timeBeforeQuit(c *Config) {
log.Printf("Waiting %d seconds before quitting...", c.Restart)
time.Sleep(time.Second * time.Duration(c.Restart))
}

View File

@ -388,6 +388,7 @@ func newCall(mainOffer receivedSignal, r *activeRelay) {
} }
}() }()
// Enter messags loop
for { for {
// Receive new channels // Receive new channels

3
ws.go
View File

@ -36,7 +36,8 @@ func openWs(conf *Config) {
// Connect to Websocket // Connect to Websocket
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil { if err != nil {
log.Fatal("dial:", err) log.Println("dial:", err)
return
} }
defer c.Close() defer c.Close()