1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-14 04:35:57 +01:00

Implement ping/pong exchanges and client timeouts.

This commit is contained in:
Juliusz Chroboczek 2020-04-25 22:44:24 +02:00
parent 0dfa71ed71
commit b201c3d93c
2 changed files with 31 additions and 0 deletions

View file

@ -707,8 +707,12 @@ func clientLoop(c *client, conn *websocket.Conn) error {
} }
} }
readTime := time.Now()
ticker := time.NewTicker(2 * time.Second) ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop() defer ticker.Stop()
slowTicker := time.NewTicker(10 * time.Second)
defer slowTicker.Stop()
for { for {
select { select {
@ -718,6 +722,7 @@ func clientLoop(c *client, conn *websocket.Conn) error {
} }
switch m := m.(type) { switch m := m.(type) {
case clientMessage: case clientMessage:
readTime = time.Now()
err := handleClientMessage(c, m) err := handleClientMessage(c, m)
if err != nil { if err != nil {
return err return err
@ -793,6 +798,18 @@ func clientLoop(c *client, conn *websocket.Conn) error {
} }
case <-ticker.C: case <-ticker.C:
sendRateUpdate(c) sendRateUpdate(c)
case <-slowTicker.C:
if time.Since(readTime) > 90*time.Second {
return errors.New("client is dead")
}
if time.Since(readTime) > 60*time.Second {
err := c.write(clientMessage{
Type: "ping",
})
if err != nil {
return err
}
}
} }
} }
} }
@ -856,6 +873,12 @@ func handleClientMessage(c *client, m clientMessage) error {
if err != nil { if err != nil {
return c.error(err) return c.error(err)
} }
case "pong":
// nothing
case "ping":
c.write(clientMessage{
Type: "pong",
})
default: default:
log.Printf("unexpected message: %v", m.Type) log.Printf("unexpected message: %v", m.Type)
return protocolError("unexpected message") return protocolError("unexpected message")

View file

@ -338,6 +338,14 @@ function serverConnect() {
case 'chat': case 'chat':
addToChatbox(m.id, m.username, m.value, m.me); addToChatbox(m.id, m.username, m.value, m.me);
break; break;
case 'ping':
send({
type: 'pong',
});
break;
case 'pong':
/* nothing */
break;
case 'error': case 'error':
displayError(m.value); displayError(m.value);
break; break;