initial state is now sent again when the websocket connection is
established. Also throttling based on user input. When browser sends multiple messages per second the user will still only get one notification per second at most.
This commit is contained in:
parent
9a5aaf56eb
commit
57c77e7d07
@ -3,6 +3,7 @@ package ui
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.wamblee.org/converge/pkg/models"
|
"git.wamblee.org/converge/pkg/models"
|
||||||
|
"git.wamblee.org/converge/pkg/support/throttling"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -24,7 +25,8 @@ type WebSession struct {
|
|||||||
|
|
||||||
func NewWebSessions(notifications chan *models.State) *WebSessions {
|
func NewWebSessions(notifications chan *models.State) *WebSessions {
|
||||||
websessions := &WebSessions{
|
websessions := &WebSessions{
|
||||||
sessions: make(map[*WebSession]bool),
|
sessions: make(map[*WebSession]bool),
|
||||||
|
lastNotification: models.NewState(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -76,7 +78,16 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte
|
|||||||
conn: wsConnection,
|
conn: wsConnection,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initial notification as soon as client connects.
|
||||||
go func() {
|
go func() {
|
||||||
|
session.notifications <- sessions.lastNotification
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
throttler := throttling.NewAsyncThrottler(func(state *models.State) {
|
||||||
|
session.notifications <- state
|
||||||
|
}, time.Second, time.Second)
|
||||||
for {
|
for {
|
||||||
// the web app opens one websocket connection and sends a hello
|
// 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.
|
// message asking for the latest state when a page is loaded that requires this.
|
||||||
@ -84,7 +95,7 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte
|
|||||||
p := make([]byte, 1024)
|
p := make([]byte, 1024)
|
||||||
_, err := wsConnection.Read(p)
|
_, err := wsConnection.Read(p)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
session.notifications <- sessions.lastNotification
|
throttler.Notify(sessions.lastNotification)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Got error reading %v", err)
|
log.Printf("Got error reading %v", err)
|
||||||
cancel()
|
cancel()
|
||||||
|
Loading…
Reference in New Issue
Block a user