parent
36a23911a8
commit
cffb4344d0
|
@ -0,0 +1,5 @@
|
||||||
|
server:
|
||||||
|
go run server.go
|
||||||
|
|
||||||
|
client:
|
||||||
|
go run client.go
|
|
@ -50,16 +50,16 @@ func main() {
|
||||||
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
|
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
|
||||||
log.Printf("connecting to %s", u.String())
|
log.Printf("connecting to %s", u.String())
|
||||||
|
|
||||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("dial:", err)
|
log.Fatal("dial:", err)
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
// handle inbound messages on the channel
|
// handle inbound messages on the channel
|
||||||
go readConn(done, c)
|
go readConn(done, conn)
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
@ -69,7 +69,7 @@ func main() {
|
||||||
case <-done:
|
case <-done:
|
||||||
return
|
return
|
||||||
case t := <-ticker.C:
|
case t := <-ticker.C:
|
||||||
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
err := conn.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("write:", err)
|
log.Println("write:", err)
|
||||||
return
|
return
|
||||||
|
@ -78,7 +78,7 @@ func main() {
|
||||||
msg := pb.CreateSampleEvent()
|
msg := pb.CreateSampleEvent()
|
||||||
msg.Name = "test echo over gorilla websocket"
|
msg.Name = "test echo over gorilla websocket"
|
||||||
data, _ := proto.Marshal(msg)
|
data, _ := proto.Marshal(msg)
|
||||||
err2 := c.WriteMessage(websocket.BinaryMessage, data)
|
err2 := conn.WriteMessage(websocket.BinaryMessage, data)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Println("write:", err2)
|
log.Println("write:", err2)
|
||||||
return
|
return
|
||||||
|
@ -88,7 +88,7 @@ func main() {
|
||||||
|
|
||||||
// Cleanly close the connection by sending a close message and then
|
// Cleanly close the connection by sending a close message and then
|
||||||
// waiting (with timeout) for the server to close the connection.
|
// waiting (with timeout) for the server to close the connection.
|
||||||
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("write close:", err)
|
log.Println("write close:", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue