add a gorilla websocket send
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
8b71cd76e5
commit
06e60fbcb6
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
46
main.go
46
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue