-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Server-Sent-Events basically stream responses to the client using (I think) chunked encoding.
The data looks like this:
event: userconnect
data: {"username": "bobby", "time": "02:33:48"}
<1 sec later>
event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}
<1 sec later>
event: userdisconnect
data: {"username": "bobby", "time": "02:34:23"}
If the connection disconnects, the browser will automatically attempt to reconnect. That is part of the protocol.
For a server to close a connection, it must send a 204 No Content with empty body: https://javascript.info/server-sent-events
This signals to the browser to not attempt to reconnect.
Then the server must disconnect the client.
When we added the Early Hints:
func (ctx *RequestCtx) EarlyHints() error {
...
c := acquireWriter(ctx)
defer releaseWriter(ctx.s, c)
_, err := c.Write(strEarlyHints) // []byte("HTTP/1.1 103 Early Hints\r\n")
if err != nil {
return err
}
_, err = c.Write(strCRLF)
if err != nil {
return err
}
err = c.Flush()
if err != nil {
return err
}
}
return nil
}
Can we add the same functionality or a function to add a status code?
Also, is there way to disconnect?
When I try to send a Header: Connection: close it displays race condition warnings and then panics.
When I try to hijack,
fasthttpRequestCtx.Conn().Close()
it seems to panic on a nil pointer (perhaps due to streaming).