From a0f41fe2e1313fa20f0255efe651883d20f12e60 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Apr 2020 10:14:16 +0200 Subject: [PATCH] Automatically close websocket --- ws.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ws.go b/ws.go index b7e769b..39bed13 100644 --- a/ws.go +++ b/ws.go @@ -6,6 +6,8 @@ package main import ( "log" + "os" + "os/signal" "github.com/gorilla/websocket" ) @@ -13,6 +15,10 @@ import ( // Open websocket connection func openWs(conf *Config) { + // Auto-close connection when the system request it + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + u := conf.getURL() log.Printf("Connecting to %s", u.String()) @@ -23,6 +29,12 @@ func openWs(conf *Config) { } defer c.Close() + // Wait for interrupt signal + go func() { + <-interrupt + c.Close() + }() + // Read remote messages for {