endpoints: register before updating state

this way, the new person also get's the current and new state
immediatly.
This commit is contained in:
Henry 2021-01-29 11:55:40 +01:00
parent 9b368d79b6
commit bc6bde116c
1 changed files with 17 additions and 4 deletions

View File

@ -110,22 +110,35 @@ func (rs *roomState) leave(_ context.Context, req *muxrpc.Request) (interface{},
func (rs *roomState) endpoints(_ context.Context, req *muxrpc.Request, snk *muxrpc.ByteSink) error {
level.Debug(rs.logger).Log("called", "endpoints")
toPeer := newForwarder(snk)
// for future updates
rs.broadcaster.Register(toPeer)
ref, err := network.GetFeedRefFromAddr(req.RemoteAddr())
if err != nil {
return err
}
rs.roomsMu.Lock()
// add ref to lobby
lobby := rs.rooms["lobby"]
if _, has := lobby[ref.Ref()]; !has {
// if the peer didn't call tunnel.announce()
if _, has := lobby[ref.Ref()]; has {
// just send the current state to the new peer
toPeer.Update(lobby.asList())
} else {
// register them as if they did
lobby[ref.Ref()] = req.Endpoint()
rs.updater.Update(lobby.asList())
rs.rooms["lobby"] = lobby
// update everyone
rs.updater.Update(lobby.asList())
}
// send the current state
rs.roomsMu.Unlock()
rs.broadcaster.Register(newForwarder(snk))
return nil
}