Skip to content
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

The OnConnect and the OnDisconnect event hook trigger order is wrong when using the same clientid #78

Closed
youmisun opened this issue Jun 11, 2022 · 1 comment
Labels
discussion Something to be discussed

Comments

@youmisun
Copy link

youmisun commented Jun 11, 2022

my implement code

        server.Events.OnConnect = func(cl events.Client, pk events.Packet) {
		fmt.Printf("<< OnConnect client connected clientid=%s\n", cl.ID)
	}

	server.Events.OnDisconnect = func(cl events.Client, err error) {
		fmt.Printf("<< OnDisconnect client disconnected clientid=%s, err=%v\n", cl.ID, username, err)
	}

client 1 connects to the server, it prints

<< OnConnect client connected clientid=mqttx_f988b2da

client 2 use the same clientid as client 1 connets to the server, it prints

<< OnConnect client connected clientid=mqttx_f988b2da
<< OnDisconnect client disconnected clientid=mqttx_f988b2da err=client session re-established

i think when client 2 use the same clientid as client 1 connets to the server, it should print like

<< OnDisconnect client disconnected clientid=mqttx_f988b2da err=client session re-established
<< OnConnect client connected clientid=mqttx_f988b2da

it should be trigger the OnDisconnect before the OnConnect

@mochi-co mochi-co added the discussion Something to be discussed label Jun 20, 2022
@mochi-co
Copy link
Collaborator

I've reviewed the code associated with the OnConnect and OnDisconnect events and I don't think it's reasonably possible to swap the sequence of these events, as they are triggered as they occur in different goroutines with no cross-communication.

Your best option may be to track the client ids on the event handling side and then only execute the relevant code if the client has been reconnected - eg. server.ErrSessionReestablished

mochi-co pushed a commit that referenced this issue Apr 21, 2023
…as been reconnected (#198)

* storage hook should not execute the relevant code if the client has been reconnected #78

* add test cases for coverage decrease

add test cases for coverage decrease
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Something to be discussed
Projects
None yet
Development

No branches or pull requests

2 participants