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

How to detect client disconnection ? #21

Open
gperrudin opened this issue Aug 20, 2015 · 6 comments
Open

How to detect client disconnection ? #21

gperrudin opened this issue Aug 20, 2015 · 6 comments

Comments

@gperrudin
Copy link

Hello,
I'm developping chat rooms based on Twitter hashtag with Server sent events.
I have a problem concerning the disconnection of the client. I run a goroutine to send messages to the client, but when the client disconnects, the goroutine still runs.
I don't know how to detect on the server side that the client is disconnected.
You can find a part of my code here : https://gist.github.com/Guiguillermo/f214fbda9aacec1ea5d9

Thanks !

@antage
Copy link
Owner

antage commented Aug 20, 2015

You can send to client a dummy "PING" data periodically. You can't detect a disconnect without sending any data.

@gperrudin
Copy link
Author

Thanks for your answer.
Do you know the better way to do this ? I was thinking that I could use ReponseWriter.Write() to send data but this does not automatically send data as it's buffered. I also don't know how to know if the client received the data.

@antage
Copy link
Owner

antage commented Aug 20, 2015

You can invoke conn.Read() and set ReadDeadline with a short duration before. If the connection is disconnected you get a error like "Broken pipe". If the connection is ok you get error "Timeout expired".

@gperrudin
Copy link
Author

The problem is that I don't see how to get the "conn" variable from my "ResponseWriter"/"Request".

@antage
Copy link
Owner

antage commented Aug 21, 2015

@gperrudin
Copy link
Author

The problem with Hijack() is that it takes control of the http connection, so the eventsource doesn't work anymore because it needs the http connection.
I haven't found any way to give back the http connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants