-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 "" |
@hjzCy The connection is already closed and removed from the server when Line 1039 in 15e3a17
neffos.OnNamespaceDisconnect event instead? It will be called on your conn.Close() too.
|
@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?The text was updated successfully, but these errors were encountered: