mirror of https://github.com/gorilla/websocket.git
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
|
// Copyright 2013 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"
|
||
|
"log"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
|
)
|
||
|
|
||
|
var addr = flag.String("addr", "localhost:8081", "http service address")
|
||
|
|
||
|
var dialer = websocket.Dialer{} // use default options
|
||
|
|
||
|
func main() {
|
||
|
flag.Parse()
|
||
|
log.SetFlags(0)
|
||
|
|
||
|
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
|
||
|
log.Printf("connecting to %s", u.String())
|
||
|
|
||
|
c, _, err := dialer.Dial(u.String(), nil)
|
||
|
if err != nil {
|
||
|
log.Fatal("dial:", err)
|
||
|
}
|
||
|
defer c.Close()
|
||
|
|
||
|
go func() {
|
||
|
defer c.Close()
|
||
|
for {
|
||
|
_, message, err := c.ReadMessage()
|
||
|
if err != nil {
|
||
|
log.Println("read:", err)
|
||
|
break
|
||
|
}
|
||
|
log.Printf("recv: %s", message)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
ticker := time.NewTicker(5 * time.Second)
|
||
|
defer ticker.Stop()
|
||
|
|
||
|
for t := range ticker.C {
|
||
|
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
||
|
if err != nil {
|
||
|
log.Println("write:", err)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|