diff --git a/pkg/server/matchmaker/websessions.go b/pkg/server/matchmaker/websessions.go index 6ee1428..c12efd2 100644 --- a/pkg/server/matchmaker/websessions.go +++ b/pkg/server/matchmaker/websessions.go @@ -59,9 +59,23 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte conn: wsConnection, ctx: ctx, } + go func() { + for { + // the web app opens one websocket connection and sends a hello + // message asking for the latest state when a page is loaded that requires this. + // Minor issue at this time is that a notification is sent twice to the client. + p := make([]byte, 1024) + _, err := wsConnection.Read(p) + if err == nil { + session.notifications <- sessions.lastNotification + } else { + log.Printf("Got error reading %v", err) + return + } + } + }() sessions.sessions[session] = true sessions.logSessions() - session.notifications <- sessions.lastNotification return session } @@ -70,7 +84,6 @@ func GetUserLocation(r *http.Request) (*time.Location, error) { if tzName == "" { tzName = r.Header.Get("X-Timezone") } - //log.Println("GetUserLocation: timezone ", tzName) if tzName == "" { tzName = "UTC" } @@ -89,10 +102,7 @@ func (session *WebSession) WriteNotifications(location *time.Location, cancel co log.Println("channel closed") return } - //log.Println("Got notification: ", notification) - err := templates.State(notification, location).Render(context.Background(), session.conn) - if err != nil { - log.Printf("WS connection closed: %v", err) + if session.writeNotificationToClient(location, notification) { return } case <-timer.C: @@ -105,6 +115,15 @@ func (session *WebSession) WriteNotifications(location *time.Location, cancel co } } +func (session *WebSession) writeNotificationToClient(location *time.Location, notification *models.State) bool { + err := templates.State(notification, location).Render(context.Background(), session.conn) + if err != nil { + log.Printf("WS connection closed: %v", err) + return true + } + return false +} + func (sessions *WebSessions) SessionClosed(session *WebSession) { sessions.mutex.Lock() defer sessions.mutex.Unlock() diff --git a/pkg/server/templates/basepage.templ b/pkg/server/templates/basepage.templ index 3822a33..436fbd7 100644 --- a/pkg/server/templates/basepage.templ +++ b/pkg/server/templates/basepage.templ @@ -30,7 +30,7 @@ templ BasePage(tab int) { Converge - +