duanfei9278 2018-12-19 08:36
浏览 120
已采纳

发生错误时如何关闭websockethandle

func WsConnectionHandle(ctx *gin.Context){
    ws, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
    if err != nil{
        config.Error.Println("connect err", err.Error())
        //config.Error.Fatalln("connect err")
        ctx.Abort() 
    }
    ...

when I use ctx.Abort() it still ran the code below, and if I use Fatalln, my main process will stop~

and this is my router

gin.SetMode(gin.ReleaseMode)
route := gin.Default()
resource.User(route.Group("/api/user"))
resource.KingAngAngle(route.Group("/api/king-and-angle", resource.JWTAuth()))
resource.Blessing(route.Group("/api/blessing", resource.JWTAuth()))
route.GET("/ws", chat.WsConnectionHandle)
  • 写回答

1条回答 默认 最新

  • doufendi9063 2018-12-19 08:58
    关注

    Well, ctx.Abort() is a function call, nothing more, nothing less. It doesn't affect the flow/scope of the function in which it's called. If you don't want to execute anything after calling ctx.Abort(), then just return to exit the function:

    if err != nil {
        log.Printf("some error: %+v", err)
        ctx.Abort()
        return // exit function
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b