当前位置:网站首页>Gorilla official: sample code for golang to open websocket client
Gorilla official: sample code for golang to open websocket client
2022-07-07 20:07:00 【Bitter flowers bloom 5486】
This is a link :websocket/examples/echo/client.go
// 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.
//go:build ignore
// +build ignore
package main
import (
"flag"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
var addr = flag.String("addr", "localhost:8080", "http service address")
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())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
done := make(chan struct{
})
go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
}
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
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")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
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
}
}
}
边栏推荐
- 力扣 989. 数组形式的整数加法
- CSDN语法说明
- torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- Nunjuks template engine
- Force buckle 2319 Judge whether the matrix is an X matrix
- Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
- ASP. Net kindergarten chain management system source code
- mysql 的一些重要知识
- YoloV6:YoloV6+Win10---训练自己得数据集
猜你喜欢
Welcome to the markdown editor
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
9 原子操作类之18罗汉增强
编译器优化那些事儿(4):归纳变量
有了ST7008, 蓝牙测试完全拿捏住了
The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
The project manager's "eight interview questions" is equal to a meeting
力扣 2319. 判断矩阵是否是一个 X 矩阵
华南X99平台打鸡血教程
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
随机推荐
sql 常用优化
Force buckle 88 Merge two ordered arrays
最多可以参加的会议数目[贪心 + 优先队列]
时间工具类
MIT科技评论文章:围绕Gato等模型的AGI炒作可能使人们忽视真正重要的问题
tp6 实现佣金排行榜
IP 工具类
pom. XML configuration file label: differences between dependencies and dependencymanagement
mock. JS returns an array from the optional data in the object array
Flink并行度和Slot详解
编译器优化那些事儿(4):归纳变量
力扣 989. 数组形式的整数加法
Force buckle 599 Minimum index sum of two lists
Equals method
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
How to buy bank financial products? Do you need a bank card?
力扣599. 两个列表的最小索引总和
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )