kataras/neffos

How to monitor client disconnect / close connection?

开放

#41 创建于 2020年5月12日

 (11 条评论) (0 个反应) (0 位负责人)Go (53 个派生)github user discovery
enhancementgood first issue

仓库指标

星标
 (628 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

I want to do something when closing, or interrupting the client connection, such as cleaning up. I call conn.Close() on ws.OnConnect{} without triggering ws.OnDisconnect{}. what should I do?

package main

import (
    "fmt"
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/websocket"
    "github.com/kataras/neffos"
)

func main() {
    app := iris.Default()
    
    ws := neffos.New(websocket.DefaultGorillaUpgrader, neffos.Namespaces{
    	neffos.OnNativeMessage: neffos.Events{
    	    "login": func(ns *neffos.NSConn, msg neffos.Message) error {
    	        return nil
    	    },
    	},
    })
    
    ws.OnConnect = func(conn *neffos.Conn) error {
        allowConnect = true
        // some operations...
        if !allowConnect {
            conn.Close()
        }
    	return nil
    }
    
    ws.OnDisconnect = func(c *neffos.Conn) {
        // No shutdown signal is received and the method body is not triggered.
    	fmt.Println("OnDisconnect")
    }
    
    app.Get("/login", websocket.Handler(ws))
    app.Logger().Fatal(app.Listen(":8080"))
}

贡献者指南