// 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 * 10) defer ticker.Stop() for { select { case <-done: return case t := <-ticker.C: log.Println("gorilla NewTicker()", t.String()) 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 } } }