func validateCookie(w http.ResponseWriter, req *http.Request) (chat.Account, error) { sessID := sessionIDFromCookie(req) log.Printf("Validating session: '%s'", sessID) if sessID == "" { log.Printf("Unable to validate empty cookie...") return nil, errors.New("Empty cookie") } acct, err := chat.LookupSession(sessID) if err != nil { //No account associated with this session, delete deleteSessionCookie(w) return nil, err } return acct, err }
/* webSocketHandler reads the sessionID from the websocket, identifies an account and joins an ongoing chat session if one exists //TODO: Actually manage disconnections properly. */ func webSocketHandler(ws *websocket.Conn) { //Client should send its sessionID as first message sc := bufio.NewScanner(ws) sc.Scan() if sc.Err() != nil { log.Printf("Error reading from client: %s", sc.Err().Error()) return } sessionID := strings.TrimSpace(sc.Text()) acct, err := chat.LookupSession(sessionID) if err != nil { ws.Write([]byte("Closing connection. Unable to find user: "******"Error: " + err.Error())) ws.Close() }