当前位置:网站首页>Golang gin websocket problem
Golang gin websocket problem
2022-04-22 18:11:00 【-multiflora-】
Interface Overview
// Talk about the interface
func Handler(c *gin.Context) {
fmt.Println(" Enter function ")
// Get sender uid And the sender uid
token := module.WsGetTokenFromHeader(c.Request.Header)
fmt.Println(token)
stuNum := dao.Redis.Get(token).Val()
toStuNum := c.Query("toStuNum")
if stuNum == toStuNum {
c.JSON(http.StatusOK, gin.H{
"status": 203,
"msg": " I can't communicate with myself ",
})
return
}
// upgrade websocket agreement
conn, err := (&websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
// Put the obtained parameters into this array
Subprotocols: []string{
token},
}).Upgrade(c.Writer, c.Request, nil) // upgrade ws agreement
if err != nil {
http.NotFound(c.Writer, c.Request)
return
}
// Create a user instance
client := &models.Client{
ID: createID(stuNum, toStuNum), //1->2
SendID: createID(toStuNum, stuNum), //2->1
FromUsername: stuNum,
ToUsername: toStuNum,
Socket: conn,
Send: make(chan []byte),
}
// User registration to user management
mux.Lock()
models.Manager.Register <- client
mux.Unlock()
go client.Read()
go client.Write()
go models.BatchRead(toStuNum, stuNum)
}
Interface slice
If the front end uses Sec-WebSocket-Protocol Field passing token It will cause websocket The problem of disconnection as soon as it is connected
At that time, it needs to be in Upgrader add to
// Put the obtained parameters into this array
Subprotocols: []string{
token},
To establish a connection
websocket upgrade
conn, err := (&websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
// Put the obtained parameters into this array
Subprotocols: []string{
token},
}).Upgrade(c.Writer, c.Request, nil) // upgrade ws agreement
if err != nil {
http.NotFound(c.Writer, c.Request)
return
}

版权声明
本文为[-multiflora-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221753165123.html
边栏推荐
- Common libraries for video playback
- Overview of cement data in Guangdong
- Expansion convolution paper notes
- 18730 涂色问题 (快速幂的两种写法)
- Soft test advanced item notes | soft skills
- Soft test high item notes | operation and maintenance service classification
- Fastjson 2 来了,性能继续提升,还能再战十年
- Topic5——198. 打家劫舍
- Soft test high item notes | project schedule management
- Halo open source project learning (I): project launch
猜你喜欢

leetcode 111:二叉树的最小深度

还弄不懂相对路径和绝对路径,这篇文章带你简单剖析

Soft test high item notes | information system security

Leetcode 108: convert an ordered array into a binary search tree

Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241

MySQL排序与分页

Soft test high item notes | contents of project proposal

Soft test high item notes | demand classification

golang-gin-websocket问题

Soft test high item notes | typical life cycle model of information system project
随机推荐
国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241
Dpdk captures traffic from a given port / queue
uniapp处理强制刷新问题
Esprima ECMAScript 解析架构
Must brush the simulated question bank and answers of the latest eight members of Jiangxi in 2022
The content exported by the rich text editor is PDF or docx
The USB flash disk cannot be recognized when it is inserted into the computer. How to solve it?
Applets -- components
National information exchange model (NIEM) operation manual
CISP examination resource sharing
Notes on soft test high items | contents of feasibility study
Pikachu File Inclusion (File Inclusion Vulnerability)
我国有哪些完全自研的电脑硬件?
Interface test mock practice (II) | complete batch manual mock in combination with JQ
Soft test high item notes | basis of project evaluation
学习文档。
18730 coloring problem (two ways of writing fast power)
Notes on soft test high items | information system life cycle
秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”
DLL的封装及调用