Server Sent Event how to replace http.CloseNotifier by Request.Context


(Benoit Pereira Da Silva) #1

Dear Gophers,

I do have implemented a Server Sent Event broker that relies on http.CloseNotifier CloseNotify to close le connection. But it is deprecated!

I try to figure out how to implement the de-connection using Request.Context instead of http.CloseNotifier

My code with the CloseNotifier, simply returns on notification :

// Listen to connection close and un-register messageChan
notify := w.(http.CloseNotifier).CloseNotify()
for {
   select {
      case <-notify:
	 return
    default:
       // On message
       commands := <-messageChan

Can i simply replace the notify channel by the Done channel?

done := r.Context().Done()
for {
   select {
      case <-done:
         return
      default:
        ...

I’m new to Go Land and i’m not sure to understand completely the notion of “http.context”.

Best regards from the south of France

Benoit


(Kamil Sala) #2

https://www.sohamkamani.com/blog/golang/2018-06-17-golang-using-context-cancellation/


(Kamil Sala) #3

hmm

connection broken - disable the network card

or

disable the application - disable the connection

is it possible to catch these events?

hmm, I think there is a problem with - disable the network card on client…

because the server on linux does not break the connection.
?


(Benoit Pereira Da Silva) #4

Hello Kamil,

I ve got an implementation of a Server Sent Event Broker that works.
My question (may be naïve) is :

Benoit


(Benoit Pereira Da Silva) #5

I ve been watching Fransec Campoy’s Just For Func episode on Context.

JustForFunc #9
JustForFunc #10

And the situation is clear!
I can replace w.(http.CloseNotifier).CloseNotify() channel by the done channel r.Context().Done()

Best regards.


(Kamil Sala) #6

Thanks, this information is also important to me. I suspected yes, but I wasn’t sure. That’s why I didn’t write anything.