Created sessions life loop

This commit is contained in:
2025-11-16 09:08:43 +03:00
parent bf921ea8d4
commit 6417964385
3 changed files with 111 additions and 13 deletions

View File

@@ -1,21 +1,64 @@
package api
import "github.com/gorilla/websocket"
import (
"fmt"
"time"
func websocketSignaler(conn *websocket.Conn) {
"github.com/gorilla/websocket"
)
func websocketSignaler(conn *websocket.Conn, partyIsA bool, sessionId string) {
defer conn.Close()
sessionsMu.RLock()
openSession, ok := openSessions[sessionId]
sessionsMu.RUnlock()
if !ok {
// conn.WriteMessage(1, "{Session not found or something}")
return
}
fmt.Println("Session", openSession)
if partyIsA {
if openSession.aConn != nil {
openSession.aConn.Close()
}
openSession.aConn = conn
if !openSession.lastInteractedPartyIsA {
openSession.lastInteractedPartyIsA = true
openSession.lastInteractionTime = time.Now()
}
} else {
if openSession.bConn != nil {
openSession.bConn.Close()
}
openSession.bConn = conn
if openSession.lastInteractedPartyIsA {
openSession.lastInteractedPartyIsA = false
openSession.lastInteractionTime = time.Now()
}
}
sessionsMu.Lock()
openSessions[openSession.id] = openSession
sessionsMu.Unlock()
for {
msgType, msg, err := conn.ReadMessage()
if err != nil {
return
break
}
// Let's just echo for now as a placeholder
err = conn.WriteMessage(msgType, msg)
if err != nil {
return
break
}
}
}