当前位置:网站首页>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
}
}
}
边栏推荐
- 841. 字符串哈希
- The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
- R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
- 8 CAS
- Simulate the implementation of string class
- Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
- R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr
- 网信办公布《数据出境安全评估办法》,9 月 1 日起施行
猜你喜欢

Nunjuks template engine

九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!

Matplotlib drawing 3D graphics

AD域组策略管理

剑指 Offer II 013. 二维子矩阵的和

Kirin Xin'an won the bid for the new generation dispatching project of State Grid!

ASP.NET幼儿园连锁管理系统源码

一张图深入的理解FP/FN/Precision/Recall

Install mysql8 for Linux X ultra detailed graphic tutorial

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!!!
随机推荐
mock. JS returns an array from the optional data in the object array
吞吐量Throughout
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
索引总结(突击版本)
【STL】vector
what‘s the meaning of inference
Semantic slam source code analysis
Kirin Xin'an cloud platform is newly upgraded!
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
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
equals 方法
【Confluence】JVM内存调整
9 atomic operation class 18 Rohan enhancement
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
IP 工具类
Semantic SLAM源码解析
PMP practice once a day | don't get lost in the exam -7.7
干货分享|DevExpress v22.1原版帮助文档下载集合
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
what‘s the meaning of inference