mirror of
https://github.com/screego/server.git
synced 2026-04-22 23:47:03 +08:00
3191988801
There may be a race condition if a user disconnects and the other peer just sent a p2p message. Fixes #14
42 lines
799 B
Go
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
|
|
}
|