Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
How to monitor client disconnect / close connection? #41
Comments
|
@hjzCy You can't close the connection immediately from the Lines 174 to 179 in f143186
However, if you wait 1 second, it will be fired, although there is no logic on closing the connection without any checks when it's just connected (that's why we have the above check in the code too): ws.OnConnect = func(conn *neffos.Conn) error {
time.Sleep(time.Second)
conn.Close()
return nil
} |
…nnect event if the server closed the connection from its OnConnect event: #41
|
OK @hjzCy, a $ go get github.com/kataras/neffos@v0.0.16
# or go.mod:
# require github.com/kataras/neffos v0.0.16 ws := neffos.New([...])
ws.FireDisconnectAlways = true // <---HERE
ws.OnConnect = func(conn *neffos.Conn) error {
conn.Close()
return nil
}
ws.OnDisconnect = func(conn *neffos.Conn) {
log.Printf("[%s] disconnected from the server.", c)
} |
|
@kataras Thank you!very perfect! |
|
@kataras ws.OnDisconnect = apis.Logout
func Logout(conn *neffos.Conn) {
ctx := websocket.GetContext(conn)
token := strings.ReplaceAll(ctx.URLParam("token"), " ", "+")
decode := aes.CBCDecrypt(token)
// If `FireDisconnectAlways = false` then data can be obtained normally
fmt.Println("token:", token)
fmt.Println("decode:", decode)
// If `FireDisconnectAlways = false`, it will execute normally.
// Otherwise, the program will crash directly here ( Out of range )!!
username := decode[:strings.Index(decode, ", ")]
if client, ok := loginClients[username]; ok {
if client.topStatus {
log.Println("顶号断开连接,不删除用户数据。将顶号状态设置为 false。")
client.topStatus = false
} else {
log.Println("异常、或正常断开连接,删除用户数据。")
delete(loginClients, username)
}
}
} |
|
The question now is that after the client disconnects, the data returned from ctx := websocket.GetContext(conn)
fmt.Println("token:", ctx.URLParam("token")) // get failed, token is "" |
|
@kataras I cannot use |
|
@kataras The two problems above are that I didn't manually call |
|
没有手动调用 ws.OnDisconnect = apis.Logout
func Logout(conn *neffos.Conn) {
ctx := websocket.GetContext(conn)
// FireDisconnectAlways = false can get the result normally, otherwise "".
fmt.Println("token:", ctx.URLParam("token"))
}Cannot get url parameter data. |
|
Well, I found that even if A suggestion: |
|
Or add a signal: |
…nnect event if the server closed the connection from its OnConnect event: kataras#41
I want to do something when closing, or interrupting the client connection, such as cleaning up. I call
conn.Close()onws.OnConnect{}without triggeringws.OnDisconnect{}. what should I do?