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"
|
2019-05-26 04:19:23 -05:00
|
|
|
// import "git.wit.com/wit/gui"
|
2019-05-12 01:21:57 -05:00
|
|
|
|
|
|
|
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) {
|
2019-05-26 04:19:23 -05:00
|
|
|
log.Println("gorillaSendProtobuf() Event_Type =", msg.Type, "Event.Account.Nick =", msg.Account)
|
|
|
|
|
2019-05-12 01:21:57 -05:00
|
|
|
if (gorillaConn == nil) {
|
2019-05-26 04:19:23 -05:00
|
|
|
log.Println("gorillaSendProtobuf() CAN NOT SEND (socket connection isn't open)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
test, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err)
|
|
|
|
log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err)
|
|
|
|
log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err)
|
|
|
|
// TODO: return here?
|
2019-05-12 01:21:57 -05:00
|
|
|
return
|
|
|
|
}
|
2019-05-26 04:19:23 -05:00
|
|
|
|
|
|
|
err2 := gorillaConn.WriteMessage(websocket.BinaryMessage, test)
|
2019-05-12 01:21:57 -05:00
|
|
|
if err2 != nil {
|
2019-05-26 04:19:23 -05:00
|
|
|
log.Println("gorillaConn.WriteMessage() failed 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 {
|
2019-05-24 00:47:26 -05:00
|
|
|
u := url.URL{
|
|
|
|
Scheme: "ws",
|
|
|
|
Host: hostname,
|
|
|
|
Path: "/event",
|
|
|
|
}
|
2019-05-12 08:18:06 -05:00
|
|
|
log.Printf("connecting to %s", u.String())
|
|
|
|
|
2019-05-24 00:47:26 -05:00
|
|
|
conn, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
2019-05-12 08:18:06 -05:00
|
|
|
if err != nil {
|
2019-05-25 23:28:21 -05:00
|
|
|
log.Println("gorillaDial failed", err)
|
2019-05-12 08:18:06 -05:00
|
|
|
} else {
|
2019-05-24 00:47:26 -05:00
|
|
|
resp.Header.Set("jcarrname", "jcarrvalue")
|
2019-05-12 08:18:06 -05:00
|
|
|
gorillaConn = conn
|
2019-05-24 00:47:26 -05:00
|
|
|
origconn := conn.UnderlyingConn()
|
|
|
|
|
|
|
|
laddr := origconn.LocalAddr()
|
|
|
|
raddr := origconn.RemoteAddr()
|
|
|
|
log.Println("RESPONSE HEADER origconn.LocalAddr()= ", laddr)
|
|
|
|
log.Println("RESPONSE HEADER origconn.RemoteAddr()= ", raddr)
|
2019-05-12 08:18:06 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|