Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

fix sending repetitive and inactive peers #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions apps/room/server/room_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"errors"
"io"
"time"

log "github.com/pion/ion-log"
room "github.com/pion/ion/apps/room/proto"
Expand Down Expand Up @@ -65,6 +66,12 @@ func (s *RoomSignalService) Signal(stream room.RoomSignal_SignalServer) error {
if err != nil {
log.Errorf("stream send error: %v", err)
}

err = s.SendPeers(peer)
if err != nil {
log.Errorf("Sending peers err: %v", err)
return err
}
case *room.Request_Leave:
log.Infof("[C->S]=%+v", payload)
reply, err := s.Leave(payload)
Expand Down Expand Up @@ -204,42 +211,6 @@ func (s *RoomSignalService) Join(in *room.Request_Join, stream room.RoomSignal_S
},
}

// find peer in room
key = util.GetRedisPeersPrefixKey(sid)
peersKeys := s.rs.redis.Keys(key)

for _, pkey := range peersKeys {
res := s.rs.redis.HGetAll(pkey)
sid = res["sid"]
uid = res["uid"]
if sid == "" || uid == "" || uid == pinfo.Uid {
continue
}
key = util.GetRedisPeerKey(sid, uid)
res = s.rs.redis.HGetAll(key)
if len(res) != 0 {
info := &room.Peer{
Sid: res["sid"],
Uid: res["uid"],
DisplayName: res["name"],
ExtraInfo: []byte(res["info"]),
Role: room.Role(room.Role_value[res["role"]]),
Protocol: room.Protocol(room.Protocol_value[res["protocol"]]),
Avatar: res["avatar"],
Direction: room.Peer_Direction(room.Peer_Direction_value["direction"]),
Vendor: res["vendor"],
}

err := peer.sendPeerEvent(&room.PeerEvent{
State: room.PeerState_JOIN,
Peer: info,
})

if err != nil {
log.Errorf("signal send peer event error: %v", err)
}
}
}
log.Infof("Join OK: replay=%+v", reply)
return reply, peer, nil
}
Expand Down Expand Up @@ -302,3 +273,27 @@ func (s *RoomSignalService) SendMessage(in *room.Request_SendMessage) (*room.Rep
r.sendMessage(msg)
return &room.Reply_SendMessage{}, nil
}

func (s *RoomSignalService) SendPeers(peer *Peer) error {
sid := peer.SID()
uid := peer.UID()
// Sending active peers to the new peer
log.Infof("Sending peers to: %v", uid)
r := s.rs.getRoom(sid)
peers := r.getPeers()
r.update = time.Now()
for _, p := range peers {
if p.info.Uid == uid {
continue
}
if err := peer.sendPeerEvent(&room.PeerEvent{
State: room.PeerState_JOIN,
Peer: p.info,
}); err != nil {
log.Errorf("send data to peer(%s) error: %v", uid, err)
return err
}
}

return nil
}