当前位置:网站首页>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
}
}
}
边栏推荐
- 歌单11111
- 爬虫实战(七):爬王者英雄图片
- The project manager's "eight interview questions" is equal to a meeting
- AD域组策略管理
- A pot of stew, a collection of common commands of NPM and yarn cnpm
- PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
- J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- what‘s the meaning of inference
- PMP practice once a day | don't get lost in the exam -7.7
猜你喜欢
![最多可以参加的会议数目[贪心 + 优先队列]](/img/f3/e8e939e0393efc404cc159d7d33364.png)
最多可以参加的会议数目[贪心 + 优先队列]

mock.js从对象数组中任选数据返回一个数组

openEuler 资源利用率提升之道 01:概论

The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list

The project manager's "eight interview questions" is equal to a meeting

Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!

Ways to improve the utilization of openeuler resources 01: Introduction

杰理之关于 TWS 配对方式配置【篇】

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

Nunjuks template engine
随机推荐
Solve the problem of remote rviz error reporting
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
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
Ways to improve the utilization of openeuler resources 01: Introduction
LeetCode_7_5
mock. JS returns an array from the optional data in the object array
[RT thread env tool installation]
我的创作纪念日
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
String - string (Lua)
指定opencv非标准安装的版本
Welcome to the markdown editor
使用高斯Redis实现二级索引
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
Flink并行度和Slot详解
ASP.NET体育馆综合会员管理系统源码,免费分享
R language ggplot2 visualization: use the ggecdf function of ggpubr package to visualize the grouping experience cumulative density distribution function curve, and the linetype parameter to specify t
Interpretation of transpose convolution theory (input-output size analysis)
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse