当前位置:网站首页>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
}
}
}
边栏推荐
- 杰理之关于 TWS 配对方式配置【篇】
- Unable to link the remote redis server (solution 100%
- 【Confluence】JVM内存调整
- AD域组策略管理
- Redis master-slave and sentinel master-slave switchover are built step by step
- How to buy stocks on your mobile phone and open an account? Is it safe to open an account
- 【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
- 歌单11111
- Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
- mock.js从对象数组中任选数据返回一个数组
猜你喜欢
mock. JS returns an array from the optional data in the object array
Jerry's headphones with the same channel are not allowed to pair [article]
Flink并行度和Slot详解
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
Matplotlib drawing 3D graphics
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
[RT thread env tool installation]
LeetCode_ 7_ five
# 欢迎使用Markdown编辑器
Make this crmeb single merchant wechat mall system popular, so easy to use!
随机推荐
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
IP 工具类
LeetCode_ 7_ five
2022如何评估与选择低代码开发平台?
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
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
8 CAS
openEuler 有奖捉虫活动,来参与一下?
最多可以参加的会议数目[贪心 + 优先队列]
位运算介绍
torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
Ucloud is a basic cloud computing service provider
Research and practice of super-resolution technology in the field of real-time audio and video
what‘s the meaning of inference
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
小试牛刀之NunJucks模板引擎
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
“本真”是什么意思
equals 方法