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的更多相关文章

  1. 7.1.1.关闭WebSocket连接

    7.1.定义 7.1.1.关闭WebSocket连接 为_关闭WebSocket连接_,端点需关闭底层TCP连接.端点应该使用一个方法完全地关闭TCP连接,以及TLS会话,如果合适,丢弃任何可能已经接 ...

  2. Ubuntu中启用关闭Network-manager网络设置问题!

    Ubuntu中启用关闭Network-manager网络设置问题! [Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/net ...

  3. 关于宽带接两台路由,并且第二台需要关闭DHCP的设置

    关于宽带接两台路由,并且第二台需要关闭DHCP的设置 https://wenku.baidu.com/view/e317a12d4b35eefdc8d333cb?pcf=2#1

  4. 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 ...

  5. WebSocket connection to,Error during WebSocket handshake: Unexpected response code: 404

    使用标准的JSR 356注解时,需要使用tomcat 8.x版本,如果使用tomcat 7.x的版本,则需要继承WebSocketServlet,否则会报WebSocket connection to ...

  6. WebSocket handshake: Unexpected response code: 404

    在执行    http://www.cnblogs.com/best/p/5695570.html  提供的 websocket时候, 报错了 “WebSocket handshake: Unexpe ...

  7. nginx反向代理 报错:Error during WebSocket handshake: Unexpected response code: 403

    遇到nginx报错:websocket wss failed: Error during WebSocket handshake: Unexpected response code: 403 serv ...

  8. Linux Firewall 开启与关闭 以及sudo 设置

    Linux 系统下,普通用户经常需要使用root 用户的权限,所以要经常切换到root用户,比较麻烦,因此可以给普通用户添加root 权限,需要在常规命令前面加上sudo 切换到root vi  /e ...

  9. websocket如何设置?

    根据之前写的步骤到当前的websocket设置 一.websocket设置: 1)在线程租下右键单击选择Sampler如图: 2)进行它的设置 2.保存 3.添加查看结果数和聚合报告(此处不详细说明, ...

  10. Ubuntu中启用关闭Network-manager网络设置问题! 【Server版本】

    在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces.具体设置方法如下: (1) UbuntuServer 修改 ...

随机推荐

  1. python官方文档:https://pypi.org/

    https://pypi.org/ Find, install and publish Python packages with the Python Package Index

  2. 硬件IIC的7位从机地址查询方式读写参考代码

    目录 本文中使用582测试,在整合先前博客中的代码的基础上,加上读写超时,加上返回值,加上16位从机寄存器地址的判断,希望读写各用一个函数就能解决硬件IIC的使用问题. #include " ...

  3. Iperf参数详解

    IperfIperf是一个网络性能测试工具,可以测试TCP和UDP最大带宽,具有多种参数和UDP特性,可以根据需要调整,报告带宽,延迟,数据包丢失 通用参数-b(bandwidth):指定UDP模式使 ...

  4. 前端记录:树形插件zTree 和 表格扩展 tableX

    树形插件zTree 表格扩展 tableX 项目实例 [地区联动实现_前端] <title>区域设置</title> <!--当前位置开始--> <div c ...

  5. IDEA Download missing driver files 下载失败解决方法

    使用IDEA连接数据库时可能会遇到缺失驱动文件的问题(提示Download missing driver files) 正常情况下点击Download后IDEA会自动下载缺失的文件,但是因为网络原因, ...

  6. excel快速生成相同内容

    先选中你所需要的单元格,如图所示: 输入自己想要的字符,然后按ctrl+enter,如图所示:

  7. Excel VBA实例

    有个朋友让帮忙看下excel数据怎么处理,初步一看有点复杂,难以下手.进一步分析.搜索,发现VBA可以很好地解决这个问题,总结记录一下. 问题描述:如下图,有N个sheet,每个sheet记录了一个公 ...

  8. Mint UI中的坑:datetime-picker在PC浏览器上可以显示弹出的日期选择,但是在手机上是空白

    在网上搜了一下,发现,在根组件App.vue上写入这个就可以了

  9. QT4.8.6移植

    sudo apt-get install libx11-dev libxext-dev libxtst-dev 配置: ./configure --prefix=/opt/qt4.8.6 -opens ...

  10. Go语言的常用函数方法整理大全-实践总结

    文章目录 前言 一.数据类型与相互转化 1.1 常用的数据类型 1.2 常用数据类型的转化 1.2.1 整形转字符串 1.2.2 字符串转整形 1.2.3 浮点转字符串 1.2.4 字符串转浮点 二. ...