idk
This commit is contained in:
+12
-14
@@ -21,13 +21,9 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
isAuthenticated = false
|
||||
continueConnection = true
|
||||
client = Client{}
|
||||
)
|
||||
client := Client{}
|
||||
|
||||
for continueConnection {
|
||||
for {
|
||||
var clientMessage map[string]any
|
||||
err := wsjson.Read(ctx, connection, &clientMessage)
|
||||
if err != nil {
|
||||
@@ -36,10 +32,13 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
if len(clientMessage) > 0 {
|
||||
if isAuthenticated {
|
||||
if client.IsAuthenticated {
|
||||
handleAuthenticatedMessage()
|
||||
} else {
|
||||
handleUnauthenticatedMessage(connection, &client, &clientMessage, &isAuthenticated, &continueConnection)
|
||||
if !handleUnauthenticatedMessage(connection, &client, &clientMessage) {
|
||||
closeConnection(connection)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,7 +57,7 @@ func sendMessageCloseIfTimeout(conn *websocket.Conn, message *map[string]any) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any, isAuthenticated *bool, continueConnection *bool) {
|
||||
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any) bool {
|
||||
token, ok := (*message)["token"].(string)
|
||||
if !ok {
|
||||
var errmsg = map[string]any{
|
||||
@@ -66,7 +65,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
|
||||
"message": "token required",
|
||||
}
|
||||
sendMessageCloseIfTimeout(conn, &errmsg)
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
err := SetClientFromToken(client, token)
|
||||
@@ -76,11 +75,10 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
|
||||
"message": "bad token",
|
||||
}
|
||||
sendMessageCloseIfTimeout(conn, &errmsg)
|
||||
continueConnection = false
|
||||
return
|
||||
return false
|
||||
}
|
||||
isAuthenticated = true
|
||||
clientInCache, ok := ClientsMap[]
|
||||
client.IsAuthenticated = true
|
||||
return true
|
||||
}
|
||||
|
||||
func handleAuthenticatedMessage() {
|
||||
|
||||
Reference in New Issue
Block a user