clean up the websocket Dial code to correctly restart

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-12 06:18:06 -07:00
parent c926d8b441
commit 8807c8fc9b
2 changed files with 59 additions and 31 deletions

View File

@ -4,7 +4,7 @@
package main
import "flag"
// import "flag"
import "log"
import "net/url"
import "os"
@ -18,8 +18,7 @@ import pb "git.wit.com/wit/witProtobuf"
var gorillaConn *websocket.Conn
func readGorillaConn(done chan struct{}, conn *websocket.Conn) {
defer close(done)
func readGorillaConn(conn *websocket.Conn) {
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
@ -46,7 +45,6 @@ func gorillaSendProtobuf() {
if (gorillaConn == nil) {
log.Println("gorillaSendProtobuf() gorillaConn == nil")
log.Println("Need to re-open connection here")
// go gorillaDial()
return
}
msg := pb.CreateSampleEvent()
@ -60,30 +58,62 @@ func gorillaSendProtobuf() {
}
}
func gorillaDial() {
var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address")
func closeGorillaConn() {
if gorillaConn == nil {
log.Println("gorillaConn already was nil")
return
}
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := gorillaConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
// this must be called or set to be called with defer
gorillaConn.Close()
gorillaConn = nil
}
func gorillaDial(hostname string) {
// var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address")
for {
// u := url.URL{Scheme: "ws", Host: *addr, Path: "/event"}
u := url.URL{Scheme: "ws", Host: hostname, Path: "/event"}
log.Printf("connecting to %s", u.String())
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Println("gorilla Dial failed", err)
} else {
gorillaConn = conn
// handle inbound messages on the channel
readGorillaConn(conn)
closeGorillaConn()
}
time.Sleep(time.Second * 5) // try every 5 seconds
}
}
//
// this is a facinating code snippet that I wanted to leave here because it is
// so interesting. Complements to the gorilla websocket example developers
//
func beautifulAndFacinatingChannel() {
done := make(chan struct{})
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/event"}
log.Printf("connecting to %s", u.String())
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Println("gorilla Dial failed", err)
return
}
defer conn.Close()
gorillaConn = conn
done := make(chan struct{})
// handle inbound messages on the channel
go readGorillaConn(done, conn)
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
defer close(done)
for {
select {
@ -94,13 +124,9 @@ func gorillaDial() {
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
gorillaSendProtobuf()
// not sure what this does. nothing right?
select {
case <-done:
case <-time.After(time.Second):

View File

@ -27,6 +27,9 @@ import _ "github.com/andlabs/ui/winmanifest"
// always sorted slice (new project)
// https://github.com/yaa110/sslice
// several smart slice functions (new project. April 2019)
// https://github.com/elliotchance/pie
// Exit and write out the config
// (in yaml and json I guess.)
// switch to json once it can be made human readable
@ -62,8 +65,7 @@ func main() {
// setups up a dnssecsocket()
go retrySocket()
time.Sleep(3 * 1000 * 1000 * 1000)
go gorillaDial()
go gorillaDial("v000185.testing.com.customers.wprod.wit.com:9000")
// make this the main loop in an attempt to figure out the crashes
// do not change this until the GUI is stable