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
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "net/url"
|
|
|
|
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-23 21:36:20 -05:00
|
|
|
func gorillaReadProtobuf(conn *websocket.Conn) {
|
2019-05-12 01:21:57 -05:00
|
|
|
for {
|
2019-05-23 21:36:20 -05:00
|
|
|
log.Println("gorilla START gorillaReadProtobuf()", time.Now())
|
2019-05-12 01:21:57 -05:00
|
|
|
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-23 21:36:20 -05:00
|
|
|
log.Printf("gorillaReadProtobuf() successfully read protobuf from gorilla websocket")
|
2019-05-12 14:32:29 -05:00
|
|
|
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)
|
2019-05-23 21:36:20 -05:00
|
|
|
log.Println("gorilla END gorillaReadProtobuf()", time.Now())
|
2019-05-12 01:21:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:36:20 -05:00
|
|
|
func gorillaSendProtobuf(msg *pb.Event) {
|
|
|
|
log.Println("gorillaSendProtobuf() START", time.Now())
|
2019-05-12 01:21:57 -05:00
|
|
|
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
|
|
|
|
}
|
2019-05-23 21:36:20 -05:00
|
|
|
/*
|
2019-05-12 01:21:57 -05:00
|
|
|
msg := pb.CreateSampleEvent()
|
|
|
|
msg.Name = "test echo over gorilla websocket"
|
2019-05-23 21:36:20 -05:00
|
|
|
*/
|
2019-05-12 01:21:57 -05:00
|
|
|
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-21 20:18:59 -05:00
|
|
|
log.Println("gorillaSendProtobuf() END OK", time.Now())
|
2019-05-12 01:21:57 -05:00
|
|
|
}
|
|
|
|
|
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
|
2019-05-21 20:18:59 -05:00
|
|
|
log.Println("closeGorillaConn() END OK", time.Now())
|
2019-05-12 08:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-05-23 21:36:20 -05:00
|
|
|
gorillaReadProtobuf(conn)
|
2019-05-12 08:18:06 -05:00
|
|
|
closeGorillaConn()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 5) // try every 5 seconds
|
|
|
|
}
|
|
|
|
}
|