当前位置:网站首页>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
}
}
}
边栏推荐
- Browse the purpose of point setting
- 关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
- SQL common optimization
- Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
- mock. JS returns an array from the optional data in the object array
- Semantic slam source code analysis
- 微信公众号OAuth2.0授权登录并显示用户信息
- 力扣 1961. 检查字符串是否为数组前缀
- Equals method
- LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
猜你喜欢
How to cooperate among multiple threads
Cloud 组件发展升级
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
CSDN语法说明
华南X99平台打鸡血教程
Some important knowledge of MySQL
vulnhub之tre1
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
【STL】vector
PMP practice once a day | don't get lost in the exam -7.7
随机推荐
力扣599. 两个列表的最小索引总和
使用高斯Redis实现二级索引
Version selection of boot and cloud
Equals method
torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
Force buckle 2319 Judge whether the matrix is an X matrix
毕业季|遗憾而又幸运的毕业季
力扣 2319. 判断矩阵是否是一个 X 矩阵
干货分享|DevExpress v22.1原版帮助文档下载集合
Force buckle 459 Duplicate substring
有了ST7008, 蓝牙测试完全拿捏住了
如何在软件研发阶段落地安全实践
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
Force buckle 599 Minimum index sum of two lists
CSDN语法说明
gorilla官方:golang开websocket client的示例代码
多个线程之间如何协同
关于自身的一些安排
力扣 599. 两个列表的最小索引总和
Simulate the implementation of string class