2019-05-12 01:21:57 -05:00
|
|
|
// 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
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
// import "flag"
|
2019-05-12 01:21:57 -05:00
|
|
|
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
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
func readGorillaConn(conn *websocket.Conn) {
|
2019-05-12 01:21:57 -05:00
|
|
|
for {
|
|
|
|
mytype, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (mytype == websocket.BinaryMessage) {
|
2019-05-12 14:32:29 -05:00
|
|
|
protobufMsg := new(pb.Event)
|
|
|
|
err = proto.Unmarshal(message, protobufMsg)
|
2019-05-12 01:21:57 -05:00
|
|
|
if (err != nil) {
|
|
|
|
log.Printf("readConn() something fucked up happened in Unmarshal")
|
|
|
|
}
|
2019-05-12 14:32:29 -05:00
|
|
|
log.Printf("readGorillaConn() successfully read protobuf from gorilla websocket")
|
|
|
|
addEvent(protobufMsg)
|
2019-05-12 01:21:57 -05:00
|
|
|
} else {
|
|
|
|
log.Printf("recv: %s", message)
|
|
|
|
// log.Printf("type, err = ", mytype, err)
|
|
|
|
}
|
2019-05-12 02:01:23 -05:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
log.Println("gorilla readGorillaConn()", time.Now())
|
2019-05-12 01:21:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func gorillaSendProtobuf() {
|
|
|
|
if (gorillaConn == nil) {
|
|
|
|
log.Println("gorillaSendProtobuf() gorillaConn == nil")
|
2019-05-12 02:01:23 -05:00
|
|
|
log.Println("Need to re-open connection here")
|
2019-05-12 01:21:57 -05:00
|
|
|
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)
|
2019-05-12 02:01:23 -05:00
|
|
|
gorillaConn = nil
|
2019-05-12 01:21:57 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
func closeGorillaConn() {
|
|
|
|
if gorillaConn == nil {
|
|
|
|
log.Println("gorillaConn already was nil")
|
|
|
|
return
|
|
|
|
}
|
2019-05-12 01:21:57 -05:00
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
// 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, ""))
|
2019-05-12 01:21:57 -05:00
|
|
|
if err != nil {
|
2019-05-12 08:18:06 -05:00
|
|
|
log.Println("write close:", err)
|
2019-05-12 01:21:57 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
// 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() {
|
2019-05-12 01:21:57 -05:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
2019-05-12 01:21:57 -05:00
|
|
|
|
2019-05-12 02:01:23 -05:00
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
2019-05-12 08:18:06 -05:00
|
|
|
|
2019-05-12 01:21:57 -05:00
|
|
|
defer ticker.Stop()
|
2019-05-12 08:18:06 -05:00
|
|
|
defer close(done)
|
2019-05-12 01:21:57 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case t := <-ticker.C:
|
2019-05-12 01:26:24 -05:00
|
|
|
log.Println("gorilla NewTicker()", t.String())
|
2019-05-12 01:21:57 -05:00
|
|
|
case <-interrupt:
|
|
|
|
log.Println("interrupt")
|
|
|
|
|
2019-05-12 08:18:06 -05:00
|
|
|
gorillaSendProtobuf()
|
|
|
|
|
|
|
|
// not sure what this does. nothing right?
|
2019-05-12 01:21:57 -05:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|