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 修改 ...
随机推荐
- 人眼对led灯的闪烁识别度:写单片机的时候,小于15ms,我们人眼视为常亮
人眼对于每11毫秒闪烁一次约83赫兹基本感觉不到,每15毫秒闪烁一次约66赫兹轻微频闪. 所以写单片机的时候,小于15ms,我们人眼视为常亮
- 遍历operation
std::ostringstream out; double f8Value; NXOpen::CAM::CAMSetup *camSetup = displayPart->CAMSetup() ...
- 添加matlab到右键菜单并从当前目录打开
前言 因为每次打开matlab都得切换一下工作目录,由于目录不固定,所以感觉会很麻烦,所以就想着怎么添加matlab到右键菜单,并从指定目录打开,不过网上没有关于怎么设置这个的,不过却又很多设置cm ...
- unixbench测试CPU性能工具
UnixBench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linux系统主机的性能.Unixbench的主要测试项目有:系统调用.读写.进程. ...
- sql执行多条语句
问题背景: 今天想在xml里面写一个sql,执行批量修改, update question_rules set score=${rule.score}, data_describe=#{rule.da ...
- moco的使用方法
需要模拟第三方接口测试并调用,找了很多mock工具,在git上找到了moco,学习一番,分享一下. 一.下载moco,moco地址 下载后,是一个jar包. 二.配置moco的配置文件 (1)在jar ...
- PyTorch之初级使用
使用流程①. 数据准备; ②. 模型确立; ③. 损失函数确立; ④. 优化器确立; ⑤. 模型训练及保存 模块介绍Part Ⅰ: 数据准备torch.utils.data.Dataset torch ...
- RocketMq 下载安装
下载地址 https://rocketmq.apache.org/zh/download linux安装步骤 启动nameserver bin目录下启动nameserver nohup sh mqna ...
- git学习资料汇总
学习持续开发和持续继承CI/CD https://zhuanlan.zhihu.com/p/609519307 git工作流主题 https://github.com/oldratlee/trans ...
- 调度器42—进程exit退出流程
基于Linux-5.10 一.do_exit()简要流程 1. 执行路径 各驱动和内核机制中直接调用 SYSCALL_DEFINE1(exit, int, error_code) //exit.c 将 ...