golem
golem copied to clipboard
Ending a connection inside of a OnConnect callback
First off, a thanks is in order - thanks for the excellent Websocket framework and excellent documentation/examples!
- I'm utilizing the framework's custom OnConnect function in my app.
- During this stage, the user has successfully initiated a websocket connection, and now I'm going to do some stuff.
- But what if something goes wrong? Instead of doing a log.Fatal on the whole server, it is obviously much better to just immediately terminate that specific websocket connection.
So, I tried to do something like this:
router.OnConnect(connOpen)
func connOpen(conn *ExtendedConnection, r *http.Request) {
// Get the session (this may be an empty session)
session, err := sessionStore.Get(r, sessionName)
if err != nil {
log.Error("Unable to get the session for HTTP request:", r)
conn.Connection.Close() // This DOES NOT work
return
}
// Get the username from the session
username := session.Values["username"].(string)
// Transfer the username from the session to a field in the ExtendedConnection for use later
// conn.Username = username
}
However, this doesn't actually work - you can't close the connection from inside a OnConnect callback, because the connection hasn't actually started yet, as illustrated here in the source code: https://github.com/trevex/golem/blob/master/router.go#L137
How is this supposed to be done in the Golem framework?
Edit - I reworded the question and the example code so that it would be more clear.