当前位置:网站首页>gorilla官方:golang开websocket client的示例代码
gorilla官方:golang开websocket client的示例代码
2022-07-07 17:52:00 【苦涩花开5486】
这是链接: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
}
}
}
边栏推荐
- LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
- 【STL】vector
- [sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
- 开源OA开发平台:合同管理使用手册
- 让这个 CRMEB 单商户微信商城系统火起来,太好用了!
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin diagram, set the palette parameter to customize the filling color of violin diagrams at different
- equals 方法
- 强化学习-学习笔记8 | Q-learning
- IP tools
- 项目经理『面试八问』,看了等于会了
猜你喜欢

转置卷积理论解释(输入输出大小分析)
Make this crmeb single merchant wechat mall system popular, so easy to use!

微信公众号OAuth2.0授权登录并显示用户信息

The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared

Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog

Redis master-slave and sentinel master-slave switchover are built step by step

Openeuler prize catching activities, to participate in?

Welcome to the markdown editor

Simulate the implementation of string class

剑指 Offer II 013. 二维子矩阵的和
随机推荐
Research and practice of super-resolution technology in the field of real-time audio and video
Make this crmeb single merchant wechat mall system popular, so easy to use!
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
ASP.NET体育馆综合会员管理系统源码,免费分享
Ucloud is a basic cloud computing service provider
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
9 atomic operation class 18 Rohan enhancement
Visual Studio 插件之CodeMaid自动整理代码
实训九 网络服务的基本配置
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
线性基
9 原子操作类之18罗汉增强
现在股票开户可以直接在网上开吗?安全吗。
IP 工具类
歌单11111
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!