2019-05-11 18:59:13 -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-11 19:22:14 -05:00
|
|
|
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"
|
|
|
|
|
2019-05-11 18:59:13 -05:00
|
|
|
var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address")
|
|
|
|
|
2019-05-11 19:07:28 -05:00
|
|
|
func readConn(done chan struct{}, conn *websocket.Conn) {
|
|
|
|
defer close(done)
|
|
|
|
for {
|
2019-05-11 19:22:14 -05:00
|
|
|
mytype, message, err := conn.ReadMessage()
|
2019-05-11 19:07:28 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
2019-05-11 19:22:14 -05:00
|
|
|
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)
|
|
|
|
}
|
2019-05-11 19:07:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:59:13 -05:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
|
|
|
|
|
|
|
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
|
|
|
|
log.Printf("connecting to %s", u.String())
|
|
|
|
|
2019-05-11 22:14:37 -05:00
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
2019-05-11 18:59:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("dial:", err)
|
|
|
|
}
|
2019-05-11 22:14:37 -05:00
|
|
|
defer conn.Close()
|
2019-05-11 18:59:13 -05:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2019-05-11 19:07:28 -05:00
|
|
|
// handle inbound messages on the channel
|
2019-05-11 22:14:37 -05:00
|
|
|
go readConn(done, conn)
|
2019-05-11 18:59:13 -05:00
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case t := <-ticker.C:
|
2019-05-11 22:14:37 -05:00
|
|
|
err := conn.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
2019-05-11 18:59:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("write:", err)
|
|
|
|
return
|
|
|
|
}
|
2019-05-11 19:22:14 -05:00
|
|
|
|
|
|
|
msg := pb.CreateSampleEvent()
|
|
|
|
msg.Name = "test echo over gorilla websocket"
|
|
|
|
data, _ := proto.Marshal(msg)
|
2019-05-11 22:14:37 -05:00
|
|
|
err2 := conn.WriteMessage(websocket.BinaryMessage, data)
|
2019-05-11 19:22:14 -05:00
|
|
|
if err2 != nil {
|
|
|
|
log.Println("write:", err2)
|
|
|
|
return
|
|
|
|
}
|
2019-05-11 18:59:13 -05:00
|
|
|
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.
|
2019-05-11 22:14:37 -05:00
|
|
|
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
2019-05-11 18:59:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("write close:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|