Files
screego/ws/event_join.go
Jannis Mattheis a0f3c37498 fix: race condition
In client.go

    if room, ok := message.(outgoing.Room); ok {
            c.info.RoomID = room.ID
    }

this part isn't thread safe. It could happen that user disconnected but
wasn't removed from a room, because the disconnecting go routine
couldn't see the roomID yet.
2024-10-11 15:11:07 +02:00

61 lines
1.1 KiB
Go

package ws
import (
"fmt"
)
func init() {
register("join", func() Event {
return &Join{}
})
}
type Join struct {
ID string `json:"id"`
UserName string `json:"username,omitempty"`
}
func (e *Join) Execute(rooms *Rooms, current ClientInfo) error {
if rooms.connected[current.ID] != "" {
return fmt.Errorf("cannot join room, you are already in one")
}
room, ok := rooms.Rooms[e.ID]
if !ok {
return fmt.Errorf("room with id %s does not exist", e.ID)
}
name := e.UserName
if current.Authenticated {
name = current.AuthenticatedUser
}
if name == "" {
name = rooms.RandUserName()
}
room.Users[current.ID] = &User{
ID: current.ID,
Name: name,
Streaming: false,
Owner: false,
Addr: current.Addr,
Write: current.Write,
}
rooms.connected[current.ID] = room.ID
room.notifyInfoChanged()
usersJoinedTotal.Inc()
v4, v6, err := rooms.config.TurnIPProvider.Get()
if err != nil {
return err
}
for _, user := range room.Users {
if current.ID == user.ID || !user.Streaming {
continue
}
room.newSession(user.ID, current.ID, rooms, v4, v6)
}
return nil
}