new start

This commit is contained in:
2026-03-22 14:26:49 +01:00
parent da5f87d67b
commit d756023952
11 changed files with 242 additions and 237 deletions
+56 -46
View File
@@ -23,85 +23,95 @@ func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
client := Client{}
var client = Client{WsConn: connection}
var isAuthenticated bool
defer closeConnection(&client)
for {
var clientMessage map[string]any
err := wsjson.Read(ctx, connection, &clientMessage)
if err != nil {
log.Printf("read error: %clientMessage", err)
log.Printf("read error: %v", err)
return
}
if len(clientMessage) > 0 {
if client.IsAuthenticated {
handleAuthenticatedMessage(connection, &client, &clientMessage)
} else {
if !handleUnauthenticatedMessage(connection, &client, &clientMessage) {
closeConnection(connection)
if isAuthenticated {
if !handleAuthenticatedMessage(&client, &clientMessage) {
return
}
} else {
if !handleUnauthenticatedMessage(&client, &clientMessage) {
return
}
isAuthenticated = true
}
}
}
}
func sendMessageCloseIfTimeout(conn *websocket.Conn, message *map[string]any) {
func sendMessageCloseIfTimeout(client *Client, message *map[string]any) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
err := wsjson.Write(ctx, conn, message)
err := wsjson.Write(ctx, client.WsConn, message)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
closeConnection(conn)
closeConnection(client)
}
log.Printf("write error: %v", err)
}
}
func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any) bool {
token, ok := (*message)["token"].(string)
if !ok {
var errmsg = map[string]any{
"type": WsServerResponse(BadMessage),
"message": "token required",
}
sendMessageCloseIfTimeout(conn, &errmsg)
return false
func sendToAllMessageCloseIfTimeout(message *map[string]any) {
mu.RLock()
defer mu.RUnlock()
for _, client := range CacheClients {
sendMessageCloseIfTimeout(client, message)
}
}
clientId, err := GetClientIdFromToken(token)
if err != nil {
var errmsg = map[string]any{
"type": WsServerResponse(InvalidCredentials),
"message": "bad token",
}
sendMessageCloseIfTimeout(conn, &errmsg)
return false
}
client, err = GetClientFromId(clientId)
if err != nil {
var errmsg = map[string]any{
"type": WsServerResponse(InvalidCredentials),
"message": "bad token",
}
sendMessageCloseIfTimeout(conn, &errmsg)
return false
}
client.IsAuthenticated = true
func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) bool {
sendToAllMessageCloseIfTimeout(clientMessage)
return true
}
func handleAuthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any) {
for _, sendTo := range clients {
if sendTo.IsAuthenticated && sendTo.Id != client.Id {
sendMessageCloseIfTimeout(conn, message)
func handleUnauthenticatedMessage(client *Client, clientMessage *map[string]any) bool {
token, ok := (*clientMessage)["token"].(string)
if !ok {
var msg = map[string]any{
"from": "server",
"error": "no token in message",
}
sendMessageCloseIfTimeout(client, &msg)
return false
}
clientId, err := TokenValidateGetId(token)
if err != nil {
var msg = map[string]any{
"from": "server",
"error": "invalid token",
}
sendMessageCloseIfTimeout(client, &msg)
return false
}
clientFromCache, err := CacheGetClientById(clientId)
if err != nil {
var msg = map[string]any{
"from": "server",
"error": "invalid token",
}
sendMessageCloseIfTimeout(client, &msg)
return false
}
*client = *clientFromCache
return true
}
func closeConnection(conn *websocket.Conn) {
conn.Close(websocket.StatusNormalClosure, "closing connection")
func closeConnection(client *Client) {
CacheDeleteClient(client.Id)
client.WsConn.CloseNow()
}