2015-09-24 01:29:30 +03:00
|
|
|
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
|
2015-09-24 01:23:26 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-18 06:48:51 +03:00
|
|
|
//go:build ignore
|
2015-09-24 01:29:30 +03:00
|
|
|
// +build ignore
|
|
|
|
|
2015-09-24 01:23:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
2015-12-10 03:50:38 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-09-24 01:23:26 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2015-11-02 21:39:51 +03:00
|
|
|
var addr = flag.String("addr", "localhost:8080", "http service address")
|
2015-09-24 01:23:26 +03:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2015-12-10 03:50:38 +03:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
|
|
|
|
2015-11-02 21:39:51 +03:00
|
|
|
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
|
2015-09-24 01:23:26 +03:00
|
|
|
log.Printf("connecting to %s", u.String())
|
|
|
|
|
2015-10-19 01:41:02 +03:00
|
|
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
2015-09-24 01:23:26 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("dial:", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2015-12-10 03:50:38 +03:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
2015-09-24 01:23:26 +03:00
|
|
|
go func() {
|
2015-12-10 03:50:38 +03:00
|
|
|
defer close(done)
|
2015-09-24 01:23:26 +03:00
|
|
|
for {
|
2024-06-14 05:53:52 +03:00
|
|
|
_, message, err := c.ReadMessage()
|
2015-09-24 01:23:26 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
2015-12-10 03:50:38 +03:00
|
|
|
return
|
2015-09-24 01:23:26 +03:00
|
|
|
}
|
2024-06-14 05:53:52 +03:00
|
|
|
log.Printf("recv: %s", message)
|
2015-09-24 01:23:26 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-11-02 21:39:51 +03:00
|
|
|
ticker := time.NewTicker(time.Second)
|
2015-09-24 01:23:26 +03:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-12-10 03:50:38 +03:00
|
|
|
for {
|
|
|
|
select {
|
2018-02-19 03:00:50 +03:00
|
|
|
case <-done:
|
|
|
|
return
|
2015-12-10 03:50:38 +03:00
|
|
|
case t := <-ticker.C:
|
|
|
|
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("write:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-interrupt:
|
|
|
|
log.Println("interrupt")
|
2018-02-19 03:00:50 +03:00
|
|
|
|
|
|
|
// Cleanly close the connection by sending a close message and then
|
|
|
|
// waiting (with timeout) for the server to close the connection.
|
2015-12-10 03:50:38 +03:00
|
|
|
err := c.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
|
2015-09-24 01:23:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|