From 06e60fbcb6e0a8a4f27005e2b3ba5aaa5ea7f4e1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 May 2019 23:21:57 -0700 Subject: [PATCH] add a gorilla websocket send Signed-off-by: Jeff Carr --- gorilla.go | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ infoTabUI.go | 8 ++++ main.go | 46 +++++++++++--------- 3 files changed, 152 insertions(+), 21 deletions(-) create mode 100644 gorilla.go diff --git a/gorilla.go b/gorilla.go new file mode 100644 index 0000000..469fa97 --- /dev/null +++ b/gorilla.go @@ -0,0 +1,119 @@ +// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "flag" +import "log" +import "net/url" +import "os" +import "os/signal" +import "time" + +import "github.com/gorilla/websocket" + +import "github.com/golang/protobuf/proto" +import pb "git.wit.com/wit/witProtobuf" + +var gorillaConn *websocket.Conn + +func readGorillaConn(done chan struct{}, conn *websocket.Conn) { + defer close(done) + for { + mytype, message, err := conn.ReadMessage() + if err != nil { + log.Println("read:", err) + return + } + if (mytype == websocket.BinaryMessage) { + pdata := new(pb.Event) + err = proto.Unmarshal(message, pdata) + if (err != nil) { + log.Printf("readConn() something fucked up happened in Unmarshal") + } + log.Printf("recv binary: %s", pdata) + } else { + log.Printf("recv: %s", message) + // log.Printf("type, err = ", mytype, err) + } + } +} + +func gorillaSendProtobuf() { + if (gorillaConn == nil) { + log.Println("gorillaSendProtobuf() gorillaConn == nil") + return + } + msg := pb.CreateSampleEvent() + msg.Name = "test echo over gorilla websocket" + data, _ := proto.Marshal(msg) + err2 := gorillaConn.WriteMessage(websocket.BinaryMessage, data) + if err2 != nil { + log.Println("write:", err2) + return + } +} + +func gorillaDial() { + var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address") + + 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) + defer ticker.Stop() + + for { + select { + case <-done: + return + case t := <-ticker.C: + err := conn.WriteMessage(websocket.TextMessage, []byte(t.String())) + if err != nil { + log.Println("write:", err) + return + } + + msg := pb.CreateSampleEvent() + msg.Name = "test echo over gorilla websocket" + data, _ := proto.Marshal(msg) + err2 := conn.WriteMessage(websocket.BinaryMessage, data) + if err2 != nil { + log.Println("write:", err2) + return + } + 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 + } + select { + case <-done: + case <-time.After(time.Second): + } + return + } + } +} diff --git a/infoTabUI.go b/infoTabUI.go index 319295d..4e70b2a 100644 --- a/infoTabUI.go +++ b/infoTabUI.go @@ -80,6 +80,14 @@ func makeCloudInfoBox() ui.Control { }) vbox.Append(add5button, false) + // Send a protobuf over a gorilla websocket + add6button := ui.NewButton("gorillaSendProtobuf()") + add6button.OnClicked(func(*ui.Button) { + log.Println("gorillaSendProtobuf()") + gorillaSendProtobuf() + }) + vbox.Append(add6button, false) + hbox.Append(ui.NewVerticalSeparator(), false) vbox = ui.NewVerticalBox() diff --git a/main.go b/main.go index 6f464c2..2872a89 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,26 @@ import _ "github.com/andlabs/ui/winmanifest" // always sorted slice (new project) // https://github.com/yaa110/sslice +// Exit and write out the config +// (in yaml and json I guess.) +// switch to json once it can be made human readable +func onExit() { + log.Println("Sleep for 1 second") + time.Sleep(1 * 1000 * 1000 * 1000) + + f, err := os.Create("/tmp/cloud-control-panel.yaml") + if err == nil { + config.DumpTo(f, "yaml") + } + + f, err = os.Create("/tmp/cloud-control-panel.json") + if err == nil { + config.DumpTo(f, "json") + } + + os.Exit(0) +} + func main() { parseConfig() @@ -39,7 +59,11 @@ func main() { initChannel() go processEvents() - go retrySocket() + // setups up a dnssecsocket() + // go retrySocket() + + time.Sleep(3 * 1000 * 1000 * 1000) + go gorillaDial() // make this the main loop in an attempt to figure out the crashes // do not change this until the GUI is stable @@ -60,23 +84,3 @@ func main() { } */ } - -// Exit and write out the config -// (in yaml and json I guess.) -// switch to json once it can be made human readable -func onExit() { - log.Println("Sleep for 1 second") - time.Sleep(1 * 1000 * 1000 * 1000) - - f, err := os.Create("/tmp/cloud-control-panel.yaml") - if err == nil { - config.DumpTo(f, "yaml") - } - - f, err = os.Create("/tmp/cloud-control-panel.json") - if err == nil { - config.DumpTo(f, "json") - } - - os.Exit(0) -}