From 641796438594672cd036e177c579461718f98209 Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Sun, 16 Nov 2025 09:08:43 +0300 Subject: [PATCH] Created sessions life loop --- api/helper.go | 19 ++++++++++++++++ api/http_handler.go | 54 +++++++++++++++++++++++++++++++++++++-------- api/ws_handler.go | 51 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 api/helper.go diff --git a/api/helper.go b/api/helper.go new file mode 100644 index 0000000..c399f18 --- /dev/null +++ b/api/helper.go @@ -0,0 +1,19 @@ +package api + +import ( + "sync" + "time" + + "github.com/gorilla/websocket" +) + +type Session struct { + id string + aConn *websocket.Conn + bConn *websocket.Conn + lastInteractionTime time.Time + lastInteractedPartyIsA bool +} + +var openSessions = make(map[string]Session) +var sessionsMu sync.RWMutex diff --git a/api/http_handler.go b/api/http_handler.go index e9a688a..51e2c5d 100644 --- a/api/http_handler.go +++ b/api/http_handler.go @@ -4,6 +4,9 @@ import ( "fmt" "log" "net/http" + "time" + + "echo/misc" "github.com/gorilla/mux" "github.com/gorilla/websocket" @@ -22,29 +25,62 @@ func serveStaticFile(fileName string) http.HandlerFunc { } func createSession(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, ` - { - "sessionId":"XyZ123" + + sessionsMu.RLock() + var id string + for { + id, _ = misc.RandomString(10) + + _, ok := openSessions[id] + if !ok { + break // make sure we don't have duplicated ids + } + } - `) + sessionsMu.RUnlock() + + session := Session{ + id: id, + lastInteractionTime: time.Now(), + lastInteractedPartyIsA: true, + } + + sessionsMu.Lock() + openSessions[session.id] = session + sessionsMu.Unlock() + + fmt.Fprintf(w, + `{ + "sessionId":%s +}`, session.id) } var upgrader = websocket.Upgrader{} func signalWS(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - party := vars["party"] - session := vars["session"] + partyStr := vars["party"] + sessionId := vars["session"] - fmt.Printf("Initiating a websocket, party=%s session=%s\n", party, session) + fmt.Printf("Initiating a websocket, party=%s session=%s\n", partyStr, sessionId) + partyIsA := true + switch partyStr { + case "A": + + case "B": + partyIsA = false + default: + fmt.Printf("Party is invalid partyStr=%s", partyStr) + return + } conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - fmt.Printf("Error initiating websocket, r.URL=%s e=%s", r.URL.String(), err) + fmt.Printf("Error initiating websocket, r.URL=%s party=%v e=%s", r.URL.String(), partyIsA, err) } - websocketSignaler(conn) + websocketSignaler(conn, partyIsA, sessionId) } diff --git a/api/ws_handler.go b/api/ws_handler.go index e81645d..82c4988 100644 --- a/api/ws_handler.go +++ b/api/ws_handler.go @@ -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 } } + }