gin 使用gorilla 关闭 websocket 时候设置close code 和message
gin 使用gorilla 时建立websocket的教程很多博客都有写,但是很少有人讲关闭websocket时自定义 close code 和message。主要是使用:websocket.FormatCloseMessage, WriteControl 两个方法 。可用close code 的范围见文档:https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent#属性。 下面给出示例:
import (
"fmt"
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var (
upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
wsPriceOnce sync.Once
)
func Test(c *gin.Context) {
//升级get请求为webSocket协议
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
c.Writer.Write([]byte(err.Error()))
return
}
m := map[string]interface{}{
"msg" :"ok",
}
er := ws.WriteJSON(m)
fmt.Println(er)
// 自定义关闭消息
closeCode := 4001
message := "server closed ! "
closeMessage := websocket.FormatCloseMessage(closeCode, message)
deadline := time.Now().Add(time.Second)
e := ws.WriteControl(websocket.CloseMessage, closeMessage, deadline)
if e != nil {
fmt.Println(e)
}
}
在postman中测试效果图,我们看到close code 为4001, message 也是我们设置的 "server closed !" :

gin 使用gorilla 关闭 websocket 时候设置close code 和message的更多相关文章
- 7.1.1.关闭WebSocket连接
7.1.定义 7.1.1.关闭WebSocket连接 为_关闭WebSocket连接_,端点需关闭底层TCP连接.端点应该使用一个方法完全地关闭TCP连接,以及TLS会话,如果合适,丢弃任何可能已经接 ...
- Ubuntu中启用关闭Network-manager网络设置问题!
Ubuntu中启用关闭Network-manager网络设置问题! [Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/net ...
- 关于宽带接两台路由,并且第二台需要关闭DHCP的设置
关于宽带接两台路由,并且第二台需要关闭DHCP的设置 https://wenku.baidu.com/view/e317a12d4b35eefdc8d333cb?pcf=2#1
- easyswoole报错:failed: Error during WebSocket handshake: Unexpected response code: 200
WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpec ...
- WebSocket connection to,Error during WebSocket handshake: Unexpected response code: 404
使用标准的JSR 356注解时,需要使用tomcat 8.x版本,如果使用tomcat 7.x的版本,则需要继承WebSocketServlet,否则会报WebSocket connection to ...
- WebSocket handshake: Unexpected response code: 404
在执行 http://www.cnblogs.com/best/p/5695570.html 提供的 websocket时候, 报错了 “WebSocket handshake: Unexpe ...
- nginx反向代理 报错:Error during WebSocket handshake: Unexpected response code: 403
遇到nginx报错:websocket wss failed: Error during WebSocket handshake: Unexpected response code: 403 serv ...
- Linux Firewall 开启与关闭 以及sudo 设置
Linux 系统下,普通用户经常需要使用root 用户的权限,所以要经常切换到root用户,比较麻烦,因此可以给普通用户添加root 权限,需要在常规命令前面加上sudo 切换到root vi /e ...
- websocket如何设置?
根据之前写的步骤到当前的websocket设置 一.websocket设置: 1)在线程租下右键单击选择Sampler如图: 2)进行它的设置 2.保存 3.添加查看结果数和聚合报告(此处不详细说明, ...
- Ubuntu中启用关闭Network-manager网络设置问题! 【Server版本】
在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces.具体设置方法如下: (1) UbuntuServer 修改 ...
随机推荐
- Jenkins+Git+Gitlab+Ansible 持续集成和自动部署
- [273] High Five Update 3 OpCodez
[273] High Five Update 3 Client 00 SendLogOut 01 RequestAttack 03 RequestStartPledgeWar 04 RequestRe ...
- python菜鸟学习 : 16 pymongo和openpyxl的联合使用
# -*- coding: utf-8 -*-import pymongo, openpyxl, time# pymongo#条件删选mongodb数据def mongodb_expoter(db_n ...
- java开发微信APP支付
直接上代码 String nonce_str=WXPayUtil.generateNonceStr(); //WXPayUtil微信自己有的,自己下载,这里是生成随机字符串,下载地址(下载java的里 ...
- 【当年笔记】Collection集合部分
集合继承关系图 1)Vector 特点:线程安全,消耗偏大 2)ArrayList 特点:基于数组实现,随机访问某个元素效率高.集和头尾之间包括头插入删除操作效率较低,因为插入元素后,其他元素要后移. ...
- File.Exists 判断不了虚拟路径
https://www.shuzhiduo.com/topic/file-exists-%E5%88%A4%E6%96%AD%E4%B8%8D%E4%BA%86%E8%99%9A%E6%8B%9F%E ...
- Day22:数据库导入&分工&准备科研训练
今日完成的任务: 1.与组员共同排查jar包中的问题,发现是由于未导入数据库. 网上查询后在MySQL workbench中完成了导入导出(图中user数据库). 2.完成实验报告分工,我负责结论部分 ...
- webpack的核心概念
一.entry 指定webpack从哪个文件开始入手打包,下面是单入口 多页面开发,每个页面都有自己的js文件,多个文件,所以需要多个入口 我们看看多入口,单出口bundle.js,我们在src仅仅多 ...
- MTK平台总结
1. 通过cmdline参数不对printk打印速率进行限制:mt_boot.c kcmdline_append(" ignore_loglevel=1 printk.devkmsg=on ...
- unable to access 'https://github.com/.../...git': Recv failure: Connection was reset
解决git下载报错:fatal: unable to access 'https://github.com/.../...git': Recv failure: Connection was rese ...