Files
screego/ws/event_clientanswer.go
T
Jannis Mattheis 3191988801 Don't fail on unknown session
There may be a race condition if a user disconnects and the other peer
just sent a p2p message.

Fixes #14
2020-10-23 19:40:06 +02:00

42 lines
799 B
Go

package ws
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/screego/server/ws/outgoing"
)
func init() {
register("clientanswer", func() Event {
return &ClientAnswer{}
})
}
type ClientAnswer outgoing.P2PMessage
func (e *ClientAnswer) Execute(rooms *Rooms, current ClientInfo) error {
if current.RoomID == "" {
return fmt.Errorf("not in a room")
}
room, ok := rooms.Rooms[current.RoomID]
if !ok {
return fmt.Errorf("room with id %s does not exist", current.RoomID)
}
session, ok := room.Sessions[e.SID]
if !ok {
log.Debug().Str("id", e.SID.String()).Msg("unknown session")
return nil
}
if session.Client != current.ID {
return fmt.Errorf("permission denied for session %s", e.SID)
}
room.Users[session.Host].Write <- outgoing.ClientAnswer(*e)
return nil
}